尽管成功,但PHP重命名警告


php rename warning despite being successfull

我正在重命名,以便我可以移动文件夹。移动成功,但我不断收到警告:

警告:重命名(site_files/259,垃圾箱/site_files/259( [function.rename]:第 79 行的/home/oosman/public_html/lib.php 中没有这样的文件或目录

这是我的代码:

$path_parts = pathinfo($file);
$d = $path_parts['dirname'];
$f = $path_parts['basename'];
$trashdir='trash/'.$d;
mkdir2($trashdir);
if(!is_dir($trashdir))
    return FALSE;
rename($file, $trashdir.'/'.$f); // this is line 79 where the warning is coming from

为什么我会收到此警告?

仅供参考,mkdir2 只是我的递归 mkdir 函数

function mkdir2($dir, $mode = 0755)
{
    if (@is_dir($dir) || @mkdir($dir,$mode)) return TRUE;
    if (!mkdir2(dirname($dir),$mode)) return FALSE;
    return @mkdir($dir,$mode);
}

这只是因为源文件夹或目标文件夹不存在。

无论如何,这将删除警告,但不是解决问题的最佳方法:

if(file_exists($file) && file_exists($trashdir)){
    rename($file, $trashdir.'/'.$f);
}

为了找出问题的真正原因,请检查以下问题:

1.源文件(site_files/259(是否存在?它有像259.txt这样的扩展名吗?

从你的日志中,我想原始文件的绝对路径应该是/home/oosman/public_html/site_files/259

2.是否成功创建了目标文件夹?你能在磁盘上看到它并从mkdir2()获得TRUE吗?

3.我强烈建议您在使用rename()时使用绝对路径而不是相对路径。

rename('/home/oosman/public_html/site_files/259', '/home/oosman/public_html/trash/site_files/259');

但不是

rename('site_files/259', 'trash/site_files/259');

也许相对路径有问题?

更新 2014-12-04 12:00:00 (GMT +900(:

由于上面没有提到任何内容,请您记录一些内容来帮助我澄清吗?

请更改

rename($file, $trashdir.'/'.$f);

echo "Before moving:'n"
echo "Orgin:".file_exists($file)."'n";
echo "Target parent folder:".file_exists($trashdir)."'n";
echo "Target file:".file_exists($trashdir.'/'.$f)."'n";
rename($file, $trashdir.'/'.$f);
echo "After moving:'n"
echo "Orgin:".file_exists($file)."'n";
echo "Target parent folder:".file_exists($trashdir)."'n";
echo "Target file:".file_exists($trashdir.'/'.$f)."'n";

如果输出:

Before moving:
Origin:1
Target parent folder:1
Target file:0
Warning: rename(site_files/259,trash/site_files/259) [function.rename]: No such file or directory in /home/oosman/public_html/lib.php on line 83
After moving:
Origin:0
Target parent folder:1
Target file:1

正好只有一次,然后我就出去了。如果没有,请告诉我区别。

一种可能性是简单地隐藏警告:

error_reporting(E_ALL & ~E_WARNING);
rename($file, $trashdir.'/'.$f);
error_reporting(E_ALL & ~E_NOTICE);

我在传输完成时遇到了通过重命名功能发出"警告"的相同问题><。问题来自将一个卷转移到另一个卷。以及源文件和目标文件之间的不同权限.
这个错误在 PHP 中被引用:错误
实际上,rename(( 函数执行以下操作:
copy、chmod、chown 和 unlink

就我而言,我认为 chown(( 操作失败,因此发出了"警告"。为了克服这个问题,而不是简单地用error_reporting(E_ALL & ~E_WARNING)隐藏所有警告,我实现了以下代码:

// If Source File Present and Accessible
if(@is_file($sourceFilePath))
{
    // If no error during transfer (we omit the warning with @)
    if(@rename($sourceFilePath,$destinationFilePath))
    {
        // If the transfer was really successful (and the file is accessible)
        if(@is_file($destinationFilePath))
        {
          echo "OK";
        }
        else echo "Error : File Not Really Transfered";
    }
    else echo "Error : File Not Transfered";
}
else echo "Error : Source File Not Present";

希望这个答案能有所帮助