Showing results for 
Search instead for 
Did you mean: 

PHP MySQL and Uploading jpegs or Gifs

Posts: 7
Registered: 21-08-2007

PHP MySQL and Uploading jpegs or Gifs

I usualy use codewalkers for this sort of thing but they have changed their site and its giving me head aches now.
I'm trying to write a page that will allow users to upload their images to their profile page. I've seen a few ways to go about this but its not as simple as I first thought.
There are probably pages out there that would explain this but I have search and have had no joy.
General briefing:

  • Users can logon and upload/delete an image through there "edit profile" page.
    Public would be able to view users page and image displayed as a thumbnail with the option to expand the image by clicking the image. 

I have thought about using the database to store the code for each image. I'm not sure that this is the best solution.
I have also thought about the possibility of sending the image file to a folder on the server along with the appropriate link, stored in the users MySQL profile field.
If anyone has successfully managed to write such a page I would appreciate your wise knowlegde on this subject or a link to a page that can help.
Posts: 393
Registered: 26-07-2007

Re: PHP MySQL and Uploading jpegs or Gifs

I have never understood the pros's and cons of storing images in a database vs as a file.
I usually store images as files and as you mentioned just include the link to the image in the database although if you rename the image to the user's id you don't even need to store that.
The code below is from an image upload script i use, it creates 2 smaller versions renaming the files along the way and eventually storing details in the database, you should be able to pull something useful out of it.
	if (!empty($_FILES['image']['name'])) {
$image = (!empty($_FILES['image']['name']));
$image_attr = "";
$thumb_image_attr = "";
$thumb_image = "";
} else {
$error .= "No Image Specified<br />\n";

#if there are no other errors, process the image
if (empty($error)) {
$image_tempname = $_FILES['image']['name']; #get filename
$ImageDir = "D:/htdocs/uploads/images/";
$ImageName = $ImageDir . $image_tempname;
if (move_uploaded_file($_FILES['image']['tmp_name'], $ImageName)) {
#add watermark
#imagettftext($ImageName, 8, 0, 5, 5, 0, "arial.ttf", "a star solutions");
#get information about the image being uploaded
list($width, $height, $imgtype, $attr) = getimagesize($ImageName);

switch ($imgtype) {
case 2:
$ext = ".jpg";
$error .= "You can only upload jpg files<br />";
unlink($ImageName); # delete file
#generate smaller images and move to correct locations
if (empty($error)) {
$Dir = "D:/htdocs/images/products/";
$WebDir = "/images/products/";

$newfilename = $Dir . $id . $ext;
$newthumbfilename = $Dir . $id . "_thumb" . $ext;
$newsmallfilename = $Dir . $id . "_small" . $ext;

$thumb_width = 150;
$thumb_ratio = ($width / $thumb_width);
$thumb_height = ($height / $thumb_ratio);

$small_width = 50;
$small_ratio = ($width / $small_width);
$small_height = ($height / $small_ratio);

if (file_exists($newfilename)) {
if (file_exists($newthumbfilename)) {
if (file_exists($newsmallfilename)) {

#create thumbnail
$largeimage = imagecreatefromjpeg($ImageName); #Create image from uploaded image

$thumb = imagecreatetruecolor($thumb_width,$thumb_height);

$small = imagecreatetruecolor($small_width,$small_height);
imagedestroy($largeimage); #Remove temp image

#rename large image - move from admin dir
rename($ImageName, $newfilename);

#get attr details for the two small images
list($width, $height, $imgtype, $thumb_attr) = getimagesize($newthumbfilename);
list($width, $height, $imgtype, $small_attr) = getimagesize($newsmallfilename);

#get the dir details for the database
$ImgDir = $WebDir . $id . $ext;
$thumbImgDir = $WebDir . $id . "_thumb" . $ext;
$smallImgDir = $WebDir . $id . "_small" . $ext;
$sql =  "UPDATE products " .
"SET product_image='" . $ImgDir .
"', product_image_attr='" . urlencode($attr) .

"', product_thumb_image='" . $thumbImgDir .
"', product_thumb_image_attr='" . urlencode($thumb_attr) .

"', product_small_image='" . $smallImgDir .
"', product_small_image_attr='" . urlencode($small_attr) .
"', product_date_updated='" . date("Y-m-d H:i:s",time()) .
"', product_user_updated='" . $_SESSION['user_id'] .
"' WHERE product_id=" . $id;
mysql_query($sql, $conn) or die(mysql_error() . "<br />" . $sql);
Posts: 7
Registered: 21-08-2007

Re: PHP MySQL and Uploading jpegs or Gifs

Thanks for that. I'll give it a wirl. Cant see it being completed in the next day or so but what i see seems to contain the answers.
Thanks again