By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,830 Members | 2,276 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,830 IT Pros & Developers. It's quick & easy.

How to specify the directory to download files

P: 11
hi,

images are stored in MYSQL . What I need to do is to download the images from the database to some predefined directory in the webserver.
I am hoping that someone could help me fixing it.

Expand|Select|Wrap|Line Numbers
  1. <?PHP
  2. //download.php
  3. mysql_connect('localhost','root','') or die("Unable to connect to SQL server");
  4. @mysql_select_db('test') or die("Unable to select database");
  5. $sqlimage = "SELECT * from pix "; 
  6. $sql_imgresult = mysql_query($sqlimage) or die (mysql_error());
  7.  
  8. header("Content-type: application/force-download");
  9. header("Content-Disposition: attachment; filename=image.img");
  10. while ( $row = mysql_fetch_array ($sql_imgresult) )
  11. {
  12. $picname = $row["imgdata"];
  13. echo "$picname";
  14. }
  15. ?>
  16.  
This code downloads the image to the Desktop. But I want to download the images to the a directory where this php file reside. i.e. if this php file "download.php" is in "localhost/images" then I want to download the file to "localhost/images".

Thank You
Sep 30 '08 #1
Share this Question
Share on Google+
3 Replies


Markus
Expert 5K+
P: 6,050
You can't tell the user where the file will be downloaded; PHP doesn't have those permissions, and rightly so. The user chooses where the file will be saved.
Sep 30 '08 #2

Atli
Expert 5K+
P: 5,058
Hi.

You mean you want your images saved on the file-system of the server?

If so, try using the file_put_contents function.

But... Wouldn't it be simpler to upload the file to the file-system to begin with and just put it's location into the database?

Why do you wan't your files to be both inside the database and on the file-system?
Sep 30 '08 #3

P: 11
hi guys ,
thanks for ur reply.

I m still having problem . It would be great if you could edit this code
Expand|Select|Wrap|Line Numbers
  1.  
  2. <?PHP
  3. mysql_connect('localhost','root','') or die("Unable to connect to SQL server");
  4. @mysql_select_db('test') or die("Unable to select database");
  5. $sqlimage = "SELECT * from pix "; 
  6. $sql_imgresult = mysql_query($sqlimage) or die (mysql_error());
  7.  
  8.  $upload_url = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']) . '/';
  9.  $file_path =  $upload_url."imageTemp/ab1.img";
  10.  
  11. header("Content-type: application/force-download");
  12. header("Content-Disposition: attachment; filename=ab1.img");
  13.  
  14. while ( $row = mysql_fetch_array ($sql_imgresult) )
  15. {
  16. $picname = $row["imgdata"]; //picname is the name of the field in the images table
  17. echo "$picname";
  18.  file_put_contents  ( $file_path  , $picname);
  19. }
  20.  
  21. ?>
  22.  
I personally appreciate the idea of storing the file path in the database and storing the images(files) in the file system.That is easy to do and a number of tried and true examples are available in the web.
But here i have to store them in Mysql it would be easier for dealing with other things like synchronization and security
Oct 1 '08 #4

Post your reply

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