1,105,328 Community Members

adding images to a database

Member Avatar
tape enterprise
Junior Poster in Training
59 posts since Feb 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

hello,
I am currently working on CMS and I want a way for the user to upload images to go along with their post

how would I upload the image with php and then add it into the database for later retrieval when people look at the posts?

Member Avatar
Shanti C
Posting Virtuoso
1,675 posts since Jul 2008
Reputation Points: 106 [?]
Q&As Helped to Solve: 165 [?]
Skill Endorsements: 4 [?]
 
0
 

you can use some editors like : http://www.openwebware.com/

Member Avatar
lyrico
Junior Poster in Training
94 posts since Dec 2010
Reputation Points: 23 [?]
Q&As Helped to Solve: 15 [?]
Skill Endorsements: 0 [?]
 
0
 

I think you can save the image on the server drives but not on the database itself. The path of the image is the one that saves in the database.

Member Avatar
dcdruck
Junior Poster in Training
92 posts since Jul 2009
Reputation Points: 11 [?]
Q&As Helped to Solve: 20 [?]
Skill Endorsements: 0 [?]
 
1
 

I think you can save the image on the server drives but not on the database itself. The path of the image is the one that saves in the database.

That's one way to do it, but you can also save the actual image in the database in a BLOB (Binary Large OBject) column. You could do something like this:

Create a table in the database to store the images:

CREATE TABLE images (
  id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  mime VARCHAR(50),
  filename VARCHAR(255),
  imagedata MEDIUMBLOB
);

To add the image to the database:

$yourimage = (object)$_FILES[$your_image_input_name];
if ($yourimage->error == UPLOAD_ERR_OK)
{
  $dbinsert = new stdClass;
  $dbinsert->mime = $yourimage->type;
  $dbinsert->filename = $yourimage->name;

  // read the image file into your data object
  $dbinsert->imagedata = 
    addslashes(fread(fopen($yourimage->tmp_name, 'r'), $yourimage->size));

  // execute a sql statement inserting the values in $yourimage into the database
}
else
{
  // there was an error with the form submission
}

To later view your image, link to viewimage.php?id=THE_ID_OF_THE_IMAGE_YOU_WANT:

// file viewimage.php
$id = $_GET['id'];

// validate the id by trying to load the image from the database
// e.g. $image = your_database_query_function("SELECT * FROM images WHERE id='{$id}'");

// now that you have the image data, send it to the browser (there
// are surely better ways to do this, but this is what comes to
// mind and I've used it in the past)
header("Content-type: {$image->mime}");
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename=\"{$image->filename}\"");
print(stripslashes($image->imagedata));
exit;
Member Avatar
tape enterprise
Junior Poster in Training
59 posts since Feb 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

thanks that was helpful!

Question Answered as of 3 Years Ago by Shanti C, dcdruck and lyrico
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: