【发布时间】:2018-03-13 21:55:15
【问题描述】:
我有一个从 SD 卡中提取照片的应用。复制后,卡片将被重新格式化并放回相机中,并且将存储更多照片。
目前,我不使用 PHP copy() 函数,而是(大致)执行以下操作:
$card = '/Volumes/SD_Card/DCIM/EOS/';
$files = scandir($card);
$target = '/Volumes/HARD_DRIVE/photos/';
foreach($files as $k => $file) {
if( strtolower ( pathinfo($file,PATHINFO_EXTENSION) ) == 'jpg') {
$img_data = file_get_contents($file);
$orig_md5 = md5($img_data);
$success = file_put_contents($target . $file, $img_data);
unset ($img_data);
if( $success != TRUE ) {
echo "an error occurred copying $file\n"; exit;
} elseif ( $orig_md5 != md5_file($target . $file) ) {
echo "an error occurred confirming data of $file\n"; exit;
} else {
echo "$file copied successfully.\n";
unlink ($img_data);
}
}
}
我目前正在这样做,因此我可以比较 md5 散列,以确保副本与原件逐位匹配。
我的问题是:
1) 使用 php copy() 会更快吗?我认为它会,因为目标文件不必被读入内存来检查 md5 哈希。
2) 在返回TRUE/FALSE之前,copy() 是否会在函数中进行某种哈希检查以确保副本的完整性?
【问题讨论】:
-
你也可以用rename()一键移动/删除,为什么觉得功能不靠谱?