我创建了一个上传站点,由于某种原因,处理 .dmg 文件在下载时总是最终损坏。任何其他文件类型都可以正常工作,但如果有人上传 .dmg 然后下载它,Mac OS X 会说它已损坏,但这不会发生在任何其他文件上。有没有其他人有这个问题。
这是我的上传代码:
$temp_file = $_FILES['Filedata']['tmp_name'];
$ext = pathinfo($_FILES['Filedata']['name'], PATHINFO_EXTENSION);
$new_file_name = md5(uniqid(rand(), true));
$target_file = rtrim(enc_target_path, '/') . '/' . $new_file_name . '.enc.' . $ext;
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$key = substr(md5('some_salt' . $password, true) . md5($password . 'more_salt', true), 0, 24);
$opts = array('iv' => $iv, 'key' => $key);
$my_file = fopen($temp_file, 'rb');
$encrypted_file_name = $target_file;
$encrypted_file = fopen($encrypted_file_name, 'wb');
stream_filter_append($encrypted_file, 'mcrypt.rijndael_128', STREAM_FILTER_WRITE, $opts);
//stream_copy_to_stream($my_file, $encrypted_file);
while (!feof($my_file)) {
fwrite($encrypted_file, fread($my_file, 4096));
}
fclose($encrypted_file);
fclose($my_file);
unlink($temp_file);
这是下载代码:
$key = substr(md5('some_salt' . $this->password, true) . md5($this->password . 'more_salt', true), 0, 24);
$opts = array('iv' => base64_decode($this->iv), 'key' => $key);
$encrypted_file = fopen($this->file_path, 'rb');
stream_filter_append($encrypted_file, 'mdecrypt.rijndael_128', STREAM_FILTER_READ, $opts);
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
header('Content-Type: "application/octet-stream"');
header("Content-Disposition: attachment; filename= $this->real_name");
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: public');
header("Content-Length: " . filesize($this->file_path));
} else {
header('Content-Type: "application/octet-stream"');
header("Content-Disposition: attachment; filename= $this->real_name");
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
header("Content-Length: " . filesize($this->file_path));
}
fpassthru($encrypted_file);
fclose($encrypted_file);
$this->add2Log();