Image Upload - SORTED

I have created an image upload page, everything is working fine for one :wink:

What I want to do is rename the file that has been uploaded, does anybody know how I can do this.

Here is the code I am using


<?php if($_POST['upload']) { # edit # $maxwidth = 350; $maxheight = 250; $max_filesize = 102400; $uploads = 'uploads/a001'; $types_array = array('image/gif','image/pjpeg','image/x-png'); # end edit # if($_FILES['image']['name'] == "") { echo "Please select a file to upload!n"; exit; } if(!in_array($_FILES['image']['type'], $types_array)) { echo "That file type is not allowed!n"; exit; } $max_filesize_kb = ($max_filesize / 1024); if($_FILES['image']['size'] > $max_filesize) { echo "You file is too large, files may be up to ".$max_filesize_kb."kbn"; exit; } $imagesize = getimagesize($_FILES['image']['tmp_name']); $imagewidth = $imagesize[0]; $imageheight = $imagesize[1]; if($imagewidth > $maxwidth || $imageheight > $maxheight) { echo "You file is too large, files may be up to ".$maxwidth."px x ".$maxheight."px in sizen"; exit; } move_uploaded_file($_FILES['image']['tmp_name'], $uploads.'/'.$_FILES['image']['name']) or die ("Couldn't upload ".$_FILES['image']['name']."n"); } ?>


and in the body tags



I think you will need to take and get the extention and store in a variable then do something along the lines of…

move_uploaded_file(“mynewfilename” . $extention, …);

I think you will need to associate the mime type with a particular extention.

case “image/gif”:
$extention = “.gif”;

case “image/pjpeg”:
$extention = “.jpg”;

Something like is what I have used in the past.

Hope that helps.

Hi and thanks for your reply

I am not looking to change the file extension just the file name i.e

something.jpg to img1.jpg

This is so I can place an echo tag in an image slideshow without changing the code everytime I change am image

That’s exactly what Ragster is showing you: how to change the filename and keep the extension.


In the file name it contains the extension. If you go and just try to change the filename you will lose the extension unless you do mynewfile.jpg. But how is the application supposed to know, or you for that matter, what the previous extension was. You can’t so you have to look at the files mime type to know the extension and then add it in when the file is being copied to the server.

Hope that clarifies things.

I appolgise if it looks like i’m ignoring what people are saying its not intentional.

I’ve managed to sort it for now by changing
move_uploaded_file($_FILES[‘image’][‘tmp_name’], $uploads.’/’.$_FILES[‘image’][‘name’])


move_uploaded_file($_FILES[‘image’][‘tmp_name’], ‘uploads/a001/img1.jpg’)

Trouble is I cant upload more than one file. How would I go about doing this for say 6 image files. I also changed

$types_array = array(‘image/gif’,‘image/pjpeg’,‘image/x-png’);


$types_array = array(‘image/pjpeg’);

so only jpg files can be uploaded

Ok, for one why didn’t the solution I give you work?

Secondly… This question is being dealt with in another post you created.

Hi Ragster00

Thanks for your reply, I am new to php, have done a bit of programming using visual basic but not much. To be honest I didnt understand what you said. I had a look around to see what it meant this is where i found the other snippet which i messed around with.

Its all to easy for people to ask questions and expect others like yourself to give the answers but I like to know what things mean and what they do.

Sorry if I have offended you

Just so you know there was no offense taken.

Sponsor our Newsletter | Privacy Policy | Terms of Service