SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast DeadPencil's Avatar
    Join Date
    Dec 2004
    Location
    England
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP: download file

    hi guys,

    I've got a problem supplying users with a file which their browser should prompt to download.
    The download link uses a GET to supply a file id which the PHP script should then query the database and supply the file filling in the appropriate header details. The problem comes with the readfile("uploads/".$filename); function at the end. It never finds the file even though I've tried every path variation I can think of (absolute/relative).
    When I echo out the the various headers they all output fine. However, the file always downloads with the correct filename (example testdownload.doc) but with the content:

    Code:
    readfile(uploads/testdownload.doc) [<a href='function.readfile'>function.readfile</a>]: failed to open stream: No such file or directory in <b>/home/uniform/public_html/getfile.php</b> on line <b>68</b><br />
    The file getfile.php is located in the site root and the files it needs to return are in a directory uploads. Is readfile() expecting a relative path or absolute path? I've tried both but why can't it find the file?!
    Here's the offending piece of code, have I made a mistake somewhere?

    Code:
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false);
    header("Content-Type: ".$ctype);
    header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".$filesize);
    readfile("uploads/".$filename);
    Thanks for any advice,
    Rob

  2. #2
    SitePoint Guru Ruben K.'s Avatar
    Join Date
    Jun 2005
    Location
    Alkmaar, The Netherlands
    Posts
    693
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd use file_get_contents()

  3. #3
    SitePoint Addict
    Join Date
    Sep 2007
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try checking the permissions of the uploads folder and testdownload.doc file. is it readable by the server? Also, try echoing the filename before trying to read it, just to make sure that you're supplying correct filename.

  4. #4
    SitePoint Addict
    Join Date
    Mar 2003
    Location
    In a house in the USA
    Posts
    293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have used this code in the past:
    PHP Code:
    <?php
    function downloadFile($fileName$downloadName ''$mimetype false)
    {
      if (!
    is_file($fileName)) {
        return 
    false;
      }
      
      
    $data file_get_contents($fileName);
      
    $filesize strlen($data);
      
      if (empty(
    $mimetype)) {
        
    $mimetype 'application/octet-stream';
      }
      
      if (empty(
    $downloadName)) {
        
    $downloadName basename($fileName);
      }
      
      
    // Start sending headers
      
    header("Pragma: public"); // required
      
    header("Expires: 0");
      
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
      
    header("Cache-Control: private",false); // required for certain browsers
      
    header("Content-Transfer-Encoding: binary");
      
    header("Content-Type: " $mimetype);
      
    header("Content-Length: " $filesize);
      
    header("Content-Disposition: attachment; filename=\"" $downloadName "\";" );
      
      
    // Send data
      
    echo $data;
      die();
    }

    downloadFile('uploads/test.docx''word.docx');
    ?>
    Does this help you at all?
    Daniel
    http://www.wlscripting.com - PHP Tutorials and code snippets
    Notepad++ Function List plugin tip - for PHP developers

  5. #5
    SitePoint Enthusiast DeadPencil's Avatar
    Join Date
    Dec 2004
    Location
    England
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks so much guys, I got it solved by using and abusing Daniels code above - it worked perfectly.


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
  •