Page 1 of 1

renaming image file before saving to database

Posted: Fri May 15, 2009 4:11 am
by daiyan
hi
i need to make my php rename the uploaded image before saving it.
my form and php work fine but when 2 image files are uploaded with the same file name then onne gets deleted.
pls can some one show how to rename and then save the uploaded image.
many thanks.


form code:

Code: Select all

 
<html>
<head>
<title></title>
</head><body>[/color]<form enctype="multipart/form-data" action="add.php" method="post"> 
Name: <input name="name" type="text"><br> 
E-mail: <input name="email" type="text"><br> 
Phone: <input name="phone" type="text"><br> 
Photo: <input name="photo" type="file"><br> 
<input value="Add" type="submit"> 
</form></body></html>
 



the php script:

Code: Select all

 
<?php 
 
//This is the directory where images will be saved 
$target = "images/"; 
$target = $target . basename( $_FILES['photo']['name']); 
 
//This gets all the other information from the form 
$name=$_POST['name']; 
$email=$_POST['email']; 
$phone=$_POST['phone']; 
$pic=($_FILES['photo']['name']); 
 
// Connects to your Database 
mysql_connect("", "", "") or die(mysql_error()) ; 
mysql_select_db("") or die(mysql_error()) ; 
 
//Writes the information to the database 
mysql_query("INSERT INTO `employees` VALUES ('id','$name', '$email', '$phone', '$pic')") ; 
 
//Writes the photo to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 
 
//Tells you if its all ok 
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
} 
else { 
 
//Gives and error if its not 
echo "Sorry, there was a problem uploading your file."; 
} 
?>

Re: renaming image file before saving to database

Posted: Fri May 15, 2009 5:05 am
by jaoudestudios
Change this line $target = $target . basename( $_FILES['photo']['name']); to the file name you want to give it.

Re: renaming image file before saving to database

Posted: Fri May 15, 2009 5:23 am
by daiyan
THANKX FOR THE REPLY I WANT THE PHP SCRIPT TO AUTO GENERATE A UNIQUE FILE NAME SO THAT IF 2 SAME NAMED IMAGE FILES ARE UPLOADED ONE WONT OVERWRITE THE OTHER.
SO THAT WHEN THE UPLOADED IMAGE IS SAVED ON THE DATABASE AND THE IMAGE FOLDER IT WILL SAVED WITH A UNIQUE NUMBER OR NAME;

THANKX

Re: renaming image file before saving to database

Posted: Fri May 15, 2009 5:25 am
by jaoudestudios
Shouting is not really going to help!

Re: renaming image file before saving to database

Posted: Fri May 15, 2009 7:12 am
by onion2k
daiyan wrote:THANKX FOR THE REPLY I WANT THE PHP SCRIPT TO AUTO GENERATE A UNIQUE FILE NAME SO THAT IF 2 SAME NAMED IMAGE FILES ARE UPLOADED ONE WONT OVERWRITE THE OTHER.
You need to add something to the filename. How do you think you'd do that?

Re: renaming image file before saving to database

Posted: Fri May 15, 2009 7:56 am
by daiyan
well i dont know how to do php tags.
anyway i have tried al sorts of different methods of the net like using .rand() and many more but nothing seems to quite work.
i don't get any error messages when i run my form it works fine but i wod like it to rename the uploaded image file before saving.
oh yeah i been up for 36 hours straight trying to figure this out and when i have caught up on some sleep i will read the article i looked up briefly just now on "how to use php tags".
iam determined to sort this frig prob out b4 i sleep as i been stuck on this step for 4 days straight .
sorry if i sound lazy trust me its not being lazy it more the case of being dizzy.

Re: renaming image file before saving to database

Posted: Fri May 15, 2009 7:57 am
by daiyan
also the code is posted above

Re: renaming image file before saving to database

Posted: Fri May 15, 2009 8:26 am
by onion2k
I didn't ask how you'd write the code, I asked you what you think you'd need to do in English. Think through the process required, then write code to do each step.

First of all, write down a list of steps that you think you need to do to achieve the result you want.

I'm happy to guide you through the entire process. But you are going to have to do the work to get the code at the end. I'm not going to just hand you a working solution. I don't see any benefit in doing that.

Re: renaming image file before saving to database

Posted: Fri May 15, 2009 10:53 am
by daiyan
ok well i just want to get this finished to hand in on monday. but as for steps basically i am not sure i get you i just want to know how to do this final step.

Re: renaming image file before saving to database

Posted: Fri May 15, 2009 1:39 pm
by onion2k
daiyan wrote:as for steps basically i am not sure i get you i just want to know how to do this final step.
What you're asking for isn't a single step process. You need to:

1. ascertain if there's already a file with the current filename (you'll need to use file_exists() ).
2. if there isn't, save the file (using move() or move_uploaded_file() ).
3. if there is check how many times you've tried to make a new filename, give up and show an error if it's too many (simple counter)
4. work out what you're going to add to the filename (an incrementing counter maybe)
5. work out where you're going to add the extra data (strpos() or strrpos() )
6. add the extra bit (substr() )
7. return to step 1 check the new filename won't overwrite an existing file (a do..while or a while loop)

It's quite complicated. It's not a simple case of generating a string. You could always "cheat" by using a hash like MD5(), but even then you'll need most of the steps above, and you'd end up with nasty filenames.

Re: renaming image file before saving to database

Posted: Sun May 17, 2009 1:21 am
by daiyan
thank you all that helped i managed to get it to work.thankx

Re: renaming image file before saving to database

Posted: Sun May 17, 2009 2:15 am
by Benjamin
Be sure to let us know what your grade was on this assignment.