Results 1 to 10 of 10

Thread: download script required

  1. #1
    Join Date
    Feb 2009
    Posts
    156
    Thanks
    0
    Thanked 4 Times in 3 Posts

    Default download script required

    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…

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    What do you mean by " dynamically a selected file starts downloading"?

    Please provide more info and a link to the page
    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

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    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 Code:
    <?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

  5. #5
    Join Date
    Feb 2009
    Posts
    156
    Thanks
    0
    Thanked 4 Times in 3 Posts

    Default

    yes its good script....

    let me try
    thanks

  6. #6
    Join Date
    Feb 2009
    Posts
    156
    Thanks
    0
    Thanked 4 Times in 3 Posts

    Default

    Quote Originally Posted by fastsol1 View Post
    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 Code:
    <?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

  7. #7
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    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.

  8. #8
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    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
    Corrections to my coding/thoughts welcome.

  9. #9
    Join Date
    Feb 2009
    Posts
    156
    Thanks
    0
    Thanked 4 Times in 3 Posts

    Default

    this scripts downloads files, but the image file are not readable.....
    when i open downloaded files, it says " preview not available"....

  10. #10
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    do they have the extension?
    Corrections to my coding/thoughts welcome.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •