Php 取消链接有时会失败-需要改进
我使用我自己制作的这个自定义的简单脚本,虽然有点失败,但它总是返回true 此类Php 取消链接有时会失败-需要改进,php,delete-file,unlink,Php,Delete File,Unlink,我使用我自己制作的这个自定义的简单脚本,虽然有点失败,但它总是返回true 此类取消链接放置在数组中的文件,该数组通过这些文件,然后在成功删除文件时递增计数器,这是我的脚本: <?php class Delete { function deleteFiles($array) { if(is_array($array)) { $filecount = count($array); $count
取消链接
放置在数组中的文件,该数组通过这些文件,然后在成功删除文件时递增计数器,这是我的脚本:
<?php
class Delete
{
function deleteFiles($array)
{
if(is_array($array))
{
$filecount = count($array);
$count = 0;
foreach($array as $file)
{
if(file_exists($file))
{
$remove = unlink($file);
if($remove)
{
$count++;
}
}
else
{
return false;
}
}
if($count == $filecount)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
?>
基本上,我需要改进如何使其万无一失,以便阵列中的图像被完全删除一旦取消链接删除了文件而没有删除,则不会返回true,因此我不知道为什么它实际上没有正确删除文件
有时它工作得很好。您可以在
取消链接后添加对is_file()
的调用,以检查文件是否仍然存在将if($remove)
更改为if($remove&&!file_存在($file))
另外,请注意,有时在Windows上取消链接会失败:我实际上在想,如果在(!file_exists($file)==true){unlink($file);}
时有一个while
循环是否会好,例如,while(!file_exists($file)==true){unlink($file);}
-这可能吗?如果问题类似于文件权限,那么这将导致无限循环。你需要弄清楚/为什么/unlink()调用失败了你在使用哪个操作系统?我以前听说过在Windows上使用取消链接时出现的问题。