Create Download link using PHP from the database - php

i wanna ask again,still have problems with my php code
i wanna ask how to create download link that looping from database, the file store in localhost in folder attach,anyone can help
my php code to show the attachment file store in database like this
<?php
if ($row['filename']==NULL)
{echo "no attachment"; }
else
{echo $row['filename']; }
?>
from that generated i want to create a download link which is stored in my localhost
my attachment like it contains only 2 columns file name and file size
Regards
Wahyu

Can you please explain your question more? Im not able to comment on questions yet, so I have to add an answer.
A general answer is:
In your database your store the filename of the file you want to be downloaded. Next, you generate a link with php and use the filename of the database.
After your edit/comment, something like this:
<?php
if ($row['filename']==NULL){
print "no attachment";
}else{
printf("<a href='yourhost.com/folder/%s'",$row['filename']);
}
?>

For making download link, you need to set appropriate headers.
For example
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename='.$filename);
To solve your problem, you can have a link like http://site.com/downloads.php?id=12456.
In the downloads.php page. you can check for the file name . and set the header in that page. Also its not necessary to have the content-type header.

Related

Export data to XML with sepa-xml-php

please I need help I'm trying to create a SEPA XML file so I downloaded the sepa-xml with coenter image description heremposer in my project then I make the same code in the documentation to test if work or not so when I enter image description hereadded the code and generate the file I get an empty file. but when I dump the variable I get a result .
I get this page
I referred here. Looks like you need to download the generated xml code.
[Git link][1].
Code will be like this.
header('Content-disposition: attachment; filename="newfile.xml"');
header('Content-type: "text/xml"; charset="utf8"');
echo $directDebit->asXML();
exit;
In this case the generated code will be saved as newfile.xml and download the file automatically. Hope it solves your problem
[1]: https://github.com/php-sepa-xml/php-sepa-xml/blob/master/doc/direct_debit.md

How do you return an image without the filename or extension on the url

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.

download pdf file without specifying the whole name

I have got a button from where users can download specific files. Each time I will have to process the name of the file and put it in a variable before giving it to the readfile function to serve it to the user. However, in many cases the file name is quite long and may change over time so there is a potential of errors in the future. So I was wondering whether one can simply pass something like /filedirectory/*.pdf or just specify only part of the name and rest could be anything and then pass it to the readfile to download the pdf which is found there like in the example below?
if(isset($_POST["submit"]))
{
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="download.pdf"');
// Whatever name the pdf has
readfile('./filedirectory/*.pdf');
#OR
// File starts with 123..
readfile('./filedirectory/123*.pdf');
}
You can use glob to list all PDF files and then you can select the name that you wish and readfile it.
Example
<?php
foreach (glob("*.pdf") as $filename) {
// Do something with the $filename
}
?>
You may get the list of files within /filedirectory using scandir() (http://php.net/manual/en/function.scandir.php) and find out whatever a file name might be.

How do I include a downloadable PDF in a register form?

I am coding a register form and need to include a pdf file next to a checkbox. The pdf file contains important information that the user needs to agree to before sending the form. When you click on the file name that appears next to the checkbox, the browser must ask you if you wanna view or download the file.
How do I realize this?
The way I would go about this is create a download directory "/downloads" and inside there put a php file named index.php with the following code
<?php
$file = 'http://example.com/download/' . $_GET['file']; //Thanks DanFromGermany!
if($file == 'http://example.com/download/index.php'){die;}
$file_headers = #get_headers($file);
if(substr_count($file_headers[0], '404 Not Found') || substr_count($file_headers[0], '410 Gone')){
exit;
} else {
if($file == 'http://example.com/download/index.php'){die;}
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=".$_GET['file'].";");
header("Content-Length: ".filesize($file));
readfile($file);
header('Location: http://example.com/thanks'); //send them to a thanks page or close the window, whatever works for you
exit;
}
?>
Place whatever files you want to be downloaded (read_this.pdf) Then link them as follows
Download this PDF
any filename you point to with the file being placed inside "/downloads" will force their browser to download it.
edit however I agree with Jeroen's suggestion about putting it in a scrollable div. to do this the file would need to be html though but you could use something like http://www.pdftohtml.net/ to make the html look just like the pdf did. I personally leave a website when it tries to force me to download something because you never know what it could be. I only use this method when giving someone a file to download. otherwise, hope this is what you were looking for.

PHP save file to users computer

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?

Categories