On my website i allow a user to download a biography someone else posted while filling out a form. The problem I am recieving is that when I open a downloaded "txt" file, it opens fine but, when its like "docx or accdb" file, I get error messages like
We're sorry. We can't open xxx because we found a problem with its contents.
Details>>
The file is corrupt and cannot be opened
and I believe the problem happens when I get the file from the user. Here is my code
Upload file
$bio_name = $_FILES['biography']['name'];
$bio_tmp = $_FILES['biography']['tmp_name'];
$bio_size = $_FILES['biography']['size'];
$bio_type = $_FILES['biography']['type'];
$ext = pathinfo($bio_name, PATHINFO_EXTENSION);
if($ext=="pdf"||$ext=="PDF"||$ext=="doc"||$ext=="DOC"||$ext=="docx"||$ext=="DOCX"
||$ext=="XLS"||$ext=="xls"||$ext=="XLSX"||$ext=="xlsx"||$ext=="xlsm"||$ext=="XLSM"){
$fp = fopen($bio_tmp, 'r');
$content = fread($fp, filesize($bio_tmp));
$content = addslashes($content);
fclose($fp);
}else{
echo "<script>
alert('ERROR: This file format is not supported');
window.location.href='form.php';
</script>";
}
Download file
$size = $formData['bio_size'];
$type = $formData['bio_type'];
$file = $formData['bio_filename'];
$content = $formData['bio_tmp'];
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$file");
ob_clean();
flush();
echo $content;
Related
This question already has answers here:
Chrome has "Failed to load PDF document" error message on inline PDFs
(7 answers)
Closed 5 years ago.
My code is working fine. My files are downloading also but when I open one file then it is not opening giving an error "Error
Failed to load PDF document."
<?php
$pno = $_GET['pno'];
$sql = "SELECT file FROM tenders WHERE Tno = $id";
$file = "data/" . $mysql_row['file '];
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename='downloaded.pdf'");
readfile($file);
?>
$file = "path_to_file";
$fp = fopen($file, "r") ;
header("Cache-Control: maxage=1");
header("Pragma: public");
header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=".$myFileName."");
header("Content-Description: PHP Generated Data");
header("Content-Transfer-Encoding: binary");
header('Content-Length:' . filesize($file));
ob_clean();
flush();
while (!feof($fp)) {
$buff = fread($fp, 1024);
print $buff;
}
exit;
I would recommend to always check if the file exists. If it doesn't readfile() would eventually put an error inside your pdf-file which may cause the problem. Try it like this:
$pno = $_GET['pno'];
$sql = "SELECT file FROM tenders WHERE Tno = $id";
$file = "data/" . $mysql_row['file '];
if(file_exists($file)){
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename='downloaded.pdf'");
readfile($file);
} else {
echo "File does not exist!";
}
Also there is no declaration of the $id variable. Could it be that $pno should be changed to $id?
I'm having an issue when trying to make a download script on my website. What ends up happening is that it only downloads between 160B to 310B instead of the whole 170MB that the file is. I'm not sure why it's happening, and if anyone can help that would be greatly appreciated. Also, I'm not sure if it's an issue with the code itself or that my host doesn't allow big files to be downloaded, as I get this message on net2ftp: "Files which are too big can't be downloaded, uploaded, copied, moved, searched, zipped, unzipped, viewed or edited; they can only be renamed, chmodded or deleted."
Here is my code:
ignore_user_abort(true);
set_time_limit(0);
$path = "/versions/";
$dl_file = $_GET['file'];
$fullPath = $path.$dl_file;
if ($fd = fopen($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
header("Content-length: $fsize");
header("Cache-control: private");
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose($fd);
exit;
I'm trying to create a temporary download link for download files.
my code is:
$file_temp_adrs = "temp/".md5(microtime());
mkdir($file_temp_adrs);
$file_temp_adr = $file_temp_adrs."/".$fileinfo['org_filename'];
$file_org_adr = "files/".$fileinfo['filename'];
copy($file_org_adr , $file_temp_adr);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime_type = finfo_file($finfo, $file_temp_adr);
finfo_close($finfo);
$name = basename($file_temp_adr);
$size = filesize($file_temp_adr);
header("Content-Disposition: attachment; filename=\"".$name."\"");
header("Content-Type: $mime_type");
header("Content-Length: $size");
header("Connection: close");
When i click on download button, the browser saves a file with true name and extention but the file size is 0KB that is not usable.
where is wrong?
I think you do not actually deliver the file content and you should output the file content using readfile:
readfile($file_temp_adr);
So here i'm trying to send to the client some files received in a array from javascript. My problem is that i cannot redefine the filename (since its a header). So for example, i wanna send 3 files to the client, the first one is sent, then i got a error for the 2 last. Does anyone have idea how to fix it? Here is my php code:
PHP
<?php
ignore_user_abort(true);
set_time_limit(0); // disable the time limit for this script
$path = "../temp/"; // change the path to fit your websites document structure
$data = escapeshellarg($_GET['File']);
$data = str_replace("\\","",$data); // remove useless characters inserted
$data = str_replace("\"","",$data); //
$dl_file = explode(",", $data);
header("Content-type: application/octet-stream");
header("Cache-control: private"); //use this to open files directly
header('Pragma: private');
for($i = 0; $i < count($dl_file); $i++)
{
$fullPath = $path.$dl_file[$i];
$path_parts = pathinfo($fullPath);
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"",true);
if ($fd = fopen ($fullPath, "r"))
{
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
ob_clean();
flush();
readfile($fullPath);
}
fclose ($fd);
}
exit;
?>
Here is the error i got in logs file of the server :
Warning: Cannot modify header information - headers already sent in (php file path)
My goal is to simply echo $_POST['imageVar']; back with content headers as a quick and dirty means to "export" an image from a flash application. I saw an example of this as follows, but it does not work with my php version/config:
if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{
// get bytearray
$jpg = $GLOBALS["HTTP_RAW_POST_DATA"];
// add headers for download dialog-box
header('Content-Type: image/jpeg');
header("Content-Disposition: attachment; filename=".$_GET['name']);
echo $jpg;
}
So as a work around I created a script that saves the image to the server, reads the image and echos it back, then deletes the image. I don't like this approach as I need to have a directory that is writable by the apache user (im on a shared server). Is there a way to accomplish what I am doing here without hte need to use the temp file?
<?
$fileName = basename( $_FILES['uploadedfile']['name']);
$tempFile = "uploads/" . $fileName;
$fileSize = $HTTP_POST_FILES['uploadedfile']['size'];
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $tempFile)) {
$fh = fopen($tempFile, 'r');
$fileContents = fread($fh, $fileSize);
header('Content-Type: image/jpeg');
header("Content-Disposition: attachment; filename=$fileName");
echo $fileContents;
fclose($fh);
unlink($tempFile);
} else {
echo "upload fail";
}
?>
Any input or ideas greatly appreciated! Thanks for looking
You can just use:
header('Content-Type: image/jpeg');
header("Content-Disposition: attachment; filename=$fileName")
readfile($_FILES['uploadedfile']['tmp_name']);
No need to move it if you're not going to keep it.
On a side note, regarding the first solution you were looking at, does an echo file_get_contents('php://input'); not work?
$s = file_get_contents('php://input');
// add headers for download dialog-box
header('Content-Type: image/jpeg');
header("Content-Disposition: attachment; filename=".$_GET['name']);
echo $s;
What about that?