nightkarnation Posted July 29, 2008 Share Posted July 29, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/117239-uploading-file-image-to-server-but-renaming-the-image-name/ Share on other sites More sharing options...
dezkit Posted July 29, 2008 Share Posted July 29, 2008 change if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) to if(move_uploaded_file($_FILES['photo']['name'], $target)) Quote Link to comment https://forums.phpfreaks.com/topic/117239-uploading-file-image-to-server-but-renaming-the-image-name/#findComment-603072 Share on other sites More sharing options...
ignace Posted July 29, 2008 Share Posted July 29, 2008 <?php $target = "images/"; list($filename, $extension) = explode('.', basename($_FILES['photo']['name'])); $filename = sha1($filename); $target = $target . $filename . $extension; Quote Link to comment https://forums.phpfreaks.com/topic/117239-uploading-file-image-to-server-but-renaming-the-image-name/#findComment-603073 Share on other sites More sharing options...
nightkarnation Posted July 29, 2008 Author Share Posted July 29, 2008 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/117239-uploading-file-image-to-server-but-renaming-the-image-name/#findComment-603126 Share on other sites More sharing options...
nightkarnation Posted July 29, 2008 Author Share Posted July 29, 2008 Ok cool, i got it... simple dot was missing $target = $target . $filename . "." . $extension; Thanx again Ignace, u've been very helpful ! Quote Link to comment https://forums.phpfreaks.com/topic/117239-uploading-file-image-to-server-but-renaming-the-image-name/#findComment-603145 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.