views:

667

answers:

5

I used this code to upload the picture. I got this code from stackoverflow. I am still unable to upload the image. I changed the db connection settings in table settings 2. I made the table but I am not sure whether the properties of the table I created are correct.

<html>
<head><title>Store Some Binary File to a DB</title></head>
<body>

<?php
if ($submit) {
    $conn = mysql_connect("localhost", "your_username", "your_password") 
        or die ("Error connecting to database.");

    mysql_select_db("your_database", $conn) 
        or die ("error connecting to database.");

    $data = addslashes(file_get_contents($_FILES['form_data']['tmp_name'])


    $result = mysql_query ("INSERT INTO your_table ".
         "(description,bin_data,filename,filesize,filetype) ".
         "VALUES ('$form_description','$data','$form_data_name',".
         "'$form_data_size','$form_data_type')",
         $conn);

    $id = mysql_insert_id();
    print "<p>This file has the following Database ID: <b>$id</b>";

    mysql_close();

} else {
?>

    <form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data">
    File Description:<br>
    <input type="text" name="form_description"  size="40">
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
    <br>File to upload/store in database:<br>
    <input type="file" name="form_data"  size="40">
    <p><input type="submit" name="submit" value="submit">
    </form>

<?php

}

?>

</body>
</html>

Also, help me in retrieving the image from the database and showing it on a page.

do u think thiis will work now?

+1  A: 

See http://www.php.net/manual/en/features.file-upload.php

Instead of using $form_data as the filename of the uploaded file, try $_FILES['form_data']['tmp_name']

You could also replace the rather clumsy

fread(fopen($form_data, "r"), filesize($form_data))

with

file_get_contents($_FILES['form_data']['tmp_name'])
Paul Dixon
+1  A: 

I wouldn't suggest storing your binary image file directly in the database. Instead, store it on disk and store the file pointer (file directory + file name) in the DB.

Pras
How can i save image in database using php please help me.
shah.shakthi
+1  A: 

The problem with storing images on the filesystem comes down to database replication. You would also need filesystem replication which can sometimes be harder than database replication especially when some servers may be remote.

None of your $form_ variables are set (unless theres code I cant see or you 'dangerously' have register globals on). Also you will want to addslashes (or mysql_real_escape_string) on any queries that accept REQUEST values, else your script is prone to SQL injection.

You want to:

$form_description = isset($_POST['form_description']) ? $_POST['form_description'] : false;

The other variables are not even being posted. Do you know any HTML and PHP? this is easy to resolve but I am not writing this for you!

What we need is some feedback what the script produces.. you can get the image back by doing:

$result = mysql_query ("SELECT `bin_data` FROM `your_table` WHERE `id` = ".$id, $conn);

Then do for example (replacing the content type with the relevant type determined from the files array):

$result_data = mysql_fetch_array($result, MYSQL_ASSOC);
header('Content-Type: image/jpg');
echo $result_data['bin_data'];
exit;

You need to do a bit more work to that script, and that requires knowledge of HTML, PHP and MySQL.

Jay
A: 

I also have problem with uploading a image using PHP. Here I attached codes of my testing program. But when I click the submit button it will display the set of of codes of my .php file.

Filename:

0) { echo "Error: " . $_FILES["file"]["error"] . "
"; } else { echo "Upload: " . $_FILES["file"]["name"] . "
"; echo "Type: " . $_FILES["file"]["type"] . "
"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; }?>

A: 

how to save the value of the color histogram in the database? how to how to search for images based on histogram distance based on the color? thanks for the support and confirmation