How to upload and rename image to folder...
Moderator: General Moderators
How to upload and rename image to folder...
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!
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!
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Yes. Here is the code I have as of now.
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
<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;;
}
?>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
andCode: 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]hi,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!!
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
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.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Errmmm.... I think he needed a dynamic renaming system. This system just uses a predefined name.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);
He clearly stated in his post that "The upload folder has 10 jpg images named in sequence from 01.jpg to 10.jpg".d11wtq wrote:
Errmmm.... I think he needed a dynamic renaming system. This system just uses a predefined name.
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.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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
This does not rename to .jpg but I figured it would be more convenient to do this.
"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