php PHP取消链接不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19026809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:40:52  来源:igfitidea点击:

PHP Unlink Not working

phpunlink

提问by Sunil

I am trying to delete photo in php using unlink. I have used it earlier on other server but this time it is not working. I have used absolute path for a test but still does not works:

我正在尝试使用 unlink 删除 php 中的照片。我之前在其他服务器上使用过它,但这次它不起作用。我已经使用绝对路径进行测试,但仍然不起作用:

I have used it as: unlink('img1.jpg');

我用它作为: unlink('img1.jpg');

and :

和 :

unlink('http://www.mysite.com/img1.jpg');

Please anyone having such experience?

请问有这种经历的人吗?

回答by Shakti Patel

url not allow in ulink function

url 在 ulink 函数中不允许

can you please used this

你能用这个吗

It's better, also safety wise to use an absolute path. But you can get this path dynamically.

使用绝对路径更好,也是安全的。但是您可以动态获取此路径。

E.g. using:

例如使用:

getcwd();

Depending on where your PHP script is, your variable could look like this:

根据您的 PHP 脚本所在的位置,您的变量可能如下所示:

$deleteImage =  getcwd() . 'img1.jpg';

unlink($deleteImage);

check this

检查这个

bool unlink ( string $filename [, resource $context ] )
bool unlink ( string $filename [, resource $context ] )

and

filename
Path to the file.

文件名 文件的
路径。

So it onlytakes a string as filename.

所以它只需要一个字符串作为文件名。

Make sure the file is reachable with the path from the location you execute the script. This is not a problem with absolute paths, but you might have one with relative paths.

确保通过执行脚本的位置的路径可以访问该文件。这不是绝对路径的问题,但您可能有相对路径的问题。

回答by Davide De Santis

Even though unlink() supports URLs (see here) now, http:// is not supported: http wrapper information

尽管 unlink()现在支持 URLs(见这里),但不支持 http://:http 包装器信息

use a filesystem path to delete the file.

使用文件系统路径删除文件。

回答by Radon8472

If you use unlink in a linux or unix you should also check the results of is_writable ( string $filename )And if the function returns false, you should check the file permissions with fileperms ( string $filename ).

如果您在 linux 或 unix 中使用 unlink,您还应该检查is_writable ( string $filename )的结果, 如果函数返回 false,您应该使用fileperms ( string $filename )检查文件权限。

File permissions are usual problems on webspaces, e.g. if you upload an file per ftp with a ftp user, and the webserver is running as an different user.

文件权限是网络空间上的常见问题,例如,如果您使用 ftp 用户为每个 ftp 上传文件,并且网络服务器以不同的用户身份运行。

If this is the problem, you have do to a

如果这是问题,你必须做一个

chmod o+rwd img1.jpg

chmod o+rwd img1.jpg

or

或者

chmod 777 img1.jpg

chmod 777 img1.jpg

to grand write (and delete) permissions for other Users.

为其他用户授予写入(和删除)权限。

回答by sameh salah

unlink($fileName);failed for me.
Then I tried using the realpath($fileName)function as unlink(realpath($fileName));it worked.

unlink($fileName);对我来说失败了。
然后我尝试使用该realpath($fileName)功能unlink(realpath($fileName));

Just posting it, in case if any one finds it useful.

只是张贴它,以防万一有人觉得它有用。

php unlink

php取消链接

回答by DS9

use filesystem path,
first define path like this:

使用文件系统路径,
首先像这样定义路径:

define("WEB_ROOT",substr(dirname(__FILE__),0,strlen(dirname(__FILE__))-3));

and check file is exist or not,if exist then unlink the file.

并检查文件是否存在,如果存在则取消链接文件。

$filename=WEB_ROOT."img1.jpg";
if(file_exists($filename))
{
$img=unlink(WEB_ROOT."img1.jpg");
}

回答by Rajeev Ranjan

unlink won't work with unlink('http://www.mysite.com/img1.jpg');

取消链接将无法使用 unlink('http://www.mysite.com/img1.jpg');

use instead unlink($_SERVER['DOCUMENT_ROOT'].'img1.jpg');//takes the current directory or,

改用 unlink($_SERVER['DOCUMENT_ROOT'].'img1.jpg');// 获取当前目录,或者,

unlink($_SERVER['DOCUMENT_ROOT'].'dir_name/img1.jpg');

There may be file permission issue.please check for this.

可能存在文件权限问题。请检查这一点。

回答by Tirthankar Kundu

Give relative path from the folder where images are kept to the file where you are writing script. If file structure is like:

提供从保存图像的文件夹到您正在编写脚本的文件的相对路径。如果文件结构是这样的:

-your php file
-images
  -1.jpg

then 

unlink(images/1.jpg);

Or there may be some folder permission issue. Your files are on a server or you are running it on localhost? If it is on a server then give 755 permission to the images folder.

或者可能存在一些文件夹权限问题。您的文件在服务器上还是在本地主机上运行?如果它在服务器上,则为图像文件夹授予 755 权限。