PHP Download file with header - php

I know it's something easy, but I can't seem to find the solution.
I want to download a pdf from a folder. The pdf is in a folder named forms.
My script is in a different folder.
When I try to download the file with the following code:
header('Content-Disposition: attachment; filename="forms/form1976.pdf"');
The filename becomes: forms-form1976.pdf. That's not right, the filename should be: form1976.pdf. How do I enter the the correct folder first?

You should do something like this
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename=example.pdf');
readfile("/path/to/yourfile.pdf");

Related

Force download php returns okb if there are spaces in the file name

I am trying to force download an mp3 file from a server using PHP.
I am using absolute paths for example my-domain.../my-file.mp3
This is the code that I am using to download the file.
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=". $_GET['name']);
$path = urldecode($_GET['path']);
if(isset($path))readfile($path);
If the file path has some spaces in it, it downloads a 0kb file and I have no idea why, I need help with this, so if the file path is my-domain.../my-file.mp3 it works great. If it is like this my-domain.../my file.mp3 it does download the file but it has 0kb.
I have tried everything I could think of but I can't figure it out.
Thank you.
If the filename contains spaces, you should output the name in qoutes like this:
header('Content-Disposition: attachment; filename="'. $_GET['name'] . '"');

how to download a file from file_get_contents?

I want to get a file via file_get_contents to copy and rename it and then trigger the download. In other words, a user click a link to a controller, the controller does the business and then return the new file to download.
All fine, the only thing I can't do till now is rename and force donwload of the file.
You can change the headers and echo out the file:
// Download the file
header('Content-Disposition: attachment; filename="myfile.csv"');
header("Content-Type: text/csv");
header("Content-Length: " . filesize($outputName));
echo (file_get_contents($outputName));
unlink($outputName);

How to create a CSV file and save it to specific path on server?

I want to create a CSV file and save it to a specific path on the server. Here I used this code to create file, but it will directly open. I want to save it to a folder.
header('Content-Type: application/csv ');
header('Content-Disposition: attachment; filename=' . basename($filename));
What you want is fputcsv. That will save a file to the server.

Force download file that's not placed on the server

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.

PHP force download, Not working with variables

When
header('Content-disposition: attachment; filename=1330554893-COVER.jpg');
header('Content-type: jpeg');
readfile('watermarked/1330554893-COVER.jpg');
Is run in a file for example "testdownload.php" It downloads the image
"watermarked/1330554893-COVER.jpg"
and names it
"1330554893-COVER.jpg"
But when I try make the code dynamic to download different files.
header("Content-disposition: attachment; filename={$newFileName}");
header("Content-type: jpeg");
readfile("{$findFile}");
where
$newFileName = "1330554893-COVER.jpg" and $findFile = "watermarked/1330554893-COVER.jpg"
It downloads an image "1330554893-COVER.jpg" but it cannot be opened and I get an error "Windows Photo Viewer can't open this picture because eaither Photo Viewer doesn't support this file format"
Thanks for helping :)
Allrite then, don't use readfile(), try echo file_get_contents after the headers!

Categories