View Full Version : code for downloading file
kalkelaxmi
07-09-2011, 06:58 AM
when button was clicked then save dialog box should open
code for downloading file in php
Beverleyh
07-09-2011, 07:43 AM
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.
keyboard
07-09-2011, 07:49 AM
I don't undersand what you mean. Are you trying to find a script to download something in php? Also what type of file?
Beverleyh
07-09-2011, 08:02 AM
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/how-to-create-zip-file-archives-in-php-and-serve-for-download/
Or do you need something else?
gurmeet
07-09-2011, 05:12 PM
<?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>
?>
jscheuer1
07-23-2011, 01:23 AM
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?
liamallan
07-23-2011, 06:40 AM
i created a downloads module for my jcow social network site which uses the following 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>';
and here is an example link to the page to download, eg. say we wanna download the file 'forums.zip':
<a href="http://yourdomain.com/download.php?file=forums.zip">Download Forums</a>
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.