I have a script that creates a vCard for members of staff when the 'Add Contact' button is clicked. I have this vCard in a variable, but I'm not really sure what to do with it next.
I take it that my frist step should be to save this file on the server?
I'd like to just have a box pop up and allow people to download and save the vCard, so if the step about is not necessary I'd like to just skip it.
Any pointers here would be appriciated.
Thanks.
If you want a File Save dialog to pop up when someone requests the export URL, you have to use
header("Content-type:text/vcard; charset=utf-8");
header("Content-Disposition: attachment; filename=vcardexport.vcf");
echo $vCardData;
So No, you dont have to save it as a file on the server first. You can serve it from the variable. Note that you can use this approach for any other data as long as you specify the right MIME Type for Content-Type.
Also see https://en.wikipedia.org/wiki/VCard and https://www.ietf.org/rfc/rfc2183.txt
If you have your vcard in a variable, then you can easily force it as a download onto the client with this code:
<?php
header('Content-type: text/vcard');
header('Content-disposition: attachment;filename=vcard.vcf');
echo $vcard_variable;
?>
Try look at the content-disposition header :)
It can force a file download at the client :)
You can just output the vCard from PHP, setting the proper content-type with a response header. This should force a download on the user's browser. I've googled it and found this example.
If you have the file on the server you can just have a link on the button that points to the file
<img src="button.jpg">
or are you looking for a different delivery method?
Related
So there is this webpage where this guy managed to return raw images from the server
just by typing the id as a parameter.
http://photos.iitm.ac.in/byid.php?id=008576
even if you right click the image and open in a new tab, only the php gets opened.
does anyone know how he managed to do this?
I need this kind of functionality for profile pics
It's pretty simple. You just have to send the data with the right Content-type. So if you want to show a gif-image just uste:
Header("Content-Type: image/gif");
and then send out the image data.
You can do like this :
Suppose example URL is :
http://www.example.com/getImage.php?id=20
So, on getImage.php page get user image information from database and provide the image source :
Code should be like this:
$userImageName="testImage.gif"; //FROM DATABASE FOR id=20, Just for example
ob_clean(); //CLEAN THE OUTPUT BUFFER
header("Content-Type: image/gif"); //SET PAGE HEADER FOR IMAGE
echo file_get_contents('imageDirectory/'.$userImageName);
die;
Although it's not a direct answer to the asked question, since it was already answered by other people, I think it's nonetheless worth mentioning.
You can also control the file name that appears when you try to save the picture! To do that, use following header:
header('Content-Disposition', 'inline;filename=my_image_title.jpg');
so that your script looks like this:
header('Content-Disposition: inline;filename=my_image_title.jpg');
header('Content-Type: image/jpeg');
readfile($pathToImage);
//or echo $imageContent
Just make sure that you didn't accidentally output anything before making first call to header(), otherwise you might get "headers already sent" error.
What i am trying to do is provide a way for an Xls file generated on the client side in js to be downloaded. So I have the xls in a string in js and need to give the user a way to download it and open it in excel.
As i understand the only way to do this is to do it on the server via the content type, so I have tried to provide a php that does a file relay... Here is the php
<?php
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"my-data.csv\"");
$data=stripcslashes($_REQUEST['csv_text']);
echo $data;
?>
A request can end up being rather long so for example i might have this request... (actually shortened greatly).
I am not good with php, can anyone suggest a better way to modify this relay script (or better way entirely) to accomplish this?
http://myserver.com/ExcelRelay.php?csv_text=Id%09City%09Phone%09Address%201%09Address%202%09State%09Type%09Employees%09Revenue%09Leed%09Established%09Comments%09Country%09Postal%20Code%09Territory%0A3%09Greensboro%096538227668%09%0978%20Rocky%20Second%20St.%09New%20Jersey%09Remote%09%090%091%09Sun%20Aug%2009%201964%2000%3A00%3A00%20GMT-0400%20%28Eastern%20Daylight%20Time%29%09%22Et%20quad%20estis%20vobis%20homo%2C%20si%20nomen%20transit.%20%0A%20Sed%20quad%20estis%20vobis%20homo%2C%20si%20quad%20ut%20novum%20vobis
Thanks For the Response, The final script was
<?php header("Content-type: application/octet-stream");
header("Content-Disposition: attachment;filename=\"".$_POST['filename']."\"");
echo $_POST['data'];
?>
One word: POST.
Request Url Too Long is a client side error, and one that is (AFAIK) exclusive to IE, these days. If you want to send the data to the server and have it sent back to you as a file, you will have to send the data in the body of the request.
See here for more information.
well I'm just wondering how I can get an mp3 download to start instantly, as oppose to it simply starting to play in the browser when you directly go to it.
Preferably using php headers.
So essentially when you click the file, I want a download box to appear saving save etc. Right now it just opens and starts playing in the browser.
Thanks
You'll need to create a PHP file that "redirects" to the MP3 file, and point your links to that PHP file.
Code as below:
<?php
header('Content-type: audio/mpeg');
header('Content-Disposition: attachment; filename="fileName.mp3"');
readfile('originalFile.mp3');
?>
Note: The line that sets the Content-Disposition header is the critical one.
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\"");
Basically I wrote a script that generates a xml file based on user input. After the file is generated a download link appears like so:
Download File
But when clicked it opens the xml in the browser, I want it to start downloading when the link it clicked instead. Is there any way to achieve that?
Yeah, there is. It does require specifying some headers. Exactly how it works depends on what language you're using, but here's an example using php, taken off of php.net:
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
?>
Basically, first we tell the client what type of file we're sending, then we tell the client that what we're sending is an attachment, and it's name, instead of it being a page to display, and then finally we print/read the file to the output.
Given that you're already using php to generate the xml file, I would suggest adding the header commands above to the code that generates the xml file, and see if that does the trick.
If you happen to be using Apache for your web server, and you always want to force downloading of XML files, there is a more efficient way to do what #chigley suggested. Just add the following to a .htaccess file.
<Files *.xml>
ForceType application/xml
Header set Content-Disposition attachment
</Files>
What happens when a browser sees a link is not dependent on the link, but rather on the target of the link. Your web server should send the appropriate header: Content-Disposition: attachment;filename="file.xml" to tell the browser that it should prompt to save the file instead of displaying it.
It depends on what the client computer does with XML files. If you doubleclick on a XML file, it will open in your browser probably.
download.php:
header('Content-Type: text/xml');
header('Content-Disposition: attachment; filename="file.xml"');
readfile('/path/to/file.xml');
HTML:
Download