Php code for downloading a file from database

Php code for downloading a file from database

php code for downloading a file from database

Using pieces of the forced download script, adding in MySQL database functions, and hiding the file location for security was what we needed for downloading. So I have a script that uploads the file correctly to the database I see it there when I look in phpmyadmin. however when I use a script to obtain said file using the. PHP-MySQL-File-upload. How to upload and download files PHP and MySQL. This code example demonstrates the process to implement the file upload.

Php code for downloading a file from database - suggest

Php code for downloading a file from database - remarkable, rather

php code for downloading a file from database

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(..\wamp\www\New folder\download file\New folderSushant_Naik.doc) [<a href='function.readfile'>function.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 />


Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. include('config.php');
  4. $filename=$_GET['fname'];
  5. $ctype=$_GET['ctype'];
  6. $size=$_GET['size'];
  7. /* echo $filename;
  8. echo $ctype;
  9. echo $size; */
  10. $tmp = explode(".",$filename); 
  11. switch ($tmp[count($tmp)-1]) 
  12.   case "pdf": $ctype="application/pdf"; break; 
  13.   case "exe": $ctype="application/octet-stream"; break; 
  14.   case "zip": $ctype="application/zip"; break; 
  15.   case "docx": 
  16.   case "doc": $ctype="application/msword"; break; 
  17.   case "csv": 
  18.   case "xls": 
  19.   case "xlsx": $ctype="application/vnd.ms-excel"; break; 
  20.   case "ppt": $ctype="application/vnd.ms-powerpoint"; break; 
  21.   case "gif": $ctype="image/gif"; break; 
  22.   case "png": $ctype="image/png"; break; 
  23.   case "jpeg": 
  24.   case "jpg": $ctype="image/jpg"; break; 
  25.   case "tif": 
  26.   case "tiff": $ctype="image/tiff"; break; 
  27.   case "psd": $ctype="image/psd"; break; 
  28.   case "bmp": $ctype="image/bmp"; break; 
  29.   case "ico": $ctype="image/vnd.microsoft.icon"; break; 
  30.   default: $ctype="application/force-download"; 
  31.  
  32. $path=$filename;
  33.  
  34. header("Pragma: public"); // required 
  35. header("Expires: 0"); 
  36. header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
  37. header("Cache-Control: private",false); // required for certain browsers 
  38. header("Content-Type: $ctype"); 
  39. header("Content-Disposition: attachment; filename=\"$path\""); 
  40. header("Content-Transfer-Encoding: binary"); 
  41. header("Content-Length: ".$size); 
  42. echo $path;
  43. ob_clean(); 
  44. flush(); 
  45. readfile($path)
  46. ?>
  47.  

✓ answered by Atli

Hi.

The error that is showing tells us exactly why the code is failing: . The file path you are passing to the 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 values exist before you use them. If your code depends on a 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.)
Expand|Select|Wrap|Line Numbers
  1. if (!empty($_GET["filename"])) {
  2.     // Proceed with the script.
  3. }
  4. else {
  5.     echo "You must pass a file name if you want to download a file!";
  6. }
  7.  


Hi.

The error that is showing tells us exactly why the code is failing: . The file path you are passing to the 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 values exist before you use them. If your code depends on a 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.)
Expand|Select|Wrap|Line Numbers
  1. if (!empty($_GET["filename"])) {
  2.     // Proceed with the script.
  3. }
  4. else {
  5.     echo "You must pass a file name if you want to download a file!";
  6. }
  7.  

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...
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. include('config.php');
  4. $ID=$_GET['id'];
  5. echo $ID;
  6. $query="SELECT * FROM upload WHERE id=$ID";
  7. $result=mysql_query($query);
  8.  
  9. $row=mysql_num_rows($result);
  10. for($i=1;$i<=$row;$i++)
  11. {
  12. $data=mysql_fetch_object($result);
  13. $filename=$data->name;
  14. $type=$data->type;
  15. $size=$data->size;
  16. $content=$data->data;
  17.  
  18.  
  19. header("Pragma: public"); // required 
  20. header("Expires: 0"); 
  21. header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
  22. header("Cache-Control: private",false); // required for certain browsers 
  23. header("Content-Type:".$type); 
  24. header("Content-Disposition: attachment; filename=".$filename ); 
  25. header("Content-Transfer-Encoding: binary"); 
  26. header("Content-Length: ".$size);  
  27. ob_clean(); 
  28. flush();   
  29. echo $content;
  30.  
  31. }
  32. ?>
  33.  
Источник: [https://torrent-igruha.org/3551-portal.html]

Php code for downloading a file from database

3 thoughts to “Php code for downloading a file from database”

Leave a Reply

Your email address will not be published. Required fields are marked *