简体   繁体   English

unlink无法删除文件

[英]unlink cannot delete file

I'm frustrated about deleting file in ubuntu using PHP unlink(). 我对使用PHP unlink()在ubuntu中删除文件感到沮丧。

I created a very simple simulation as follow: 我创建了一个非常简单的模拟如下:

  1. create a folder named "files" beneath /var/www with 766 permission. 在/ var / www下创建一个名为“files”的文件夹,具有766权限。
  2. upload a file, let say "image.png" in that folder & set the permission into 666 上传文件,在该文件夹中说“image.png”并将权限设置为666
  3. create a php file named delete.php, set the permission to 644 and upload to /var/www directory 创建一个名为delete.php的php文件,将权限设置为644并上传到/ var / www目录
  4. Call the file in browser (I use localhost) 在浏览器中调用该文件(我使用localhost)

The "image.png" still exists in "files" directory “image.png”仍然存在于“files”目录中

Here is the php script of delete.php : 这是delete.php的php脚本:

$filename = 'image.png';
$file = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . $filename;
unlink($file);

I also tried the following script : 我还尝试了以下脚本:

$filename = 'image.png';
$dir = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'files';
chdir($dir);
unlink($filename);

But still can't delete the file. 但仍无法删除该文件。

Unlink throws a warning on failure. Unlink会在失败时抛出警告。 Check if E_WARNING is visible for you to find out whats going on. 检查E_WARNING是否可见,以便了解最新情况。

It usually boils down to user rights. 它通常归结为用户权利。 Keep in mind if your script is executed by a browser, usually a user named wwwrun or wwwdata (or something similar) is executing it on your server. 请记住,如果您的脚本是由浏览器执行的,通常名为wwwrunwwwdata (或类似的东西)的用户正在您的服务器上执行它。

Check if this user has permissions to delete, then try again. 检查此用户是否有权删除,然后重试。

The folder/owner of the directory could be a different user than the user being used to run php. 目录的文件夹/所有者可以是与用于运行php的用户不同的用户。

You should create a folder with the user php assigned. 您应该创建一个分配了用户php的文件夹。 If you cannot do that yourself ask your ISP to do it. 如果您不能这样做,请让您的ISP进行此操作。 That is how I solved a similar problem. 这就是我解决类似问题的方法。

One user cannot delete files of another user on a unix system. 一个用户无法删除unix系统上其他用户的文件。 If you would set it to 777 then you could delete it... 如果您将其设置为777,那么您可以将其删除...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM