Jump to content

UPLOADING FILE (IMAGE) TO SERVER - BUT RENAMING THE IMAGE NAME...


nightkarnation

Recommended Posts

Ok...the title says it all.

Here's the working code to upload an image to my localhost server...but what i want to do and cant seem to understand is to rename the uploaded image...

ex: lets say the image about to be uploaded is called "test.jpg" and when it uploads to my localhost server i want it saved as: "user1.jpg" instead of "test.jpg"

 

CODE:

 

<body>

<form enctype="multipart/form-data" action="add.php" method="POST"> 
Name: <input type="text" name="name"><br> 
E-mail: <input type="text" name = "email"><br> 
Phone: <input type="text" name = "phone"><br> 
Photo: <input type="file" name="photo"><br> 
<input type="submit" value="Add"> 
</form>

<?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 Database 
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("learning") or die(mysql_error()); 

//Writes the information to the database 
mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$pic')") ; 

//Writes the photo to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 

//Tells 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."; 
} 
?> 

</body>

 

Any ideas? looking forward for your reply

Thanx!

Thanx for the reply Ignace, very helpful !!

 

Its working now but theres only one problem left...the file that is being saved on the server, is not the actual image...its giving a long name "9c914babd6813dcc1051a77f015cef3ab28b6d89jpg" and of course is not recognized as an image.

 

any ideas of what it could be ?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.