Я пытаюсь переместить все файлы.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);
?>
Вы забыли добавить имя файла для адресата, измените эту строку:
if (!$rename($src.$file, $dest.) {
в:
if (!$rename($src.$file, $dest.$file) {
Если это не работает, убедитесь, что каталог назначения действительно существует и у вас есть права на запись. Если вы включили отчет об ошибках, вы бы увидели сообщение об ошибке:
Ошибка анализа: ошибка синтаксического анализа в /path/to/script/rename.php в строке 18