when button was clicked then save dialog box should open
code for downloading file in php
when button was clicked then save dialog box should open
code for downloading file in php
Last edited by jscheuer1; 07-09-2011 at 03:14 PM. Reason: merge threads/posts
I don't think you need any special code - just zip up the file and point the href of an anchor tag at it. Zip files should always prompt a download/save box.
Focus on Function Web Design
Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps
I don't undersand what you mean. Are you trying to find a script to download something in php? Also what type of file?
Please clarify your exact requirements. I already answered your other post about this but now I'm thinking there may be more to it.
If browsers are behaving strangely maybe this is a header issue?
Maybe you are enquiring about a zip-and-download script/function? Try pclzip: http://www.phpconcept.net/pclzip/ and tutorial: http://www.techiepark.com/tutorials/...-for-download/
Or do you need something else?
Focus on Function Web Design
Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps
PHP Code:<?php
// place this code inside a php file and call it f.e. "download.php"
$path = $_SERVER['DOCUMENT_ROOT']."/path2file/"; // change the path to fit your websites document structure
$fullPath = $path.$_GET['download_file'];
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-type: application/pdf"); // add here more headers for diff. extensions
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
break;
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
exit;
// example: place this kind of link into the document where the file download is offered:
// <a href="download.php?download_file=some_file.pdf">Download here</a>
?>
Code works well gurmeet. Thanks. I have some questions though:
- Can we redirect to another page and/or echo page content on completion?
- If not, can we do something concurrently to show the user something?
- Shouldn't
fclose ($fd);be inside the conditional that opened the file and established the $fd handle?
- Is the buffering necessary? If so why?
- Similarly is the echoing of the buffer value really needed, and if so why?
Last edited by jscheuer1; 07-23-2011 at 01:37 AM. Reason: add last two questions
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
i created a downloads module for my jcow social network site which uses the following code:
and here is an example link to the page to download, eg. say we wanna download the file 'forums.zip':PHP Code:
$fileserver_path = 'your/downloads/folder/'; // change this to the directory your files reside
$req_file = basename($_GET['file']);
$whoami = basename(__FILE__); // you are free to rename this file
if (empty($req_file)) {
print "Usage: $whoami?file=<file_to_download>";
exit;
}
/* no web spamming */
if (!preg_match("/^[a-zA-Z0-9._-]+$/", $req_file, $matches)) {
print "I can't do that. sorry.";
exit;
}
/* download any file, but not this one */
if ($req_file == $whoami) {
print "I can't do that. sorry.";
exit;
}
/* check if file exists */
if (!file_exists("$fileserver_path/$req_file")) {
print "File <strong>$req_file</strong> doesn't exist.";
exit;
}
if (empty($_GET['send_file'])) {
header("Refresh: 5; url=download.php?file=$req_file&send_file=yes");
}
else {
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header('Content-Length: ' . filesize("$fileserver_path/$req_file"));
header('Content-Disposition: attachment; filename=' . $req_file);
readfile("$fileserver_path/$req_file");
exit;
}
mysql_query("UPDATE `your_downloads_tbl` SET `download_total` = download_total+1 WHERE `download_file` = '$req_file'");
echo '
<h3>Downloading '.$req_file.'...</h3>';
echo '
<p>Your download should begin shortly. If it doesn\'t start,
follow this <a href="'.$fileserver_path.''.$req_file.'">link</a>.</p>';
Code:<a href="http://yourdomain.com/download.php?file=forums.zip">Download Forums</a>
Last edited by liamallan; 07-23-2011 at 08:24 AM.
Bookmarks