How to specify the directory to download files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • foss
    New Member
    • Dec 2007
    • 11

    #1

    How to specify the directory to download files

    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.

    Code:
    <?PHP
    //download.php
    mysql_connect('localhost','root','') or die("Unable to connect to SQL server");
    @mysql_select_db('test') or die("Unable to select database");
    $sqlimage = "SELECT * from pix "; 
    $sql_imgresult = mysql_query($sqlimage) or die (mysql_error());
    
    header("Content-type: application/force-download");
    header("Content-Disposition: attachment; filename=image.img");
    while ( $row = mysql_fetch_array ($sql_imgresult) )
    {
    $picname = $row["imgdata"];
    echo "$picname";
    }
    ?>
    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.p hp" is in "localhost/images" then I want to download the file to "localhost/images".

    Thank You
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    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.

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi.

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

      If so, try using the file_put_conten ts 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?

      Comment

      • foss
        New Member
        • Dec 2007
        • 11

        #4
        hi guys ,
        thanks for ur reply.

        I m still having problem . It would be great if you could edit this code
        Code:
        <?PHP
        mysql_connect('localhost','root','') or die("Unable to connect to SQL server");
        @mysql_select_db('test') or die("Unable to select database");
        $sqlimage = "SELECT * from pix "; 
        $sql_imgresult = mysql_query($sqlimage) or die (mysql_error());
         
        [B] $upload_url = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']) . '/';
         $file_path =  $upload_url."imageTemp/ab1.img";[/B]
         
        header("Content-type: application/force-download");
        header("Content-Disposition: attachment; filename=ab1.img");
        
        while ( $row = mysql_fetch_array ($sql_imgresult) )
        {
        $picname = $row["imgdata"]; //picname is the name of the field in the images table
        echo "$picname";
        [B] file_put_contents  ( $file_path  , $picname);[/B]
        }
        
        ?>
        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

        Comment

        Working...