473,473 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to download the files stored in database using php

5 New Member
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.  
Sep 2 '12 #1
2 16214
Atli
5,058 Recognized Expert Expert
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.)
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.  
Sep 3 '12 #2
Sushant29
5 New Member
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.  
Sep 3 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: fibreiv | last post by:
I am trying to download files from my database that I uploaded to it. I can download bmp, txt file but have not been able to d/l pdf files. I am able to d/l pdf files stored in the file system...
13
by: Daniel Walzenbach | last post by:
Hi, Imagine the following situation: I have an asp.net application which allows uploading files to a SQL Server 2000 database (Files are stored as type "images"). As a next step I would like to...
1
by: JessiRight77 | last post by:
Hello... Our website is built using Dreamweaver. We need to incorporate some script that will allow users to view files stored in a folder on our webserver, and then download them to their...
2
by: Hatricks | last post by:
Hi, How can I store & retrieve PDF file in SQL 2005 database using java. Any code will be of great help.
1
by: ganeshg | last post by:
Hai All, Please let me know how to insert audio files into mysql database using .net. Thanking you.....
0
by: namrataa | last post by:
Hi, I want to write an application in WPF using C# in VS2008 to upload media files to database and download media files from database to my hard disk. In ASP we have that provision but in WPF I did...
0
by: alister7 | last post by:
i need a code to download a music file(.wav) in WPF Browser Applications. below is the code that m trying... private void downloadfile() { string connectionstring = @"Data...
6
by: JFKJr | last post by:
Hello everyone, I am a new beginner to ASP.NET, C# and I am trying to upload/download files as blobs into sql database. I searched a lot for the code on google, I found only vb.net code but not...
3
by: axelman | last post by:
Hi guys, I'm using Classic ASP, IIS6, IE 7, FF 3 I've developed a vb script to downloand files hosted in my server (zip, doc, pdf, jpg, xls, et. etc.) it's very straight forward and there's a ...
0
by: Amar Nath | last post by:
i am using this code..it shows the error as bulk insert CSVTest1 from 'D:\1_attlog.txt' with (FIELDTERMINATOR=' ') { FIELDTERMINATOR=' ' ROWTERMINATOR='\N'...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.