4
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by tape enterprise
Featured Replies
  • [QUOTE=lyrico;1438948]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.[/QUOTE] That's one way to do it, but you can also save the actual image in the database in a BLOB … Read More

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.

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;
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.