文件未复制到其他文件夹

时间:2015-02-23 11:22:31

标签: php file

我正在尝试将所有.vtk文件和.raw文件移动到其他文件夹。但它没有被复制。我该如何解决这个问题?

<?php
define ('DOC_ROOT', $_SERVER['DOCUMENT_ROOT'].'/');
$src = '/var/www/html/php/';
$dest ='/var/www/html/php/emd/';
$dh = opendir ($src); //Get a directory handle
$validExt = array('vtk','raw'); //Define a list of allowed file types
$filesMoved = 0;

// Loop through all the files in the directory checking them and moving them
while (($file = readdir ($dh)) !== false) {
   // Get the file type and convert to lower case so the array search always matches
   $fileType = strtolower(pathinfo ($file, PATHINFO_EXTENSION));
   if(in_array ($fileType, $validExt)) {
      // Move the file - if this is for the web really you should create a web safe file name
      if (!$rename($src.$file, $dest.) {
        echo  "Failed to move {$file} to {$newPath}";
      } else {
        echo "Moved {$file} to {$newPath}";
        $filesMoved++;
      }
   }
}
echo "{$filesMoved} files were moved";
closedir($dh); 
?>

1 个答案:

答案 0 :(得分:1)

您忘记添加目标文件名,请更改此行:

if (!$rename($src.$file, $dest.) {

成:

if (!$rename($src.$file, $dest.$file) {

如果这不起作用,请确保目标目录确实存在,并且您具有写入权限。如果您启用了错误报告,则会看到如下错误消息:

  

解析错误:解析错误   /path/to/script/rename.php上   第18行