How to upload and rename image to folder...

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
sethwise
Forum Newbie
Posts: 3
Joined: Thu Mar 10, 2005 3:55 pm

How to upload and rename image to folder...

Post by sethwise »

Hi everyone. I'm new to this forum so let me say in advance I greatly appreciate any help I can get with this problem, and I hope I have the chance to repay the favor. I also hope I'm not posting this in the wrong area.

I have an admin page I'm setting up for a simple personal profile site. I want the user to be able to upload images which will be displayed on their site. Normally I do this in mySQL, but in this case I'm uploading the pictures to a folder on the server. The admin page has 10 file input fields with the current image displayed above each one. The user selects their image, clicks submit (there is a submit button for each file), and the page reloads with that image updated.

So here's where my problem lies... The upload folder has 10 jpg images named in sequence from 01.jpg to 10.jpg. When the user uploads their image, after php verification of the file type (.jpg) I can't figure out how to have the file upload to the folder with the specific name (rename to 01.jpg or 02.jpg), overwriting the existing image.

Thanks again for any help. I've heard great things about this forum!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Do you have some code so far... it's quite simple to do. I could give you an example if you don't have anything to work with.

If you have some code, that we can just modify or give tips on that would be better on both sides I guess :wink:
sethwise
Forum Newbie
Posts: 3
Joined: Thu Mar 10, 2005 3:55 pm

Post by sethwise »

Yes. Here is the code I have as of now.

Code: Select all

<form name=&quote;img1upload&quote; method=&quote;post&quote; action=&quote;editimg1.php&quote; enctype=&quote;multipart/form-data&quote;>
<input type=&quote;hidden&quote; name=&quote;MAX_FILE_SIZE&quote; value=&quote;200000&quote; />
<input type=&quote;file&quote; name=&quote;image1&quote; />
</form>

Code: Select all

<?php

if(is_uploaded_file($_FILESї'image1']ї'tmp_name'])){

	if($File_type == &quote;image/jpeg&quote; or $File_type == &quote;image/jpg&quote;){
  
		move_uploaded_file($_FILESї'image1']ї'tmp_name'], &quote;./pics/&quote;.$_FILESї'image1']ї'name']);

	}
	
	else{

		echo &quote;Image must be in a jpeg (.jpg) format!&quote;;

	}
}

else{

	echo &quote;No image was uploaded!&quote;;

}

?>
I have posted an example of the main page (with the form) at:
http://www.freestylepro.com/temp/temp/images.php

I guess I have 2 additional questions.
1) On my main page, I have 6 forms, all in the same format as the code above, but with different names representing each of the images (image1, image2, etc.). I don't mind having a different file to process each form request, but since there is only a difference with one part of the code, there has to be a more efficient way where I can use one file instead of multiple, and just pass the variable from whichever form is being used. This definitely isn't as urgent, but I would welcome any suggestions.

2) If someone tries uploading a jpg file with jpeg extension, I'd like to change it to jpg. Is that easy to do with the image renaming?

Lastly, I'm not committed to this code; it may in fact be very poor. If there's a better way to do it I'm all ears.

Thanks!!


feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

watch it with the double posts... :?
sethwise
Forum Newbie
Posts: 3
Joined: Thu Mar 10, 2005 3:55 pm

Post by sethwise »

Woops... Sorry. Won't happen again.
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post by hongco »

sethwise wrote:
I guess I have 2 additional questions.
1) On my main page, I have 6 forms, all in the same format as the code above, but with different names representing each of the images (image1, image2, etc.). I don't mind having a different file to process each form request, but since there is only a difference with one part of the code, there has to be a more efficient way where I can use one file instead of multiple, and just pass the variable from whichever form is being used. This definitely isn't as urgent, but I would welcome any suggestions.

2) If someone tries uploading a jpg file with jpeg extension, I'd like to change it to jpg. Is that easy to do with the image renaming?

Lastly, I'm not committed to this code; it may in fact be very poor. If there's a better way to do it I'm all ears.

Thanks!!
hi,
1 - to use with 1 single file for multiple upload, the name="img1upload" should be an array. In your form, use looping, and also use loop to go through each uploaded file in your php code.

2- get the file, example pic.jpeg. From here you can separate the name from the extension. when copying the temp file to your host, the target file should contain the name of the file (without the original ext) concatinated with your desired extension.

3- to verify if the file is truly images, as feyd told me, check the image size. if the file is not image, you won't get any size from it

:)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

To rename the file, just change the second parameter in move_uploaded_file () to whatever you want. There's nothing saying you need to use the filename sent from the client.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

To rename your file is simple

Code: Select all

$filename = $_POST['image1']['tmp_name'];
// get $extention and other checks here
$filename2 = "01.$extension"; // If you want to use predefined names.
$time = time(); $filename2 = $time.$extention; // If you want to use a dynamic renaming system
$dest = "/folder/destination/$filename2";
move_uploaded_file($filename, $dest);
Last edited by s.dot on Wed Mar 16, 2005 7:57 am, edited 2 times in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

scrotaye wrote:To rename your file is simple

Code: Select all

$filename = $_POST['image1']['tmp_name'];
$filename2 = "01.ext";
$dest = "/folder/destination/$filename2
move_uploaded_file($filename, $dest);
Errmmm.... I think he needed a dynamic renaming system. This system just uses a predefined name. :roll:
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

d11wtq wrote:
Errmmm.... I think he needed a dynamic renaming system. This system just uses a predefined name. :roll:
He clearly stated in his post that "The upload folder has 10 jpg images named in sequence from 01.jpg to 10.jpg".

That is why I just put 01.ext.

And it was an example that would need tweaking, just to give him an idea of what was needed to be done. Not a script that should be copied and pasted as he would have to check for filetype and extension before it could be used.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

OK sorry, no offense intended... pickle mentioned the same thing as you too :)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

No offense taken. :). I edited my post and provided options.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Oh, In addition

"2) If someone tries uploading a jpg file with jpeg extension, I'd like to change it to jpg. Is that easy to do with the image renaming?"

Easy

Code: Select all

$filename = $_POST['image1']['tmp_name']; // get file name
$extension = strtolower(strrchr($filename,".")); // get everything after the last . and include the . Also lowercases the string for easier file checking
$extensions = array('.gif','.jpg','.jpeg');  // Allowed File Types 
if (!in_array($extension, $extensions)) { echo "File type not allowed."; } // Check to see if the file is allowed
$time = time(); // dynamic renaming
$filename2 = $time.$extension // final file name with correct extension
This does not rename to .jpg but I figured it would be more convenient to do this.
Post Reply