How can I force a file download using php? - php

I am new to programming, just learning it at school, but i want my users download some excel files from my homepage. The following code isn't working, it only displays it in the browser and not forcing a download dialogue.
How can i solve this problem?
The link should be: http://myurl.com/download.php?fileid=1 or http://myurl.com/download.php?fileid=2 and so on.
<?php
switch ($_GET["fileid"]) {
case 0:
$file = "files/mon.xls";
break;
case 1:
$file = "files/uru2.xls";
break;
case 2:
$file = "files/oppo23.xls";
break;
}
readfile($file);
Thanks for your help!

You have to use header. Like you can read on the php manual, its for your file:
<?php
header('Content-type: application/xls');
header('Content-Disposition: attachment; filename="downloaded.xls"');
readfile($file);

There are a few ways to do this but the most simple way I have found is to just use header. You could do something like this.
header('Location: files/oppo23.xls');

Related

Show PDF without path in URL

Refer to Can an ASP.NET MVC controller return an Image?
, the answer suggest to return image file from controller in C#.
My question is:
Can I do the same thing or similar in PHP? What I want is to hide PDF path from URL.
Thanks
I think you are trying to hide the real local path of your pdf file using php. If this is the case, you can use something like this:
<?php
$localfilename = 'my_local_path/my_file.pdf';
header('Content-Type: application/pdf');
header("Content-Disposition: attachment; filename='download.pdf'");
readfile($localfilename);
?>
I hope this helps you. Greetings!

header("Content-Type: image/jpeg") not working

Having read through all the similar questions, no situation is like mine.
After cutting all the crap, my code is:
<?php
$file = 'Images\Bird1.jpg';
header('Content-Type: image/jpeg');
imagejpeg($file);
?>
But it dispays a blank page. The path\file does exist, I checked it with code like:
if (file_exists($file))
The file type is indeed image/jpeg, this I also checked in the more extensive version of my code. Please help?
imagejpeg($file);
This function requires a resource, not a filename. You are looking for
readfile($file);
http://php.net/manual/en/function.readfile.php

PHP Save an Image to a Server?

I'm having a stump with some PHP...
I have a Flash Application that sends an image (using as3corelib) to a PHP script that previews it in the browser, which works! But, I would actually like it to permanently save it the a server folder (uploads, etc.) instead of temporarily saving it. I can't find the right variable in the PHP that actually sends the image to a server so it could save it.
<?php
switch ($_POST["format"]) {
case 'jpg':
header('Content-Type: image/jpeg');
break;
case 'png':
header('Content-Type: image/png');
break;
}
if ($_POST['action'] == 'prompt') {
header("Content-Disposition: attachment; filename=" . $_POST['fileName']);
}
echo base64_decode($_POST["image"]);
?>
Here's an example of it: http://shmoggo.com/snapshot
JPEG, Open to Browser (but I would like it to SAVE to browser)
Any PHP guru help would be terrific, thanks a lot!
Aaron
If you have the filename, you can simply do
$newpath = "/folders/image.jpg";
$data = file_get_contents($_POST['fileName']);
file_put_contents($newpath, $data);
Rather then displaying it, save $_POST['image'] to the server, see File System

php with mp3 header will not load (update/fixed)

test.php code:
$fileloc = 'audio.mp3';
header('Content-type: audio/mpeg');
header("Content-disposition: inline; filename=$filename");
header('Content-Length:'.filesize($fileloc));
readfile($fileloc);
html code:
<iframe src="test.php"></iframe>
here is the updated code that is working.
thanks hafichuk
if anyone knows a way to do this with
<embed> or <object> rather than <iframe>
please share your code or send me a link.
Try changing your content type to audio/mpeg3.
Update 1:
Since it's downloading the file as an attachment, you could also try changing the content-disposition to header('Content-Disposition: inline; filename=audio.mp3');
The reference I found for this was for swf files in the embed tag, however I would imagine that you should do the same.
I know this is old, but I found a simple way to accomplish this, yet it might not be the prettiest, but
here it is!
test.php:
<?php
if (isset($_GET['file'])) {
echo file_get_contents('music.mp3');
exit;
}
<audio src="test.php?file" controls></audio>
You simply extract the file using file_get_contents and output it, using the php file as the sound file, pretty neat :P

send a file to client

I want to write a text file in the server through Php, and have the client to download that file.
How would i do that?
Essentially the client should be able to download the file from the server.
This is the best way to do it, supposing you don't want the user to see the real URL of the file.
<?php
$filename="download.txt";
header("Content-disposition: attachment;filename=$filename");
readfile($filename);
?>
Additionally, you could protect your files with mod_access.
In addition to the data already posted, there is a header you might want to try.
Its only a suggestion to how its meant to be handled, and the user agent can chose to ignore it, and simply display the file in the window if it knows how:
<?php
header('Content-Type: text/plain'); # its a text file
header('Content-Disposition: attachment'); # hit to trigger external mechanisms instead of inbuilt
See Rfc2183 for more on the Content-Disposition header.
PHP has a number of very simplistic, C-like functions for writing to files. Here is an easy example:
<?php
// first parameter is the filename
//second parameter is the modifier: r=read, w=write, a=append
$handle = fopen("logs/thisFile.txt", "w");
$myContent = "This is my awesome string!";
// actually write the file contents
fwrite($handle, $myContent);
// close the file pointer
fclose($handle);
?>
It's a very basic example, but you can find more references to this sort of operation here:
PHP fopen
If you set the content type to application/octet-stream, the browser will ALWAYS offer file as a download, and will never attempt to display it internally, no matter what type of file it is.
<?php
filename="download.txt";
header("Content-type: application/octet-stream");
header("Content-disposition: attachment;filename=$filename");
// output file content here
?>
Just post a link on the site to http://example.com/textfile.php
And in that PHP file you put the following code:
<?php
header('Content-Type: text/plain');
print "The output text";
?>
That way you can create the content dynamic (from a database)...
Try to Google to oter "Content-Type" if this one is not the one you are looking for.

Categories