I need to download files from remote server with my PHP script. The problem is that the links I recieve look like "example.com?download=12345". So I want to save the file with correct extension and (at best) preserve it's original filename. How do I do this?
Thank you all!
The Content-Disposition header in the HTTP response, if it exists, will contain the filename the other server wants the file to have.
#EricLaw (of the IE Team) has posted a detailed analysis of this on the IEBlog last November. While his post is focused on IE, the basics hold for most browsers:
use a Content-Disposition header (cleanest, standard-compliant, allows you to specify the filename; can be combined with either of the following)
don't use a query-string (not always practical - but possible with URL rewriting)
"Add a bogus querystring parameter at the end with the desired extension" (a bit of a hack, but works)
However, the source server is not obliged to do any of this; the most likely option would be the Content-Disposition header. If that is not present, you are back to square 1 (although you could guesstimate the file type from its content).
If you are using curl, the response header should return the actual filename. The header for file downloads looks like:
'Content-Disposition: attachment; filename="downloaded.pdf"'
Related
I have a PHP application that generates a CSV file and redirect the user to a static page linking to the file, just the example below :
https://www.example.com/public_html/static/temp/myfile.csv
Problem is, Chrome is opening the file instead of saving it. I need Chrome to save this file, as it would do with any other file like a zip or mp3, for instance.
Here is what I tried :
header('location:https://www.example.com/public_html/static/temp/myfile.csv');
header('Content-Disposition: attachment; filename=myfile.csv');
But no luck, Chrome keeps showing the myfile.csv contents instead of downloading it.
Any ideas ?
Thanks
Your argumentation in the comments has one never-ending misunderstanding: the Location header instructs any client to perform a new request to the given URI. With that the current request is over. Headers from the current request (i.e. Content-Disposition) aren't magically carried over to the next request.
In other words: your "static page linking to the file, just the example below" must send your wanted header.
Ultimately I'm sure it's not a Chrome problem either, but affects all internet browsers, as they easily detect the CSV data as text, hence being able to render/display that data instead of only being able to save it to a file.
With html5 you can set the "download" attr in an element.
Download it!
Source : http://updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-download
After struggling with this issue for some days, the only real solution i got is to ZIP the file and then redirecting to the ZIP file instead of the CSV. By doing this, Chrome will download the ZIP file instead of opening it :
header('location:https://www.example.com/public_html/static/temp/myfile.csv.zip');
I am dealing with images on the web that come without a file extension, like this:
https://static1.squarespace.com/static/52a74d9ae4b0253945d2aee9/t/52ed63b1e4b04368c021b921/1463088116168/?format=500w
Images like these can be found, e.g., on websites made with squarespace, like this demo: https://bedford-demo.squarespace.com/
I'm trying to download these images and store them on my server, using PHP. But how can I find out the actual URL of those images? How does this work? And how can I tell the filetype of this image? What is this sorcery?
Any hints are appreciated!
Quick answer:
To find out the Content-Type returned for any URL, look at this answer:
Get Content-Type of requested URL in PHP
Why you need the Content-Type:
Just like how not every webpage on the internet has an URL that ends with .html, images are not required to have an "extension" in their URL either.
What determines whether the browser will treat the data retrieved from the URL as an image is the Content-Type header in the HTTP response.
The URL you posted returns the following HTTP headers:
For HTML documents the Content-Type is text/html. You can inspect the headers as you browse by opening the Network tab of the developer console in your browser. Look for the "response headers".
You can get the mime type of the file with getimagesize:
<?php
$size = getimagesize("https://static1.squarespace.com/static/52a74d9ae4b0253945d2aee9".
"/t/52ed63b1e4b04368c021b921/1463088116168/?format=500w");
print_r($size["mime"]);
?>
Prints:
image/jpeg
I have a page in my site, displaying some images that are produced my PHP. When I right click on an image and click Save Image As I get as default name the name of the php file used for generating the image.
This is for example the html for the image :
<img src="picture_generator.php?image_id=5&extension=.png">
and the name I get is:
picture_generator.php.png
Is there a way to set this name to a default one?
Thanks in advance
You can provide it in the Content-Disposition HTTP header:
header('Content-Type: image/png');
header('Content-Disposition: inline; filename="' . $filename . '"');
However, some browsers (namely Internet Explorer) are likely to ignore this header. The most bullet-proof solution is to forge the URL and make the browser believe it's downloading a static file like /images/5/foo.png while the actual path behind the scenes is /picture_generator.php?image_id=5&extension=.png. This can be accomplished by some web server modules like Apache's mod_rewrite.
You can try to set the file name using the HTTP headers but not all browsers respect that.
The simplest trick is to extend the URL so that the last part contains the desired file name:
<img src="picture_generator.php/desiredfilename.jpg?image_id=5&extension=.png&name=desiredfilename.jpg">
Note I also added the file name at the end of the query string (the name doesn't really matter) as some browsers use that part.
Depending on your server configuration this will immediately work without any special configuration (no mod_rewrite or anything like that). You can check if it works on your server by simply appending "/foo" to any PHP-URL on your site. If you see the output of your PHP, all is good. If you see a 404 error then your server configuration can't deal with such URLs.
In your picture_generator.php file you need to add a header with the name. such as
header("Content-Disposition: attachment; filename=\"myfile.png\"");
I need to provide a static url to a client, eg. http://domain.com/video.mp4. However this URL needs to provide a random video from a selection of 5 videos each time it is accessed.
Is this possible using PHP and mod_rewrite? Or some other way?
Thanks
You may be able to provide a url to a PHP script (http://domain.com/video.php), which then sets its content-type header to the type of a video and then randomly reads out a file using readfile.
I don't think you need to use mod_rewrite in this case.
header('Content-type: video/mp4'); // I don't know the correct MIME type
$files = array('vid1.mp4', 'vid2.mp4', 'vid3.mp4');
readfile($files[array_rand($files)]);
Does your client require the url to end in .mp4?
If not you could indeed use a mod_rewrite by selecting a random number from, say, 1 to 5
Sorry - looks like this is a duplicate of: How to get the content-type of a file in PHP?
A few times I've run into situations where I'd like to be able to include a file using PHP, and depending on the included filetype, output the appropriate headers. In the past I've just done this manually by switch/casing the extension type with the appropriate content-type headers.
However what I'm wondering now is if there's a function like
get_header($filename)
or maybe
get_contenttype($extension)
For example if I wanted to route all requests for media through a php file, I could use that function to output the correct headers.
finfo_file might help. There some more info on this question.