Renaming image uploads to specific names

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Leao
Forum Commoner
Posts: 49
Joined: Mon Aug 21, 2006 8:57 pm
Location: London

Renaming image uploads to specific names

Post by Leao »

Hi, I'm using a form to upload a jpeg image to my server via PHP (see code). I'd like to rename the jpeg as title.jpg before uploading it to the server and also I'd like to reject the image if it isn't exactly 500x375 pixels.

I've searched the net but can only find a few tutorials about renaming files with random file names in order to avoid overwriting old ones but I actually want to overwrite old image files so I want to rename files to a specific name rather than to a random one. I can't find much either about checking an image's pixel dimensions.

Thanks so much for all your help – leao

Code: Select all

<?php

$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']);
$ok=1;


if ($uploaded_size > 70000)
{
echo "Your file is too large.<br>";
$ok=0;
}

if ($uploaded_type != 'image/jpeg') {
echo "You may only upload JPEG files.<br>";
$ok=0;
}

if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}


else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ".

basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
?>
Last edited by Leao on Wed Aug 23, 2006 3:53 pm, edited 2 times in total.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

First of all, use [ php ] tags.

Now, this:

Code: Select all

if (!($uploaded_type=="image/jpeg"))
should be written like this:

Code: Select all

if ($uploaded_type != 'image/jpeg')
You should actually avoid checking the file type this way. Check out exif_imagetype()

And last one... What you want to do can be done like this:

Code: Select all

move_uploaded_file($_FILES['uploaded']['tmp_name'], 'path/file_name.file_type')
Leao
Forum Commoner
Posts: 49
Joined: Mon Aug 21, 2006 8:57 pm
Location: London

Post by Leao »

Hi, thanks for correcting the script. Any ideas regarding changing the image name on upload and checking the file's pixel dimensions?

Many thanks
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Leao wrote:Any ideas regarding changing the image name on upload
I've already answered to that one.
Leao wrote:and checking the file's pixel dimensions?
getimagesize()

If you are going to call getimagesize() then you can use it to get the file type instead of using exif_imagetype() :wink:
paladaxar
Forum Commoner
Posts: 85
Joined: Fri Jun 18, 2004 11:50 pm

Post by paladaxar »

Dont forget about good ol' rename() :)
Leao
Forum Commoner
Posts: 49
Joined: Mon Aug 21, 2006 8:57 pm
Location: London

Post by Leao »

Hi
Oren wrote:I've already answered to that one.
Yup, forgive me I entered the data incorrectly. It works now.

I tried the getimagesize() function but it didn't seem to work. Even if the image was exactly 500 pixels in width I still got the 'Your file is not the correct width...' error message and also
PHP Warning: getimagesize(mountain.jpg): failed to open stream: No such file or directory in C:\hshome\stpatric\yourdomain.org\test\upload.php on line 7
The php I used was as follows:

Code: Select all

list($width, $height) = getimagesize($_FILES['uploaded']['name']) ; 

if ($width != 500)
{
echo "Your file is not the correct width of 500 pixels.<br>";
$ok=0;
}
Cheers leo
Leao
Forum Commoner
Posts: 49
Joined: Mon Aug 21, 2006 8:57 pm
Location: London

Post by Leao »

I managed to do it with the following code:

Code: Select all

list($ImportWidth,$ImportHeight,$ImageMimeType) = getimagesize($_FILES['uploaded']['tmp_name']) ;

if ($ImportWidth != 500)
{
echo "Your file is not the correct width of 500 pixels.<br>";
$ok=0;
}
Thanx
Post Reply