I am trying to download a remote pdf file in codeigniter which is password protected. Actually i have the username and password for the file, but i need to download the file directly without prompting by asking username and password for viewing it.I was trying with the below code in controller, but i am not sure that i tried correctly or not.
Actually, downloading is happening, but after opening the file its showing 'Failed to load PDF document'.
One more thing, i dont have a pre idea about the filename of the file to be downloaded, one file should be there corresponding to a cart id. So i am not sure whether the line header("Content-disposition: attachment; filename=tickets.pdf"); i used in below code is correct or not. I supposed it must be our custom file name for the file to be downloaded.
Below is the code in controller.
function file_download(){
$cart_id = $this->uri->segment(3);//Getting cart id from the url
$ticket_url=Base_url.'carts/'.$cart_id.'/tickets';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=tickets.pdf");
ob_clean(); flush();
readfile($ticket_url);
exit();
}
Thanks in advance!
Related
I'm trying to use readfile to download a file from the server.
The file definitely exists and when I use the URL in browser directly it downloads the correct file as predicted.
However when I do it with the following code, it download the file with the correct filename but it is completely blank in content, and zero bytyes.
I am wondering if this code is in fact incorrect and it is instead creating a new file, which of course would be blank.
function downloadFile($filename){
$downloadroot = 'http://my.url.co.uk/exports/'.$filename.'.csv';
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename='.$filename.'.csv');
header('Pragma: no-cache');
readfile("$downloadroot");
}
What would be the cause of this? Is there an alternative way to download files from a URL without having to create some sort of "save target as" link?
You could use file_get_contents() see http://php.net/file_get_contents for the full details.
Or if it's only csv's you're trying to read:
http://php.net/manual/en/function.fgetcsv.php
I'm trying to force download a pdf file that I'm generating. I don't need the pdf file to be actually saved on the server.
So when I generate my pdf file, I get the file content. I then encode it with base64. Now the problem is that I need to force download it. I've looked all over the web, but I haven't found any search results that tells me how to do this without the file actually being placed on the site.
I've tried the following code:
header("Content-type: application/pdf");
header("Content-disposition: attachment; filename=\"invoice.pdf\"");
header("Content-Length: ".filesize($pdffile));
readfile(base64_decode($pdffile));
But, it's giving me a corrupt pdf file, (1 kb). The actual file should be around 50kb.
Any ideas, as to what I can try?
readfile trying to output content from file, but you have only data string. Try this instead:
header("Content-type: application/pdf");
header("Content-disposition: attachment; filename=\"invoice.pdf\"");
echo base64_decode($pdffile);
I also suggest rename $pdffile to $pdfcontent for even better clarification.
I have a site which allows clients to download files that they have purchased.
A link is sent to the customer which directs them to a page which checks if the link is valid, and if it is, allows them to download their product.
Here is an example of the link: http://www.psptubestop.com/dl/2z129a2.php?reference=6d556bde201a2fe4079874168&password=535864380ee2bc0f57cced3fe&pid=402
A lot of customers are saying that when they download, the .zip extension is missing, and I can't find the problem, it works fine on my machine, except when I do "save as", but even then some people are having problems.
Is there any way to allow them to download the file, and keep the .zip extension even if they press "save as"?
This is the code I am using to redirect them to the download...
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$originalfilename");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
Thanks for any help.
The problem could be that some files have spaces, in that case with your current code only the first part is used so loosing the extension part, to prevent such error you have to enclose the filename in double quotes, like so:
header('Content-Disposition: attachment; filename="'.$originalfilename.'"');
I am willing to bet it has something to do with there being a space in the filename seeing as the filename in the headers is PSP_Tubes_PSP_Tubes_Lisa_Cree_The Gift_6932.zip and the file wants to be save as PSP_Tubes_PSP_Tubes_Lisa_Cree_The.
Add a dummy GET parameter that looks like a filename.
...&fname=/foo.zip
I made a form that allows user to upload a file (text documents). I’m using an unique (?) file name made from a combination of time() and the user id (only logged user can upload).
My problem is that the file cannot be accessed externally. That is, only the user who uploaded it or an admin can see it, while it can’t be reached while simply typing www.domain.com/uploads/file_name.txt
I know I can prevent the access to file through htaccess, but if I did understand it correctly, in that way I couldn’t open it even after I am logged in as admin (or as the user who sent the file).
I know I could open the file locally through php so I could show up the content through my admin panel, but that’s a pain since I could output only plain text files without problems. Also I could not download the file.
I could generate on the fly pdf or rtf versions in some cases, however that would quite a long way since I would need to elaborate the content in a complex way. And anyway, I would have no idea how to handle Word or OpenOffice files, which are likely to be the most common cases, and how to not loose formatting or other possible features.
Any ideas?
Why not display a download link for logged in users, like www.domain.com/download.php?file=... The code could look something like:
if( isset($_GET['file']) && user_is_logged_in() ) {
$file = DIR_SOME_WHERE .'/'. basename($_GET['file']);
if( file_exists($file) && user_has_file_access( $file ) ) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
}
I have a pictrures gallery on my server. The pictures are stored on diffrent external servers. On my server are placed the thumbnails only.
How I can make a button "save as" in php so that a user can download a big picture file which is from external servers. I need a simple php script which can do download a jpg file cross all browser agents and from diffrent external servers. The button will be implemented inside html code. The button is a regular link formated in css style.
So how to do it properly. Thanks.
I would like also that the path of file should be send as a variable parameter to php script somehow.
I am guessing you are trying to have the pictures be downloaded automatically (you want a dialog box to pop up prompting where to save the file).
There is a great tutorial on this site that uses the php header function to force download
Check it out: http://www.ryboe.com/tutorials/php-headers-force-download
I found some solution with following php script
<?PHP
// Define the path to file
$file = $_GET['file'];
$name = basename ($file);
if(!file)
{
// File doesn't exist, output error
die('file not found');
}
else
{
// Set headers
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$name");
header("Content-Type: image/jpg");
header("Content-Transfer-Encoding: binary");
// Read the file from disk
readfile($file);
}
?>
and I can send parameters like url address cross html code
download
The only problem is that it is not working with all servers. It's not woriking for exemple with that file
http://backalleypics.com/Pictures/Letter Je~Ju/Jessica Alba/Jessica Alba 230.jpg
I don't know what I need to do?
Remove the spaces from your file name:
change: http://backalleypics.com/Pictures/Letter Je~Ju/Jessica Alba/Jessica Alba 230.jpg
to: http://backalleypics.com/Pictures/Letter_Je~Ju/Jessica_Alba/Jessica_Alba_230.jpg