php 取消链接。不返回错误消息并且不删除我的文件

发布于 2024-11-30 22:25:28 字数 482 浏览 2 评论 0原文

我只是想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

浪荡不羁 2024-12-07 22:25:28

您使用的代码不必要地麻烦。简单地调用 unlink 就可以解决问题:

unlink( $log_file );

但是让我们找出问题所在。该文件存在,因为您进入了打印语句完成的循环。 unlink 调用可能返回 false,因为输出是“11”而不是“111”。

所以我的直觉告诉我,这一定是文件权限问题。您确定网络用户有权删除该文件吗?您能否向我们展示文件夹权限,例如通过在命令行上运行 ls -la 并粘贴输出?

The code you used is needlessly cumbersome. A simple call to unlink should do the trick:

unlink( $log_file );

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?

瀞厅☆埖开 2024-12-07 22:25:28

unlink 返回 truefalse。如果您尝试打印 false (print $deleted;),这将打印一个空字符串。

试试这个:

print $deleted ? "The file was deleted" : "The file was not deleted";

unlink returns either true or false. If you try to print false (print $deleted;), this will print an empty string.

Try this instead:

print $deleted ? "The file was deleted" : "The file was not deleted";
海风掠过北极光 2024-12-07 22:25:28

我认为您需要检查 $log_path 是否是文件。使用:

if( is_file( $log_path ) AND is_readable( $log_path ) )

此外,在脚本开头添加下一行以显示所有错误和警告:

ini_set( 'error_reporting', E_ALL );

I think you need to check if $log_path is file. Use:

if( is_file( $log_path ) AND is_readable( $log_path ) )

Also, add next line to begin of your script to show all errors and warnings:

ini_set( 'error_reporting', E_ALL );
§对你不离不弃 2024-12-07 22:25:28

并不是说你的文件必须是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;

窝囊感情。 2024-12-07 22:25:28

我真的不知道问题所在,但您应该检查文件是否可写,而不是可读:

<?php
$log_file = "/tmp/test.log";
$log_path = realpath($log_file);
echo "Testing: ". $log_path."\n";
if (is_writable($log_path)) {
    echo "exists?  ";
    echo (file_exists($log_path))?"yes\n":"no\n";
    $deleted = unlink($log_path);
    echo "deleted? ";
    echo ($deleted)?"yes\n":"no\n";
    echo "exists?  ";
    echo (file_exists($log_path))?"yes\n":"no\n";
} else {
    echo "file unwritable\n";
}

此代码对我来说工作正常(是的,它也很混乱;))。

I don't really know the problem, but you should check, if the file is writable and not if it is readable:

<?php
$log_file = "/tmp/test.log";
$log_path = realpath($log_file);
echo "Testing: ". $log_path."\n";
if (is_writable($log_path)) {
    echo "exists?  ";
    echo (file_exists($log_path))?"yes\n":"no\n";
    $deleted = unlink($log_path);
    echo "deleted? ";
    echo ($deleted)?"yes\n":"no\n";
    echo "exists?  ";
    echo (file_exists($log_path))?"yes\n":"no\n";
} else {
    echo "file unwritable\n";
}

This code works fine for me (yes, it's also messy ;) ).

不再见 2024-12-07 22:25:28

file_exists 缓存操作结果,您应该调用 clearstatcache () 在第二次调用之前

file_exists caches the result of the operation, you should call clearstatcache() before the second call

泼猴你往哪里跑 2024-12-07 22:25:28

对我来说,这是承诺问题解决它,

  chown    -R www-data:www-data      /var/www/
  chmod    -R g+rwx                  /var/www/
  chmod    -R 0755                  /var/www/

确保 php 在 www.conf 中的用户 www-data 下运行,例如我正在使用 PHP 7.2
nano /etc/php/7.2/fpm/pool.d/www.conf
更改这些值:-

listen.owner = www-data
listen.group = www-data

for me it was promises issue solve it with

  chown    -R www-data:www-data      /var/www/
  chmod    -R g+rwx                  /var/www/
  chmod    -R 0755                  /var/www/

make sure that php run under user www-datain www.conf e.g I'm using PHP 7.2
nano /etc/php/7.2/fpm/pool.d/www.conf
Change these values :-

listen.owner = www-data
listen.group = www-data
内心激荡 2024-12-07 22:25:28

在我的例子中,unlink 函数每次都返回 false。首先,感谢维克托:

此外,在脚本开头添加下一行以显示所有错误和警告:ini_set( 'error_reporting', E_ALL );

它帮助我看到警告:

unlink():http 不允许在 PHP 中取消链接。

我终于找到了解决方案,描述如下https://a1websitepro。 com/warning-unlink-http-not-allow-unlinking-php/

情况是我使用文件的 http 路径,但 unlink 函数需要服务器中文件的绝对路径。

unlink function in my case just returned false everytime. First, thanks to Viktor for this:

Also, add next line to begin of your script to show all errors and warnings: ini_set( 'error_reporting', E_ALL );

It helped me to see warning:

unlink(): http does not allow unlinking in PHP.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文