Jump to content

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 ?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.