Chicken Posted September 29, 2007 Share Posted September 29, 2007 I'm trying to upload and rename some files, renaming them by the form used to upload them (a hidden input). So far I've gotten the uploading to work but I'm a little confused as how to rename the uploaded file. This is what I have so far: My submit form <html> <body> <form enctype="multipart/form-data" action="upload.php" method="POST"> Please choose a file: <input name="uploaded" type="file" /> <input type="hidden" name="name" value="00000" /> <br> <input type="submit" value="Upload" /> </form> </body> </html> My upload.php <?php $target = "uploads/"; $target = $target . basename( $_FILES['uploaded']['name']) ; if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { echo "The file was uploaded"; } else { echo "Problem during upload"; } ?> I am not concerned about file size, or anything like that, and the file types are all txt Quote Link to comment https://forums.phpfreaks.com/topic/71131-upload-and-rename/ Share on other sites More sharing options...
hemlata Posted September 29, 2007 Share Posted September 29, 2007 Hello, Simply give the new name while uploading file as... $target = $target . $new_file_name; and will save the uploaded file with the new name that you want to save with. Hope this will solve your issue. Regards, Quote Link to comment https://forums.phpfreaks.com/topic/71131-upload-and-rename/#findComment-357768 Share on other sites More sharing options...
Chicken Posted September 29, 2007 Author Share Posted September 29, 2007 eh, I'm getting an error still this is what my new upload.php looks like: <?php $name = $_REQUEST["name"]; $target = "uploads/"; $target = $target . $name; if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { echo "The file was uploaded"; } else { echo "Problem during upload"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71131-upload-and-rename/#findComment-357865 Share on other sites More sharing options...
BlueSkyIS Posted September 29, 2007 Share Posted September 29, 2007 eh, I'm getting an error still What is the error? Quote Link to comment https://forums.phpfreaks.com/topic/71131-upload-and-rename/#findComment-357898 Share on other sites More sharing options...
Chicken Posted September 29, 2007 Author Share Posted September 29, 2007 when i try to upload it says Problem during uploading and the only thing i changed was the name Quote Link to comment https://forums.phpfreaks.com/topic/71131-upload-and-rename/#findComment-357903 Share on other sites More sharing options...
BlueSkyIS Posted September 29, 2007 Share Posted September 29, 2007 is $target writable? I would avoid using relative paths and use absolute paths instead: uploadto("./relative_path/somefile.jpg"); // Bad uploadto($_SERVER['DOCUMENT_ROOT']."/relative_path/somefile.jpg"); // No ambiguity. Quote Link to comment https://forums.phpfreaks.com/topic/71131-upload-and-rename/#findComment-357915 Share on other sites More sharing options...
Chicken Posted September 29, 2007 Author Share Posted September 29, 2007 $target is writable i used the first script to successfully upload Quote Link to comment https://forums.phpfreaks.com/topic/71131-upload-and-rename/#findComment-357978 Share on other sites More sharing options...
Chicken Posted September 30, 2007 Author Share Posted September 30, 2007 anyone? Quote Link to comment https://forums.phpfreaks.com/topic/71131-upload-and-rename/#findComment-358190 Share on other sites More sharing options...
pocobueno1388 Posted September 30, 2007 Share Posted September 30, 2007 Take a look at this tutorial http://php.about.com/od/advancedphp/ss/rename_upload.htm It shows you exactly how to do it. Google brings up a lot of different tutorials, so you might want to try that if the tut I gave you doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/71131-upload-and-rename/#findComment-358192 Share on other sites More sharing options...
xylex Posted September 30, 2007 Share Posted September 30, 2007 Start by adding error_reporting(E_ALL); to the beginning of your code, and debug from there. Quote Link to comment https://forums.phpfreaks.com/topic/71131-upload-and-rename/#findComment-358311 Share on other sites More sharing options...
hemlata Posted October 1, 2007 Share Posted October 1, 2007 Hello, What is the value of $target you are getting. Since the following code is running successfully on my end.. <?php // new name for the uploaded file test.jpg $name = 'test1.jpg'; $target = "uploads/"; $target = $target . $name; if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { echo "The file was uploaded"; } else { echo "Problem during upload"; } ?> Regards, Quote Link to comment https://forums.phpfreaks.com/topic/71131-upload-and-rename/#findComment-358958 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.