954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Upload images to mysql database

0
By Alex Telford on Aug 4th, 2010 3:17 pm

first create the table

create table images (
    image_id    serial,
    filename    varchar(255) not null,
    mime_type   varchar(255) not null,
    file_size   int          not null,
    file_data   longblob     not null,
    primary key (image_id),
    index (filename)
);

the file to output the images to the browserpicsrc.php

<?PHP
//detect if image is called correctly
if($image){
//connect to db
    $db = mysql_connect('localhost', 'user', 'password');
//find the image
        $query  = sprintf('select * from images where filename = %d', $image);
        $result = mysql_query($query, $db);
$image = mysql_fetch_array($result);
//tell the browser the url is an image
    header('Content-type: ' . $image['mime_type']);
    header('Content-length: ' . $image['file_size']);
//finally output the image
echo $image['file_data'];
}
?>

to call the image use this line

<img src="picsrc.php?image=potato.jpeg">


fairly straightforward. you can view a full tutorial at http://www.effectivewebdesign.co.nz/tutorial.php

hope this helps.

<?php
//connect to database
    $db = mysql_connect('localhost', 'user', 'password');

//detect if form has been processed
 if($_FILES['image']){
  $image = $_FILES['image'];
//detect file is image
  $info = getImageSize($image['tmp_name']);
        if (!$info) {
            $notice = 'File is not an image';
        }
  else {
//insert the image
           $query = sprintf(
            "insert into images (filename, mime_type, file_size, file_data)
                values ('%s', '%s', %d, '%s')",
            mysql_real_escape_string($image['name']),
            mysql_real_escape_string($info['mime']),
            $image['size'],
            mysql_real_escape_string(
                file_get_contents($image['tmp_name'])
            )
        );
$notice = 'Image Uploaded successfully';
  }

 }
?>
<html><head>
<title>Upload an Image</title>
</head>
<body>
<h1>Upload an Image</h1>
<?PHP if ($notice){
//echo image status
 echo $notice;
}
?>
<br />
<a href="view.php">View uploaded images</a>
<form method="post" action="<?PHP echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit" value="Upload Image" />
</form>
</body>
</html>

yes I realise it is in the wrong order.
It is difficult to put tutorials in code snippets :)

metalix
Posting Whiz in Training
218 posts since Mar 2010
Reputation Points: 13
Solved Threads: 34
 

Hey where is the

<a href="view.php">View uploaded images</a>

Code

Rewired
Light Poster
30 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

Yeah sorry I didn't put that one in. you just call the images as you normally would (pic.jpg) but instead you'll have picsrc.php?image=myimage.jpg

however I suggest you use a .htaccess mod rewrite and an url encoder so as to make it a little more secure (I removed security checks for simplicity)

metalix
Posting Whiz in Training
218 posts since Mar 2010
Reputation Points: 13
Solved Threads: 34
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: