I'm trying to make a distant client able to download an existing file(csv) named "source.csv" in my local machine using php.
After downloading the file the client should have a file named sourceCopy.csvwith the same content as the source file.
Here's what I've tried :
$file_url = 'source.csv';
header('Content-Type: application/csv');
header("Content-Transfer-Encoding: Binary");
header('Content-disposition: attachment; filename="sourceCopy.csv"');
readfile($file_url);
The file is downloaded but it doesn't match the source file !
Related
I am trying to download pdf file from my server. while downloading file it replaces forward slash(/) with underscore and file path become like this(127.0.0.1_file_upload_demoform1.pdf), because of that not able to download file.
here is code.
header('Content-Type: application/pdf');
$download_path = "127.0.0.1/file_upload/demoform1.pdf";
header('Content-Disposition: attachment; filename='.$download_path);
readfile('downloads/'.$download_path);
Can anyone suggest what is issue in it.
I got the answer.
I was trying to download the pdf file from another server. which does not allow me directly download from that location.
Here is my solution.
$content = file_get_contents('http://127.0.0.1/file_upload/demoform1.pdf');
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename=demoform1.pdf');
echo $content;
I made a script which creates .jpg file and saves it to server, then I want to make it downloadable by user but when I open this image which I downloaded I get error about corrupted file, although this image is not corrupted because I can open it from server (manually with image viewer). These are my headers:
header('Content-Description: File Transfer');
header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=image.jpg");
readfile('image.jpg');
I also tried to change Content-type to image/jpeg but I get same results
I have a code which force download a pdf file. Code below
$file_name = 'Bv_Ebook.pdf';
$file_url = 'http://' .$_SERVER['HTTP_HOST'].'/sites/default/files/'. $file_name;
header('Content-Type: text/html');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
readfile($file_url);
This code works on our beta server and the pdf is downloaded and can be opened.
On our live server same code downloads the pdf but pdf file cannot be opened, it says:
Unable to open document
File type plain text document (text/plain) is not supported
I have gone though force download pdf and apache headers setting but nothing seems to work.
Any idea what could be the issue?
Check file permission using is_readable() to check if the file is accessible or not. If accessible then check below :
Can you please try to modify your headers to the following:
//We'll be outputting a PDF.
header('Content-type: application/pdf');
//PDF name.
header('Content-Disposition: attachment; filename="'.$file_name.'"');
Then do a file read or what ever, in my case I have used file_get_contents(file_path) + echo to display the content and worked as a charm.
Im using X-Sendfile to send a file instead of readfile. The script processing this, is named download.php and contains this:
$video_file = '/path/to/file/file.mp4';
header('X-Sendfile: '.$video_file);
header('Content-Type: video/mp4');
header('Content-Disposition: attachment; file=\"file.mp4\"');
exit();
But problem is that the downloaded file is always named "download.php" (155Mb), and i would like to download it named as file.mp4. Ive tried several things like:
header('Content-Disposition: attachment; file="file.mp4"');
header('Content-Disposition: attachment; file=file.mp4');
And all other posibilities, but still downloading the file as download.php.
My htaccess file contains: XSendFile On
You are sending an incorrect header. The filename property is called filename, not name:
header('Content-Disposition: attachment; filename="file.mp4"');
See RFC2616 for a detailed description of the header.
I'm trying to create a file download page. This page when requested should prompt the user to download a file. Here is the source code for the page:
<?php
header('Content-disposition: attachment; filename=zip.zip')
header('Content-type: application/zip');
readfile('zip.zip');
?>
This works ok.
The problems starts when I want to move the file zip.zip from the folder where this script is in. I tried using relative and absolute URLs but I always get strange results,
the browser still prompts for file download but somehow it's just an odd file name converted from the URI I supplied somthing like ".._.._files_zip.zip instead of ../../files/zip.zip.
Any suggestions why this happens?
Thanks
Use basename to get just the file name:
$file = '../../files/zip.zip';
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Type: application/zip');
readfile($file);