# help uploading and downloading files from mysql database using php



## chishake

Can anyone help me with my php script. i have been trying to upload and download files (images, pdfs etc) from mysql database but i haven't been able to. i have succeeded in uploading but the problem now is to download. i have used this code 
<?
//connect to the database
include "dbaseConnection.php";
$query = "SELECT * FROM images WHERE id = 1";
$result = mysql_query($query) or die(mysql_error());

// define results into variables
$name=mysql_result($result,0,"name");
$size=mysql_result($result,0,"size");
$type=mysql_result($result,0,"type");
$content=mysql_result($result,0,"content");

// give our picture the proper headers...otherwise our page will be confused
header("Content-Disposition: attachment; filename=$name");
header("Content-length: $size");
header("Content-type: $type");
echo $content;

mysql_close();
?>
but its not giving me any results.
can anyone help me please?


----------



## tajjyarden

Here is a script I found that does exactly what you are wanting:



Code:


<?php
    // just so we know it is broken
    error_reporting(E_ALL);
    // some basic sanity checks
    if(isset($_GET['image_id']) && is_numeric($_GET['image_id'])) {
        //connect to the db
        $link = mysql_connect("localhost", "username", "password") or die("Could not connect: " . mysql_error());
 
        // select our database
        mysql_select_db("testblob") or die(mysql_error());
 
        // get the image from the db
        $sql = "SELECT image FROM testblob WHERE image_id=0";
 
        // the result of the query
        $result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
 
        // set the header for the image
        header("Content-type: image/jpeg");
        echo mysql_result($result, 0);
 
        // close the db link
        mysql_close($link);
    }
    else {
        echo 'Please use a real id number';
    }
?>

My only guess without seeing any of your actual data is that it's a problem with the headers.


----------



## chishake

that you script doesnt help at all. did you even try it at all? echo mysql_result($result, 0); echos but the id number of the image i am selecting. my script above does the job but the problem is that it only works on pdf files. when i try to download an image, i actually get the download screen with the image type name and size on it. but when i click to open or save the image, it downloads but the image doesnt show up. unlike with pdf file it shows.


----------



## tajjyarden

sorry for trying to help. Your description was so vague how was I supposed to know what the problem was?

Good luck buddy


----------



## Redcore

Yeah, you could *definitely* be more respectful to those who are trying to help. Nobody gets paid to help you with your problems and thus every piece of commentary (whether it is the solution you are looking for or not) needs to be taken with gratefulness.

I wrote a tutorial (that is on this forum) for uploading files into a MySQL database:

http://www.techsupportforum.com/des...ng/176804-tutorial-upload-files-database.html

If you need help after you've digested that material, let me know.


----------



## Pyro-Fire

wouldnt that be pretty stressful on the mysql database?

use this instead imo.

http://www.php.net/features.file-upload


----------

