lobski Posted July 24, 2008 Share Posted July 24, 2008 I'm looking to find out how to rename uploaded image files, into "img_1" "img_2" and so on. Here is my code: <title>Finished!</title> <?php $target_path = "uploads/"; if(getimagesize($_FILES['uploadedfile']['tmp_name'])) { $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "Upload successful. The link to your image is: http://absolutely-corrupt.com/" .$target_path . "<br />" ."<br />". "Signature code: <FONT size=4>[img=http://absolutely-corrupt.com/" . "$target_path" . "<FONT size=4>]"; } else{ echo "There was an error uploading the file, please try again!"; } } else { die("You may only upload image files!"); } ?> Link to comment https://forums.phpfreaks.com/topic/116369-renaming-uploaded-image-files/ Share on other sites More sharing options...
JonnyThunder Posted July 24, 2008 Share Posted July 24, 2008 if you're storing image upload details in a database - you can use AutoIncrement to generate a unique ID for every new record - then use that number as your filename. Link to comment https://forums.phpfreaks.com/topic/116369-renaming-uploaded-image-files/#findComment-598372 Share on other sites More sharing options...
lobski Posted July 24, 2008 Author Share Posted July 24, 2008 if you're storing image upload details in a database - you can use AutoIncrement to generate a unique ID for every new record - then use that number as your filename. Unfortunately, I am not. What can I do to implement a simple renaming feature to my current code? Link to comment https://forums.phpfreaks.com/topic/116369-renaming-uploaded-image-files/#findComment-598374 Share on other sites More sharing options...
JonnyThunder Posted July 24, 2008 Share Posted July 24, 2008 The renaming bit is easy - its how you're going to get a unique ID which is tricky part. To rename it, simply use $target_path . "/yourfilenamehere.xxx" when using move_uploaded_file. You could update a 'counter' file or something to store the last ID but DB is safer. Link to comment https://forums.phpfreaks.com/topic/116369-renaming-uploaded-image-files/#findComment-598377 Share on other sites More sharing options...
lobski Posted July 24, 2008 Author Share Posted July 24, 2008 Could you show me an example of this? Link to comment https://forums.phpfreaks.com/topic/116369-renaming-uploaded-image-files/#findComment-598379 Share on other sites More sharing options...
JonnyThunder Posted July 24, 2008 Share Posted July 24, 2008 Hows about this instead (assuming you'll never remove files from this directory).... $dir = "/myfiledirectory"; $dh = opendir($dir); $totalfiles = 0; while (false !== ($filename = readdir($dh))) { if ($filename != "." && $filename != "..") { $totalfiles++; } } The above will give you the total files (variable $totalfiles) in the directory, which you could use as your ID. As long as no files are removed, this will work. DB is still the way to go though. Link to comment https://forums.phpfreaks.com/topic/116369-renaming-uploaded-image-files/#findComment-598382 Share on other sites More sharing options...
lobski Posted July 24, 2008 Author Share Posted July 24, 2008 Hows about this instead (assuming you'll never remove files from this directory).... $dir = "/myfiledirectory"; $dh = opendir($dir); $totalfiles = 0; while (false !== ($filename = readdir($dh))) { if ($filename != "." && $filename != "..") { $totalfiles++; } } The above will give you the total files (variable $totalfiles) in the directory, which you could use as your ID. As long as no files are removed, this will work. DB is still the way to go though. Hmm, I'm not quite sure where to insert this into my code. Link to comment https://forums.phpfreaks.com/topic/116369-renaming-uploaded-image-files/#findComment-598394 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.