Page 1 of 1
How to upload and rename image to folder...
Posted: Thu Mar 10, 2005 4:16 pm
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!
Posted: Thu Mar 10, 2005 4:34 pm
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

Posted: Sun Mar 13, 2005 8:55 pm
by sethwise
Yes. Here is the code I have as of now.
Code: Select all
<form name="e;img1upload"e; method="e;post"e; action="e;editimg1.php"e; enctype="e;multipart/form-data"e;>
<input type="e;hidden"e; name="e;MAX_FILE_SIZE"e; value="e;200000"e; />
<input type="e;file"e; name="e;image1"e; />
</form>
Code: Select all
<?php
if(is_uploaded_file($_FILESї'image1']ї'tmp_name'])){
if($File_type == "e;image/jpeg"e; or $File_type == "e;image/jpg"e;){
move_uploaded_file($_FILESї'image1']ї'tmp_name'], "e;./pics/"e;.$_FILESї'image1']ї'name']);
}
else{
echo "e;Image must be in a jpeg (.jpg) format!"e;;
}
}
else{
echo "e;No image was uploaded!"e;;
}
?>
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
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Sun Mar 13, 2005 9:03 pm
by feyd
watch it with the double posts...

Posted: Tue Mar 15, 2005 5:19 pm
by sethwise
Woops... Sorry. Won't happen again.
Posted: Tue Mar 15, 2005 5:31 pm
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

Posted: Tue Mar 15, 2005 6:28 pm
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.
Posted: Wed Mar 16, 2005 7:48 am
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);
Posted: Wed Mar 16, 2005 7:52 am
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.

Posted: Wed Mar 16, 2005 7:55 am
by s.dot
d11wtq wrote:
Errmmm.... I think he needed a dynamic renaming system. This system just uses a predefined name.

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.
Posted: Wed Mar 16, 2005 7:57 am
by Chris Corbyn
OK sorry, no offense intended... pickle mentioned the same thing as you too

Posted: Wed Mar 16, 2005 7:58 am
by s.dot
No offense taken.

. I edited my post and provided options.
Posted: Wed Mar 16, 2005 8:07 am
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.