如何动态下载文件,即扩展名为.pec的文件已上传到服务器,现在我必须下载相同的文件而不提供带有.pec扩展名的文件名。
在第4行我已经给出了文件名isifresume.pdf,我已将其上传到服务器并获取加密到wasifresume.pec扩展名,现在我必须在第4行动态下载文件相同的文件
$file = "wasifresume.pec";
没有给出文件名称isifresume
是否有任何代码可以像* .pec那样下载同一个文件,因为我想动态制作它。
<<?php
session_start();
ob_start();
$file = "wasifresume.pec";
echo $_SESSION["file_name"]."the name";
ob_start();
// put some business here
if(file_exists($file))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
readfile($file);
exit;
}
else
echo "file not found";
?>
请参阅upload.php
<?php
session_start();
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
//$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$uploadOk = 1;
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 50000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
echo $_FILES["fileToUpload"]["tmp_name"];
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
// echo $_FILES["fileToUpload"]["name"];
$_SESSION["file_name"]=$_FILES["fileToUpload"]["name"];
// echo $ _SESSION [“file_name”];
exec('/var/www/html/CryptCmdline http://*.*.*.*/uploads/'. $_FILES["fileToUpload"]["name"],$output);
// $output=shell_exec("dr.bs");
// print_r($output);
include('/var/www/html/down.php');
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
答案 0 :(得分:0)
仅在PHP代码中设置content-type是不够的。您需要将PEC添加到Web服务器的MIME类型中,以便了解它正在传输的文件类型
答案 1 :(得分:0)
在.htacces文件中
AddType application/octet-stream .pec
这将使其自动下载
或:
<FilesMatch ".pec">
ForceType application/octet-stream
Header set Content-Disposition attachment
Header set Content-Description "File Transfer"
Header set Content-Transfer-Encoding "binary"
</FilesMatch>