how to download the files stored in database using php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sushant29
    New Member
    • Aug 2012
    • 5

    how to download the files stored in database using php

    hey guys is this coding is write for downloading purpose.
    this gives me a file from database but it doesn't shows the file content in it.there is an error which shows in my file
    <br />
    <b>Warning</b>: readfile(..\wam p\www\New folder\download file\New folderSushant_N aik.doc) [<a href='function. readfile'>funct ion.readfile</a>]: failed to open stream: No such file or directory in <b>C:\wamp\www\ New folder\download file\download1. php</b> on line <b>48</b><br />


    Code:
    <?php
    
    include('config.php');
    $filename=$_GET['fname'];
    $ctype=$_GET['ctype'];
    $size=$_GET['size'];
    /* echo $filename;
    echo $ctype;
    echo $size; */
    $tmp = explode(".",$filename); 
    switch ($tmp[count($tmp)-1]) 
    { 
      case "pdf": $ctype="application/pdf"; break; 
      case "exe": $ctype="application/octet-stream"; break; 
      case "zip": $ctype="application/zip"; break; 
      case "docx": 
      case "doc": $ctype="application/msword"; break; 
      case "csv": 
      case "xls": 
      case "xlsx": $ctype="application/vnd.ms-excel"; break; 
      case "ppt": $ctype="application/vnd.ms-powerpoint"; break; 
      case "gif": $ctype="image/gif"; break; 
      case "png": $ctype="image/png"; break; 
      case "jpeg": 
      case "jpg": $ctype="image/jpg"; break; 
      case "tif": 
      case "tiff": $ctype="image/tiff"; break; 
      case "psd": $ctype="image/psd"; break; 
      case "bmp": $ctype="image/bmp"; break; 
      case "ico": $ctype="image/vnd.microsoft.icon"; break; 
      default: $ctype="application/force-download"; 
    } 
    
    $path=$filename;
     
    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-Type: $ctype"); 
    header("Content-Disposition: attachment; filename=\"$path\""); 
    header("Content-Transfer-Encoding: binary"); 
    header("Content-Length: ".$size); 
    echo $path;
    ob_clean(); 
    flush(); 
    readfile($path)
    ?>
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    The error that is showing tells us exactly why the code is failing: No such file or directory. The file path you are passing to the readfile() function is invalid, so PHP can't send it to you.

    You say this is a file "from database". Do you mean that the file is stored in the database, like I do in the tutorial you originally posted in, or is the file on the file system and it's location in the database? (The latter is what you usually do, and what you should usually do.)

    If the file is inside the database, then what you are doing there makes no sense. Look at phase #4 in the tutorial for an example of what you should be doing.

    If the file is on the file-system, then there are steps you must take before trying to send it. First of all, you need to verify that the file actually exists before you try to read it. For that, the file_exists function can be used. You should never attempt to send a file without using that function to make sure it exists.

    Also, as with all user input, you should verify that $_GET values exist before you use them. If your code depends on a $_GET["filename"] value being present, you need to use isset() or empty() to make sure that it really exists, and/or that it actually has a value. (The isset() function only verifies the former, while empty() verifies both.)
    Code:
    if (!empty($_GET["filename"])) {
        // Proceed with the script.
    }
    else {
        echo "You must pass a file name if you want to download a file!";
    }

    Comment

    • Sushant29
      New Member
      • Aug 2012
      • 5

      #3
      hey Atli , thanks for reply but i have sort out with the downloading coding.

      And yes it was wrong way to access file in previous coding , now i can easily download it directly from database.

      The Coding seems to be like this and it work's...
      Code:
      <?php
      
      include('config.php');
      $ID=$_GET['id'];
      echo $ID;
      $query="SELECT * FROM upload WHERE id=$ID";
      $result=mysql_query($query);
      
      $row=mysql_num_rows($result);
      for($i=1;$i<=$row;$i++)
      {
      $data=mysql_fetch_object($result);
      $filename=$data->name;
      $type=$data->type;
      $size=$data->size;
      $content=$data->data;
       
       
      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-Type:".$type); 
      header("Content-Disposition: attachment; filename=".$filename ); 
      header("Content-Transfer-Encoding: binary"); 
      header("Content-Length: ".$size);  
      ob_clean(); 
      flush();   
      echo $content;
      
      }
      ?>

      Comment

      Working...