Jump to content

[SOLVED] Upload and save image as blob in Mysql database


robinrl2008

Recommended Posts

Hello,

I have been trying to accomplish uploading an image and save it to mysql, so far I can't seem to get anywhere. I have looked at several tutorials and other information and got this script to work partially. The screen comes up but when you choose an image and click submit nothing happens can someone please take a look at this script. I am just learning but I have to learn quickly due to deadlines. I am running IIS on windows XP php 5.2; I know its basic but I am just trying to get a record to save then I will implement the safe guards.

Any help would be very much appreciated!

Robin

 

<html>

<head>

<basefont face="Arial">

<title>File Upload To Database</title>

</head>

<body>

<h3>Please Choose a File and click Submit</h3>

<?php

 

if (!isset($_POST['submit'])) {

// form not submitted

?>

 

    <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

            <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />

            <input name="userfile[]" type="file" />

            <input type="submit" value="Submit" />

        </form>

 

<?php

}

else {

// form submitted

// set server access variables

    $host = "localhost";

    $user = "root";

    $pass = "XXX";

    $db = "YYY";

   

// get form input

    // check to make sure it's all there

    // escape input values for greater safety

 

 

    $imagetype = $_FILES["userfile"]["type"];

    $imagename = $_FILES["userfile"]["name"]; ;

    $image = addslashes(file_get_contents($_FILES["userfile"]["tmp_name"])); 

    $imagesize = $_FILES["userfile"]["size"];

    $imagectgy = $_FILES["userfile"]["type"]; 

 

 

   

 

 

    // open connection

    $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

   

    // select database

    mysql_select_db($db) or die ("Unable to select database!");

   

    // create query

  $query = "INSERT INTO images (image_type, image,image_size,image_ctgy,image_name) VALUES ('$imagetype', '$image','$imagesize','$imagectgy','$imagename')";

   

    // execute query

  $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

   

    // print message with ID of inserted record

  echo "New record inserted with ID ".mysql_insert_id();

 

   

    // close connection

    //mysql_close($connection);

}

?>

 

</body>

</html>

Link to comment
Share on other sites

Change this

 

$image = addslashes(file_get_contents($_FILES["userfile"]["tmp_name"]));

 

to this

 

$fp = fopen($_FILES['userfile']['tmp_name'], 'r');
$content = fread($fp, filesize($_FILES['userfile']['tmp_name']));
$image = base64_encode($content);

 

This will get your content into the database. On display, make sure to use base64_decode or you'll just get gibberish

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.