Welcome to WebmasterWorld Guest from 35.175.120.174

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Image file upload and retrieval

Using MySql to store path

     
11:04 am on Feb 17, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:June 13, 2002
posts:108
votes: 0


I am presently storing all my images in BLOB fields in the db but this is making the db a bit "heavy". I want to upload my images using copy and also place its path in the db to be able to retrieve it.

Do I use a variable which tags on the file name from the directory? What is the neatest way to produce a form to upload and place the path in the db in the same action?

4:12 pm on Feb 17, 2003 (gmt 0)

New User

10+ Year Member

joined:Jan 8, 2003
posts:24
votes: 0


How about uploading your images to a pre-defined folder and just store the name of the image in your database?

The upload form:
<form enctype="multipart/form-data" action="upload.php" method="post" name="upload_file">
<input type="hidden" name="MAX_FILE_SIZE" value="5000000">
<input name="userfile" type="file">
<input type="submit" value="Upload" name="file_uploaded">
</form>

upload.php:
<?php
// file Upload - In PHP 4.1.0 or later, $_FILES should be used instead of $HTTP_POST_FILES.

if (is_uploaded_file($_FILES['userfile']['tmp_name']))
{
//UPLOAD
$file_realname = $_FILES['userfile']['name'];

//PUT IN FILDER
copy($_FILES['userfile']['tmp_name'], "path/to/folder/goes/here/$file_realname");
}
else
{
echo "<b><font color=red>No file uploaded.</font></b><BR>No file available or file too big to upload.";
}
?>

$file_realname can be saved in database.

Be Positive

4:30 pm on Feb 17, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:June 13, 2002
posts:108
votes: 0


Thanks BP

I have managed to do it this morning but I think the way I am getting the image to display is a bit clunky! Basically I have saved the path to the image in the db at the same time as adding the file name

i.e. sent to db as:

$sql="insert into mytable(picture) values
('<img src=\"images/$file_name\">')";

and then I just query the db and echo what is already html.

This probably isn't the done thing, it works but is there a better way of retreiving from the db?

I am also looking into resizing of the image to display a consistent size despite what the user has uploaded - any ideas?

11:38 pm on Feb 17, 2003 (gmt 0)

New User

10+ Year Member

joined:Jan 8, 2003
posts:24
votes: 0


cyclic,

You can just save the image file name in the database:
$sql="insert into mytable(picture) values
($file_name)";

Have the rest (<img src=...) in your HTML file:
<img src="<? echo ($file_name);?>" ...

And for you consistent size:
<img src="<? echo ($file_name);?>" width="100" height="150">

You can put any number you like for width & height.

If you need all the images to look good you can upload images with any dimentions but width & height should have the same ratio, like 1 to 2 or ...

If not they are going to look streched from width OR height.

Be Positive

9:47 am on Feb 18, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:June 13, 2002
posts:108
votes: 0


Thanks again BP

I thought I had taken the tortuous route to displaying the image!

My next challenge is to set up a delete of the image file from the directory at the same time as deleting the record from the database. The latter I have set up already, it is now a case of combining the two.

3:53 pm on Feb 18, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 22, 2002
posts:2546
votes: 0


>>>And for you consistent size:
<img src="<? echo ($file_name);?>" width="100" height="150">

I would not recommend that route for resizing because the file size does not change.

Suppose you upload from a floppy you just took out of your digital camera. My camera puts out a 640 X 480 pixel image and it is pretty slow loading on a dial-up connection. Resizing it in you html img tag will make it display how you want, but it will still load slow.

I just went through making a jpeg upload page for two clients. My "pics" table has three fields:


1)pic_id(INT)
2)description(varchar)
3)cat_id

The "cat_id" is a cross-reference to my "cats" table, which allows me to control where the pic is displayed.

I start with the upload form:


<form enctype="multipart/form-data" action="upload.php" method="post">
<input name="pic" type="file" />
<input type="submit" value="Upload Now" />
</form>

Then, in upload.php I query the "pics" table for the highest "pic_id" and then add 1 to that value. This will be the new pic's id and file name.


$sql = "SELECT pic_id FROM pics ORDER BY pic_id DESC LIMIT 1";
$results = mysql_query($sql);
while ($e = mysql_fetch_array($results)){
$new_pic = $e[prod_id] + 1;
}

Now you move the file from the temp directory to it's destination and name it($new_pic).


$uploaddir = '/home/your_path/public_html/images/';
if (move_uploaded_file($_FILES['pic']['tmp_name'], $uploaddir . $new_pic)) {
} else {
print File did not upload!";
}

Now that I have the file named and saved where I want it, I grab it and make a thumbnail version:


$sourcefile = "$uploaddir$new_pic";
$picsize=getimagesize("$sourcefile");
$source_x = $picsize[0];
$source_y = $picsize[1];
if ($source_x > $source_y){
$dest_x = 175;
$dest_y = 140;
} else {
$dest_x = 140;
$dest_y = 175;
}
$targetfile = "$uploaddir$new_pic";
$jpegqual = 75;

$source_id = imageCreatefromjpeg("$sourcefile");

$target_id=imagecreate($dest_x, $dest_y);
$target_pic=imagecopyresized($target_id,$source_id,0,0,0,0,$dest_x,$dest_y,$source_x,$source_y);
imagejpeg ($target_id,"$targetfile",$jpegqual);

Go to php.net(image functions) [php.net] for detailed descriptions of these functions.

Sorry for the length of this post:)

4:19 pm on Feb 18, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:June 13, 2002
posts:108
votes: 0


Thanks, I have looked up the descriptions and will incorporate this into my script. This would not be a problem if my client would upload the size I tell him!

I am still figuring out how to delete the file at the same time as the record - time for some fresh air....

5:05 pm on Feb 18, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 22, 2002
posts:1782
votes: 0


>I query the "pics" table for the highest "pic_id" and then
>add 1 to that value. This will be the new pic's id and file
>name.

Id use an auto_increment column for that. Saves the hassle of doing that manually.

Andreas

5:19 pm on Feb 18, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 22, 2002
posts:2546
votes: 0


Good point Andreas.

>>>This would not be a problem if my client would upload the size I tell him!

Ha! Don't we wish!

Also, that script only resizes the original image. I had said that it made a thumbnail version. To keep the large and thumbnail versions, change $targetfile.

7:31 pm on Feb 18, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:June 13, 2002
posts:108
votes: 0


Brilliant, thanks guys. I had used auto-increment in my first attempt so that should keep things neat.

Im still a bit stuck on the deletion of the file, despite the fresh air! I can delete the record fro the database no probs but I want to tie that in with deleting the corresponding image file from the directory. I can do each separately but they may be beyond my customer!

7:46 pm on Feb 18, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 22, 2002
posts:2546
votes: 0


You never mentioned deleting files. Try unlink($filename) [php.net].

Oops! Just read message #7. Sorry ;)

9:45 pm on Feb 18, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Oct 8, 2002
posts:65
votes: 0


>>>This would not be a problem if my client would upload the size I tell him!

So check the file size, and image dimensions. Reject if it's too big. Php can handle that, all the information is there.

if (!($info = getimagesize($_FILES["image"]['tmp_name']))) 
echo "Could not get information from temporary image.";

if ($image[0]>800  $image[1]>600) 
echo "Image dimensions are too large.";