Loading an image using php

I used this code to load an image. I got this code from stackoverflow. I still cannot upload the image. I changed the db connection parameters in table 2 settings. I made a table, but I'm not sure if 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 to fetch the image from the database and display it on the page.

Do you think yours will work now?

-3


source to share


3 answers


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

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

You can also replace the rather clumsy



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

      

from

file_get_contents($_FILES['form_data']['tmp_name'])

      

+1


source


I would not suggest storing your binary image file directly in the database. Instead, save it to disk and save the file pointer (file directory + filename) to the DB.



+1


source


The problem with storing images in the file system comes down to database replication. You will also need file system replication, which can sometimes be more complex than database replication, especially when some servers may be remote.

None of your $ form_ variables are set (unless the code I see, or you are "dangerous", has a global register). Also you will want addlashes (or mysql_real_escape_string) for any queries that accept REQUEST values, otherwise your script is prone to SQL injection.

Do you want to:

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

      

The other variables are not even published. Do you know any HTML and PHP? it's easy to resolve, but I'm not writing this for you!

We need some feedback that the script produces. You can get the image back by running:

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

      

Then, for example (replacing the content type with the appropriate type determined from the array of files):

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

      

You need to do a little more work with this script and it requires knowledge of HTML, PHP and MySQL.

+1


source







All Articles