I am trying to download the file with http header but it will not download completly. The uploaded file size is 1 MB and the file download with my code 336 byte size only.
I am trying with below code
$attachment_location= "filename";
$filePath= "$siteURL/foldername/filename";
$file_content = file_get_contents($filePath);
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$attachment_location\"");
echo $file_content;
I have solved this issue by just changing the filepath remove the path with SiteURL and provide the path from folder. Check code below,
$attachment_location= "filename";
$filePath= "foldername/filename";
$file_content = file_get_contents($filePath);
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$attachment_location\"");
echo $file_content;
Related
I'm trying to download a .rar file from a cloud (for sake of simplicity I'm using my google drive storage), the file is downloading perfectly, but once i want to open the .rar file , it says that "the archive is either unknown format or damaged" , tried all methods even cURL ,but it didnt want to work,
Im just wondering what I'm missing in my code, thank you
<?php
$filename = 'stu.rar';
if ( file_put_contents( $filename,file_get_contents("mygoogleDriveLink/search?q=stu.rar"))) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
//header('Content-Length: '.filesize($filename));
readfile($filename);
//print_r("this is ".$id);
exit();
}
else{
echo "err";
}
You may use file_put_contents to save the file to the server first, before you stream it to your browser.
if you do not need to stream to user's web browser, then you may remove the codes from start streaming to end streaming.
Please try the following:
<?php
// Initialize a file URL to the variable
$url = 'http://www.xxxxxxxxxxx.com/xxxxx.rar';
// Use basename() function to return the base name of file
$file_name = basename($url);
// Use file_get_contents() function to get the file
// from url and use file_put_contents() function to
// save the file by using base name
if(file_put_contents( $file_name,file_get_contents($url))) {
// File successfully saved in the server
// start streaming . The following is to stream and save to browser
$file_name = $file_name;
$file_url = $file_name;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
readfile($file_url);
exit;
/// end streaming
}
else {
echo "File downloading failed.";
}
?>
I have seen many examples but none of them is resolving my issue.
I generated cvs file in ajax post request ( I am not changing window.location.href). I want this file to auto download just like what happens after changing window.location.href. Currently i don't know solution.Kindly help me here is my code
$file_name="temp_".time().".csv";
$new_csv = fopen($file_name, 'w');
fputcsv($new_csv, $csv_data);
fclose($new_csv);
header("Content-type: application/csv; charset=utf-8");
header("Content-disposition: attachment; filename =\"" .$file_name. "\"");
readfile($file_name);
unlink($file_name);
exit;
$this->setLayout(false);
return sfView::NONE;
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);
I've made a page where you can go and write text in a "textarea" and then when you click download you download that file as a .txt file. I've done the same thing to some other extensions and that is working fine. But it won't work with .PDF, nothing I read works. Here is the snippet I use for the .PDF downloading:
<?php
if($fileFormat == ".pdf"){
$content = $_POST['text'];
$name = stripslashes($_POST['name']);
$nameAndExt = $name.".pdf";
print strip_tags($content);
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="'.$nameAndExt.'"');
header('Content-Transfer-Encoding: binary ');
}
?>
I'm grateful for any answear, thanks!
// hold the filename for use elsewhere so you don't have to append .pdf every time
$filename = "$id.pdf";
// create the file
$pdf->output( $filename );
// set up the headers
header("Content-Description: File Transfer");
header("Content-disposition: attachment; filename={$filename}");
header("Content-Type: application/pdf");
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($file));
// push the buffer to the client and exit
ob_clean();
flush();
// read the file and push to the output stream
readfile( $filename );
// remove the file from the filesystem
unlink( $filename );
exit();
I would recommend a class like TCPDF, see http://www.tcpdf.org/. I used it couple of times and it's quite nice (open source).
I have problem in download image from url,
$url = 'http://example.com/image.php';
$img = '/my/folder/flower.gif';
file_put_contents($img, file_get_contents($url));
in this code file is saving in my server folder .but i needed without saving in my server it needs to be download to user.
set the correct header and echo it out instead of file_put_contents
$url = 'http://example.com/image.php';
header('Content-Disposition: attachment; filename:image.jpg');
echo file_get_contents($url);
Use application/octet-stream instead of image/jpg:
If [the Content-Disposition] header is used in a response with the application/octet-stream content-type, the implied suggestion is that the user agent should not display the response, but directly enter a `save response as...' dialog.
— RFC 2616 – 19.5.1 Content-Disposition
EDIT
function forceDownloadQR($url, $width = 150, $height = 150) {
$url = urlencode($url);
$image = 'http://chart.apis.google.com/chart?chs='.$width.'x'.$height.'&cht=qr&chl='.$url;
$file = file_get_contents($image);
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=qrcode.png");
header("Cache-Control: public");
header("Content-length: " . strlen($file)); // tells file size
header("Pragma: no-cache");
echo $file;
die;
}
forceDownloadQR('http://google.com');
if you want to download the file onto your webserver(and save it), just use copy()
copy($url, 'myfile.png');