无法打开流:副本中的参数无效

时间:2014-07-08 09:57:51

标签: php copy

我想从文件中读取具有文件名的文本,并希望将这些文件复制到

另一个地方,不幸的是它无法正常工作,如何解决这个问题?提前致谢。

//fetches the view file names to copy
$handle = @fopen("D:/myfolder/log.txt", "r");
if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {
        // echo $buffer;
        $filenames[] = $buffer;
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}

foreach ($filenames as $row) {
    $fileNames  = explode("/", strval($row));
    $singleFile = "C:/wamp/www/project/application/modules/admin/" . strval($row);
    $file = $singleFile;  
    $newfile = "C:/Users/myname/Dropbox/local_changes/$fileNames[2]";
   //copy a file from one location to another
    if (!copy($file, $newfile)) {
        echo "failed to copy $file...\n";
    }
}
?>

log.txt的示例如下:

views/layouts/file1.phtml
views/layouts/file2.phtml

1 个答案:

答案 0 :(得分:1)

尝试让您的代码看起来更清晰。我试图改进代码的可读性。我猜读源文件有错误。

<?php
//
// Setup.
//
$pathCopyInstructionsFile = 'D:/myfolder/log.txt';
$pathFilesSourceDirectory = 'C:/wamp/www/project/application/modules/admin/';
$pathFilesDestinationDirectory = 'C:/Users/myname/Dropbox/local_changes/';

//
// Execution.
// 

// Read copy instructions file (log).
if(!file_exists($pathCopyInstructionsFile)) {
    die('File "'. $pathCopyInstructionsFile .'" does not exist');
}
$filesToCopy = file($pathCopyInstructionsFile, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);

// Loop through $filesToCopy and try to copy them from $pathFilesSourceDirectory to $pathFilesDestinationDirectory.
foreach($filesToCopy as $fileToCopy) {
    // Set source file path and check if file exists - otherwise continue loop.
    $sourceFilepath = $pathFilesSourceDirectory . $fileToCopy;
    if(!file_exists($sourceFilepath)) {
        echo('Source file "'. $sourceFilepath .'" not found' . "\n");
        continue;
    }

    // Set destination file path. Only use filename itself for copying.
    $destinationFilepath = $pathFilesDestinationDirectory . basename($fileToCopy);

    // Try to copy.
    $successfulCopy = copy($sourceFilepath, $destinationFilepath);
    if(!$successfulCopy) {
        echo('Source file "'. $sourceFilepath .'" could not be copied to "'. $destinationFilepath . '"' . "\n");
    }
}
?>