1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to create download link

Discussion in 'PHP' started by adsegzy, Aug 7, 2010.

  1. #1
    Hello friends,

    please how do i create a download link through which once my visitor click on the product or the download link they will be able to download files like MSWord format, PDF format, Audios and Videos.

    Regards,
    adsegzy
     
    adsegzy, Aug 7, 2010 IP
  2. scriptsy26

    scriptsy26 Active Member

    Messages:
    274
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    83
    #2
    I use this <a href="<?=$link?>">Download</a>

    Not that i'm an expert, but on top of the page where the $link? stands for and what he should do.
     
    scriptsy26, Aug 7, 2010 IP
  3. kadmine

    kadmine Peon

    Messages:
    1
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    you can use this code in a page and call it from link.

    header('Content-type: application/pdf');

    // It will be called downloaded.pdf
    header('Content-Disposition: attachment; filename="lamp_portfolio.pdf"');

    // The PDF source is in original.pdf
    readfile('1.pdf');

    Regards,
    kadmine
     
    kadmine, Aug 7, 2010 IP
    usasportstraining likes this.
  4. caciocode

    caciocode Peon

    Messages:
    65
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    caciocode, Aug 8, 2010 IP
  5. swashata

    swashata Member

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #5
    kadmine has shown you a way! I will show you another, based on that approach! What we would do here is force the browser to download whatever it has!

    We would create a file download.php... And would place files inside different folders where the download.php resides... have a look at the screenshot...
    file structure.png
    Now for downloading we would pass the URL like this:
    And here is the code inside the download.php file!

    <?php
    $file = $_GET['file']; //Get the file from URL variable
    $file_array = explode('/', $file); //Try to seperate the folders and filename from the path
    $file_array_count = count($file_array); //Count the result
    $filename = $file_array[$file_array_count-1]; //Trace the filename
    $file_path = dirname(__FILE__).'/'.$file; //Set the file path w.r.t the download.php... It may be different for u
    
    header("Content-disposition: attachment; filename={$filename}"); //Tell the filename to the browser
    header('Content-type: application/octet-stream'); //Stream as a binary file! So it would force browser to download
    readfile($file_path); //Read and stream the file
    ?>
    PHP:
    That;s it! Although I would recommend storing the file information inside database and then collect the information from download.php for a better security!
     
    swashata, Aug 8, 2010 IP
    usasportstraining likes this.
  6. reza56

    reza56 Active Member

    Messages:
    92
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
  7. militarysmurf

    militarysmurf Member

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    40
    #7
    Instead of readfile try fopen,fread in small increments.

    I find this helps when using PHP to stream large files, a 2GB file would just about crash PHP when using readfile
     
    militarysmurf, Aug 8, 2010 IP
    usasportstraining likes this.
  8. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #8
    Why not simply use the pathinfo() function?
     
    danx10, Aug 9, 2010 IP
    usasportstraining likes this.
  9. militarysmurf

    militarysmurf Member

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    40
    #9
    Why not basename or basefile
     
    militarysmurf, Aug 9, 2010 IP
  10. swashata

    swashata Member

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #10
    I just showed a possible way! But to tell you the truth, I find it really stupid to pass the file location as URL variable! That should reside on some database for better security ;)
     
    swashata, Aug 9, 2010 IP
  11. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #11
    Or just encode the file location, so all the user shall see is download.php?file=354865843= (example) and then on download.php, decode the $_GET['file'] to make it usable...you'll obviously have to create a function to encode/decode the file location. (consider using a variation of php's internal functions)
     
    danx10, Aug 9, 2010 IP
  12. swashata

    swashata Member

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #12
    Very much true! :)
     
    swashata, Aug 9, 2010 IP
  13. adsegzy

    adsegzy Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Thank you all for your assistance, i really appreciate it a lot. pls is there any where i can be voting for you on your contributions because i have seen other forums where by once the members contribution is of help to you, you can just click on a button to vote for that person. i have search this forum if there is anything like that but i could not find. in case there is any way pls tell me because i want to be showing my heart of appreciation.
     
    adsegzy, Aug 9, 2010 IP
  14. usasportstraining

    usasportstraining Notable Member

    Messages:
    4,876
    Likes Received:
    363
    Best Answers:
    0
    Trophy Points:
    280
    Articles:
    4
    #14
    You could Rate the thread (upper right corner) and rate the contribution (lower left of each post).
     
    usasportstraining, Aug 9, 2010 IP
  15. bankruptcyonly

    bankruptcyonly Peon

    Messages:
    91
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    sorry, but you can find that code from google.
     
    bankruptcyonly, Aug 9, 2010 IP