Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php can';t取消链接url格式的图像_Php_File_Unlink_Getcwd - Fatal编程技术网

Php can';t取消链接url格式的图像

Php can';t取消链接url格式的图像,php,file,unlink,getcwd,Php,File,Unlink,Getcwd,php在回显$\u POST['img'] http://localhost/uploads/images/1533033949-8.jpg 但是为什么取消链接不起作用呢- // Get src. $img = $_POST["img"]; // Check if file exists. if (file_exists(getcwd() . $img)) { // Delete file. unlink(getcwd() . $img); echo "Deleted"; }

php在回显
$\u POST['img']

http://localhost/uploads/images/1533033949-8.jpg
但是为什么取消链接不起作用呢-

// Get src.
$img = $_POST["img"];

// Check if file exists.
if (file_exists(getcwd() . $img)) {
  // Delete file.
  unlink(getcwd() . $img);
  echo "Deleted";
}
我试着直接测试,但不起作用

unlink($img)
取消链接在文件系统上工作,而不是在HTTP URL上工作。和附加

@CBroe是正确的

首先获取live server上的基本路径 或者手动指定基本路径,如下面的示例所示

$base_directory = '/home/myuser/';
然后取消需要删除的文件的链接

if(unlink($base_directory))
    echo "File has been Deleted.";

我希望它能有所帮助。

最后我解决了将url信息存储为变量和php
substr
strlen
函数的问题

$img=$_POST['img'];
$len = strlen("http://localhost/uploads/");
$new_path = substr($img, $len, strlen($img)-$len); 
if(unlink($new_path)){
    echo "Deleted";
}
else{
    echo "Fail";
}

文件是否存在于
getcwd()$img
?你把它打印出来看看是什么样子了吗?您/您的脚本/您的服务器是否具有取消文件链接的权限?
unlink
在文件系统上工作,而不使用HTTP URL。和附加
http://localhost/uploads/images/1533033949-8.jpg
到当前工作目录不太可能为您提供正确的文件系统路径。
getcwd()
上的手册明确指出:“成功时返回当前工作目录,失败时返回FALSE。”*,而不是URL。另外,上面的示例将输出类似于:/home/didou/home/didou/cvs的内容。看看你现在得到的网址;不一样吧?好了,wink。使用
substr,strlen
在变量中转换url信息是可行的。谢谢,但它不起作用,在尝试了许多方法后,我在php
substr,strlen
函数的帮助下解决了将url信息存储为变量和路径的问题,我已经发布了答案