View Full Version : download script required
gurmeet
01-01-2011, 08:16 AM
Hi,
I have a php page; I want a download script….
There is a download button on a page, I want a when a user clicks on this button, dynamically a selected file start downloading. On the same page, without refreshing …
Plz tell me how it’s possible, or give me a link where I can download such script… I searched but not found such coding…
Beverleyh
01-01-2011, 10:28 AM
What do you mean by " dynamically a selected file starts downloading"?
Please provide more info and a link to the page
djr33
01-01-2011, 12:30 PM
You can't control what a browser does with a file. By default a new page is always loaded when you load a new file in the browser, except when the browser CHOOSES to save that file instead. You might want to look into a "force download [script]", but that's not entirely reliable.
fastsol1
01-01-2011, 02:42 PM
Do you mean you want to click and say download a picture or a file and the normal file "save or open" box pops up. If so this is what I have used.
<?php
// block any attempt to the filesystem
if (isset($_GET['file']) && basename($_GET['file']) == $_GET['file']) {
$filename = $_GET['file'];
$folder = $_GET['folder'];
} else {
$filename = NULL;
}
// define error message
$err = '<p style="color:#990000">Sorry, the file you are requesting is unavailable.</p>';
if (!$filename) {
// if variable $filename is NULL or false display the message
echo $err;
} else {
// define the path to your download folder plus assign the file name
$path = 'images/albums/'.$folder.'/'.$filename;
// check that file exists and is readable
if (file_exists($path) && is_readable($path)) {
// get the file size and send the http headers
$size = filesize($path);
header('Content-Type: application/octet-stream');
header('Content-Length: '.$size);
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
// open the file in binary read-only mode
// display the error messages if the file cant be opened
$file = @ fopen($path, 'rb');
if ($file) {
// stream the file and exit the script when complete
fpassthru($file);
exit;
} else {
echo $err;
}
} else {
echo $err;
}
}
?>
And then just put a link on the appropriate item to download and in you link tag include the appropriate GET variables for the code to reference. I saved this as a separate page called download.php
gurmeet
01-01-2011, 03:46 PM
yes its good script....
let me try
thanks
gurmeet
01-01-2011, 04:47 PM
Do you mean you want to click and say download a picture or a file and the normal file "save or open" box pops up. If so this is what I have used.
<?php
// block any attempt to the filesystem
if (isset($_GET['file']) && basename($_GET['file']) == $_GET['file']) {
$filename = $_GET['file'];
$folder = $_GET['folder'];
} else {
$filename = NULL;
}
// define error message
$err = '<p style="color:#990000">Sorry, the file you are requesting is unavailable.</p>';
if (!$filename) {
// if variable $filename is NULL or false display the message
echo $err;
} else {
// define the path to your download folder plus assign the file name
$path = 'images/albums/'.$folder.'/'.$filename;
// check that file exists and is readable
if (file_exists($path) && is_readable($path)) {
// get the file size and send the http headers
$size = filesize($path);
header('Content-Type: application/octet-stream');
header('Content-Length: '.$size);
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
// open the file in binary read-only mode
// display the error messages if the file cant be opened
$file = @ fopen($path, 'rb');
if ($file) {
// stream the file and exit the script when complete
fpassthru($file);
exit;
} else {
echo $err;
}
} else {
echo $err;
}
}
?>
And then just put a link on the appropriate item to download and in you link tag include the appropriate GET variables for the code to reference. I saved this as a separate page called download.php
is it possible to rename file before downloading?
where to use rename method in this script... i tried but it doesnot works
fastsol1
01-01-2011, 06:42 PM
The script is one that I found online previous. I don't know a lot about the file download process but I would think you couldn't rename it automatically cause it's simply getting the current file name and you're not actually doing anything with it to be able to rename it. I know you can name it whatever you want on upload but not sure about download.
bluewalrus
01-01-2011, 10:32 PM
This line I think is what is displayed in the save as prompt
header('Content-Disposition: attachment; filename='.$filename);
so change
'.$filename
to whatever you want the file name to be
file_name_is'
You'll probably also need the file extension in there, not sure though post back if it saves without the ext
gurmeet
01-02-2011, 07:22 AM
this scripts downloads files, but the image file are not readable.....
when i open downloaded files, it says " preview not available"....
bluewalrus
01-02-2011, 04:26 PM
do they have the extension?
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.