Php 文件在下载时损坏

Php 文件在下载时损坏,php,file,mime,Php,File,Mime,我从一个网站上下载了这段代码。 确实实现了这段代码,但它只能下载,但当我试图查看该文件时,它显示了文件中的错误。 我确实下载了各种文件格式,如pdf、docx、doc,但它总是显示错误。 我已经将上传的内容或文件存储在数据库中。它不在目录中。 请给我一些建议 错误是- 警告:fopen(files/JAVAPROGRAMS.doc)[function.fopen]:无法打开流:否 中的此类文件或目录 C:\wamp\www\pages\candidate\download.php第75行错误-无

我从一个网站上下载了这段代码。 确实实现了这段代码,但它只能下载,但当我试图查看该文件时,它显示了文件中的错误。 我确实下载了各种文件格式,如pdf、docx、doc,但它总是显示错误。 我已经将上传的内容或文件存储在数据库中。它不在目录中。 请给我一些建议

错误是-

警告:fopen(files/JAVAPROGRAMS.doc)[function.fopen]:无法打开流:否 中的此类文件或目录 C:\wamp\www\pages\candidate\download.php第75行错误-无法打开文件

mysql\u connect('localhost','root','')或die(mysql\u error());
//回声“连接”;
mysql_选择_db('talent')或死亡(mysql_error());
函数输出_文件($file,$name,$mime_类型=“”)
{
$size=filesize($file);
$name=rawurldecode($name);
$known\u mime\u types=数组(
“htm”=>“文本/html”,
“exe”=>“应用程序/八位字节流”,
“zip”=>“应用程序/zip”,
“doc”=>“应用程序/msword”,
“jpg”=>“图像/jpg”,
“php”=>“文本/普通”,
“xls”=>“应用程序/vnd.ms excel”,
“ppt”=>“应用程序/vnd.ms powerpoint”,
“gif”=>“图像/gif”,
“pdf”=>“应用程序/pdf”,
“txt”=>“文本/普通”,
“html”=>“文本/html”,
“png”=>“图像/png”,
“jpeg”=>“图像/jpg”
);
如果($mime_type==''){
$file_extension=strtolower(substr(strrchr($file,“.”),1));
如果(数组\密钥\存在($file\扩展名,$known\ mime\类型)){
$mime_type=$known_mime_type[$file_extension];
} 
其他的
{
$mime_type=“应用程序/强制下载”;
};
};
//关闭输出缓冲以减少cpu使用
@ob_end_clean();
//IE需要,否则可能忽略内容处理
if(ini_get('zlib.output_compression'))
ini_集('zlib.output_compression','Off');
标题(“内容类型:”.$mime\U类型);
标题('Content-Disposition:attachment;filename=“.”.$name.'”);
标题(“内容传输编码:二进制”);
标题('Accept-Ranges:bytes');
//多部分下载和下载恢复支持
如果(isset($\u服务器['HTTP\u范围]]))
{
列表($a,$range)=分解(“=”,$\u服务器['HTTP\u range',2);
列表($range)=分解(“,”,$range,2);
列表($range,$range_end)=分解(“-”,$range);
$range=intval($range);
如果(!$range\u end)
{
$range_end=$size-1;
} 
其他的
{
$range\u end=intval($range\u end);
}
$new\u length=$range\u end-$range+1;
标题(“HTTP/1.1 206部分内容”);
标题(“内容长度:$new_长度”);
标题(“内容范围:字节$Range-$Range_end/$size”);
} 
其他的
{
$new_length=$size;
标题(“内容长度:.$size”);
}
/*将输出文件本身*/
$chunksize=1*(1024*1024);//您可能需要更改此值
$bytes\u send=0;
如果($file=fopen($file,'r'))
{
如果(isset($\u服务器['HTTP\u范围]]))
fseek($file,$range);
当(!feof($file)&&(!connection_aborted())&&($bytes_send时,请尝试此操作

 output_file($file_path, ''.$_REQUEST['filename'].'', '');

试试这个。只要提到正确的文件路径。希望它对你有用

ignore_user_abort(true);
set_time_limit(0); // disable the time limit for this script

$path = "/YOUR_FILE_PATH/"; // change the path to fit your    websites document structure
$dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\].]|[\.]{2,})", '',$_GET['download_file']); // simple file name validation
$dl_file = filter_var($dl_file, FILTER_SANITIZE_URL); // Remove (more) invalid characters
$fullPath = $path.$dl_file;

if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
              case "pdf":
                          header("Content-type: application/pdf");
                          header("Content-Disposition: attachment;   filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a file download
    break;
    // add more headers for other content types here
    default;
    header("Content-type: application/octet-stream");
    header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
    break;
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
    $buffer = fread($fd, 2048);
    echo $buffer;
}
}
 fclose ($fd);
 exit;

此代码仅适用于文件系统上的文件,您需要重写此代码以使用SQL db。您发布的脚本从文件系统读取一个文件。错误表明它找不到该文件。您需要为脚本提供正确的路径。如果您已将该文件存储在数据库中,则需要提供代码以检索该文件。我在这里看不到。@AndriyStruk…你能建议我如何实现这一点吗…因为我所有的文件都在db中…我只需要下载并查看它。谢谢you@HoboSapiens..thank你…正如你所说,我需要给出正确的路径…我应该在哪里提到它。如果有任何链接或一些代码格式,我会从那里引用此文件下载代码和view it,请帮助我。很抱歉,我真的在问你,但我在过去的3个小时里一直在忙。所以最后发布在这里。@Madhuripaltane…这是我代码中最后一行我需要放这个的地方。是的。不是放一个参数“text/plain”,放为空白参数将下载doc文件。这是强制下载文件。可以吗e这种情况。@madhuripaltane..I使用了上面的代码..但它给出的相同错误无法打开流。只是一些修改和它的工作.fopen($_文件[“UploadFileName”][“tmp_名称”],“r'))并添加了->头(“内容长度:”.filesize($filePath));
ignore_user_abort(true);
set_time_limit(0); // disable the time limit for this script

$path = "/YOUR_FILE_PATH/"; // change the path to fit your    websites document structure
$dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\].]|[\.]{2,})", '',$_GET['download_file']); // simple file name validation
$dl_file = filter_var($dl_file, FILTER_SANITIZE_URL); // Remove (more) invalid characters
$fullPath = $path.$dl_file;

if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
              case "pdf":
                          header("Content-type: application/pdf");
                          header("Content-Disposition: attachment;   filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a file download
    break;
    // add more headers for other content types here
    default;
    header("Content-type: application/octet-stream");
    header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
    break;
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
    $buffer = fread($fd, 2048);
    echo $buffer;
}
}
 fclose ($fd);
 exit;