我尝试了不同的代码从php下载文件。
fileread()提出问题所以,现在甚至都不是一个选项。
我正在尝试使用fread(),但它给出的问题是,当你下载文件时,该文件永远不会有效。我下载了破碎的文件。例如,如果我下载图像文件,它将在picasa中给出错误无效图像。 (如下所示)
以下是我正在使用的代码:
<?php
require '../php/db.php'; // Database Connection (No issue in this)
ob_start();
set_time_limit(0); // To set script time to infinity.
ini_set('memory_limit', '512M');
if(isset($_GET['file_id'])&&!empty($_GET['file_id'])) download_file($_GET['file_id']); // calling a function download_file given below
else die("There was an error in downloading file. Please try again later.");
function download_file($id){
global $con; // For database connection
$id = mysqli_real_escape_string($con,htmlentities($id));
$file="SELECT file_name,file_title,file_size,down FROM files WHERE file_id= $id"; // Taking file info from database
$result = mysqli_query($con,$file);
if($result) echo "Okay!";
$row = mysqli_fetch_assoc($result);
$name = $row['file_name']; // File is stored with this name in same dir
$title = $row['file_title'];
$ext = ext($name);
$down = $row['down']; // Tells number of downloads
$newname = $title.'.'.$ext; // Changing file name while downloading.
$down++;
if(is_file($name)) {
$update_down = "UPDATE files SET down = $down WHERE file_id = '$id'";
$update_down_result = mysqli_query($con,$update_down); // Increment number of downloads
download_by_fread($name,$newname); // Calling a function
exit;
}else header("Location: ../index.php?msg=Sorry!+File+could+not+found!");
}
function download_by_fread($name,$newname){
if($fd=fopen($name, "rb")){
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-type: ".tell_file_type($name));
header("Content-Disposition: attachment; filename=\"".$newname."\"");
header("Content-length: ".filesize($name));
header("Cache-control: public"); //use this to open files directly
while(!feof($fd)) {
print(fread($fd, 4096));
ob_flush();
flush();
}
}else header("Location: ../index.php?msg=Sorry!+File+could+not+found!");
fclose($fd);
}
function ext($name){
$rut = strrev($name);
$erut = explode('.', $rut);
return strrev($erut[0]);
}
function tell_file_type($file_name){
$rut = strrev($file_name);
$erut = explode('.', $rut);
$ext = strrev($erut[0]);
switch($ext){
case 'txt': return 'text/plain'; break;
case 'rar':
case 'zip': return 'application/x-compressed'; break;
case 'gif': return 'image/gif'; break;
case 'jpg':
case 'jpeg': return 'image/jpeg'; break;
case 'bmp': return 'image/bmp'; break;
case 'png': return 'image/png'; break;
case 'pdf': return 'application/pdf'; break;
case 'mp3': return 'audio/mpeg3'; break;
case 'mp4': return 'video/mp4'; break;
case 'mkv': return 'video/mkv'; break;
case 'mpg': return 'video/mpeg'; break;
case 'avi': return 'video/avi'; break;
case 'wav': return 'audio/wav'; break;
case 'doc':
case 'docx': return 'application/msword'; break;
case 'pps':
case 'ppt':
case 'pptx': return 'application/mspowerpoint'; break;
case 'xls':
case 'xlsx': return 'application/excel'; break;
case 'exe': return 'application/octet-stream'; break;
case 'swf': return 'application/x-shockwave-flash'; break;
}
}
?>
请帮我在此代码中找到错误。
谢谢。
解答: 我发现了错误。我在回答“好的!”在mysql_query之后。检查查询是否成功。我们可以这样做,否则它将被添加到文件中。和文件将无效。
答案 0 :(得分:1)
从PHP文件的末尾删除?>
,并确保开头<?php
之前没有空格。
可能发生的是PHP正在向图像的输出发送“”空白字符。如果PHP括号外有任何内容,这会导致文件损坏,因为这些文件会自动作为内容发送到浏览器。