Forum Moderators: coopster
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?
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
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?
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
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
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:)
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!
Oops! Just read message #7. Sorry ;)
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.";