检查文件复制是否成功

时间:2013-01-30 18:17:59

标签: php file copy

运行命令copy($uploadedFile, "pdf/".$fullFileName);后,验证文件是否成功复制的最快最有效方法是什么?

3 个答案:

答案 0 :(得分:7)

这就够了吗?

if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n";
}

参考:http://php.net/manual/en/function.copy.php

答案 1 :(得分:0)

如果查看copy function in the PHP documentation,您会看到:

Returns TRUE on success or FALSE on failure.

所以,简单如下:

if(!copy($uploadedFile, "pdf/".$fullFileName)) {
    // Failure code
}

或者:

$returnCode = copy($uploadedFile, "pdf/".$fullFileName);
if(!$returnCode) {
    // Failure code
}

就足够了。

答案 2 :(得分:0)

你可以比较大小whlist复制 - 如果大小等于我们可以假设复制完成..

$fs1=$fs='';

$filename = 'test.zip'; // copy from ftp or slow copy..

  if (ob_get_level() == 0) ob_start();

  for ($i = 0; $i<25; $i++){

          echo "<hr> Compare \n";
          echo "<br>fs1: $fs1";

          $fs='';
          $fs = filesize($filename);
          echo "<br>fs: $fs";
          if ( $i > 0 )
              if (  $fs1 === $fs ) break;

          $fs1 = $fs;

          ob_flush();
          flush();
          sleep(2);
          clearstatcache();
  }

  echo "<br>Done copying.";
  ob_end_flush();