php 取消链接。不返回错误消息并且不删除我的文件
我只是想使用 PHP 的取消链接来删除文件。我在这里找到了一个答案,它告诉我执行此操作的最佳方法,并且我实施了它。这是我正在使用的代码。
$log_path = realpath($log_file);
if (is_readable($log_path)){
print file_exists($log_path);
$deleted = unlink($log_path);
print "Deleted:".$deleted;
print "Exists:".file_exists($log_path);
}
这只是打印 1Deleted:exists:1
。我还尝试在取消链接下添加 print_r(error_get_last());
,但这也没有返回任何内容。该目录中的所有文件都是 chmod 777 *
并且没有其他文件处理程序打开。 ……到底什么啊……
I'm simply trying to delete a file using PHP's unlink. I found an answer on here that told me the best way to do this and I implemented it. Heres the code I am using.
$log_path = realpath($log_file);
if (is_readable($log_path)){
print file_exists($log_path);
$deleted = unlink($log_path);
print "Deleted:".$deleted;
print "Exists:".file_exists($log_path);
}
This just prints 1Deleted:exists:1
. I also tried to add in print_r(error_get_last());
under the unlink, but this also returned nothing. All files in the directory are chmod 777 *
and there are no other file handlers open. ....what the heck...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您使用的代码不必要地麻烦。简单地调用 unlink 就可以解决问题:
但是让我们找出问题所在。该文件存在,因为您进入了打印语句完成的循环。
unlink
调用可能返回 false,因为输出是“11”而不是“111”。所以我的直觉告诉我,这一定是文件权限问题。您确定网络用户有权删除该文件吗?您能否向我们展示文件夹权限,例如通过在命令行上运行 ls -la 并粘贴输出?
The code you used is needlessly cumbersome. A simple call to unlink should do the trick:
But let's find out what is going wrong. The file exists, because you enter the loop where the print statements are done. The
unlink
call probably returns false, because the output is "11" and not "111".So my gut says, it must be a file permissions issue. Are you sure the web user has permission to remove the file? Can you show us the folder permissions, for instance by running
ls -la
on the command line and pasting the output?unlink 返回
true
或false
。如果您尝试打印false
(print $deleted;
),这将打印一个空字符串。试试这个:
unlink returns either
true
orfalse
. If you try to printfalse
(print $deleted;
), this will print an empty string.Try this instead:
我认为您需要检查 $log_path 是否是文件。使用:
此外,在脚本开头添加下一行以显示所有错误和警告:
I think you need to check if $log_path is file. Use:
Also, add next line to begin of your script to show all errors and warnings:
并不是说你的文件必须是0777,还需要你的目录可以访问。你的目录的mod是什么?
接下来:
print $deleted;
显然打印 false,这显示为空。试试这个:echo $deleted ? 1:0;
Its not that your files need to be 0777. It also necessary that your directory can be accessed. What's the mod of your directory?
Next:
print $deleted;
apparently print false, which is shown as nothing. Try this:echo $deleted ? 1 : 0;
我真的不知道问题所在,但您应该检查文件是否可写,而不是可读:
此代码对我来说工作正常(是的,它也很混乱;))。
I don't really know the problem, but you should check, if the file is writable and not if it is readable:
This code works fine for me (yes, it's also messy ;) ).
file_exists
缓存操作结果,您应该调用 clearstatcache () 在第二次调用之前file_exists
caches the result of the operation, you should call clearstatcache() before the second call对我来说,这是承诺问题解决它,
确保 php 在
www.conf
中的用户www-data
下运行,例如我正在使用 PHP 7.2nano /etc/php/7.2/fpm/pool.d/www.conf
更改这些值:-
for me it was promises issue solve it with
make sure that php run under user
www-data
inwww.conf
e.g I'm using PHP 7.2nano /etc/php/7.2/fpm/pool.d/www.conf
Change these values :-
在我的例子中,
unlink
函数每次都返回false
。首先,感谢维克托:它帮助我看到警告:
我终于找到了解决方案,描述如下https://a1websitepro。 com/warning-unlink-http-not-allow-unlinking-php/。
情况是我使用文件的 http 路径,但
unlink
函数需要服务器中文件的绝对路径。unlink
function in my case just returnedfalse
everytime. First, thanks to Viktor for this:It helped me to see warning:
And i finally found the solution, described here https://a1websitepro.com/warning-unlink-http-not-allow-unlinking-php/.
The case is that I use http path to the file, but
unlink
function requires the absolute path to the file from your server.