npsari Posted September 15, 2007 Share Posted September 15, 2007 Hi there, I am using this code to rename uploaded images: $target_path = "images/"; $target_path = $target_path . basename( $_FILES['Image1']['name']); $_FILES['Image1']['tmp_name']; $target_path = "images/"; $filename = basename( $_FILES['Image1']['name']); $filename = time() . '_' . $filename; $target_path .= $filename; if(move_uploaded_file($_FILES['Image1']['tmp_name'], $target_path)) { echo "Image was uploaded successfully!"; } else{ print"Image could not be uploaded!"; } So, if the image is called.. ME.jpg > Becomes > 1285035030_ME.jpg ROOM.gif > Becomes > 1186085030_ROOM.gif But i want all the images to be called UPLOADED AND to keep the extension of the image only... ROOM.jpg > Becomes > 1285535030_UPLOADED.jpg ROOM.gif > Becomes > 1185036030_UPLOADED.gif Can you show me how to change this code? Quote Link to comment https://forums.phpfreaks.com/topic/69449-solved-rename-uploaded-images/ Share on other sites More sharing options...
Wuhtzu Posted September 15, 2007 Share Posted September 15, 2007 First of all the beginning of your code is pretty weird: <?php $target_path = "images/"; $target_path = $target_path . basename( $_FILES['Image1']['name']); $_FILES['Image1']['tmp_name']; $target_path = "images/"; ?> You overwrite your $target_path variable twice... why? Quote Link to comment https://forums.phpfreaks.com/topic/69449-solved-rename-uploaded-images/#findComment-348950 Share on other sites More sharing options...
Wuhtzu Posted September 15, 2007 Share Posted September 15, 2007 Secondly, this is the part you need to change: <?php $filename = basename( $_FILES['Image1']['name']); $filename = time() . '_' . $filename; $target_path .= $filename; ?> Just set $filename = 'UPLOADED'; <?php $filename = 'UPLOADED'; $filename = time() . '_' . $filename; $target_path .= $filename; ?> Quote Link to comment https://forums.phpfreaks.com/topic/69449-solved-rename-uploaded-images/#findComment-348951 Share on other sites More sharing options...
npsari Posted September 15, 2007 Author Share Posted September 15, 2007 Cool, However, in this case, the extension of the image will be gone i want only the extension to stay so, it should be like UPLOADED.jpg Quote Link to comment https://forums.phpfreaks.com/topic/69449-solved-rename-uploaded-images/#findComment-348952 Share on other sites More sharing options...
Wuhtzu Posted September 15, 2007 Share Posted September 15, 2007 <?php //Since you redeclare $taget_path later this does nothing $target_path = "images/"; //Since you redeclare $taget_path later this does nothing too $target_path = $target_path . basename( $_FILES['Image1']['name']); //This does nothing... $_FILES['Image1']['tmp_name']; //Here you "reset" $target_path to "images/" - this is why the previous lines does nothing $target_path = "images/"; //Here you assing the original filename to $filename $filename = basename( $_FILES['Image1']['name']); //Here you put a timestamp and _ (underscore) in front of the original filename $filename = time() . '_' . $filename; //Here you combine $target_path ("images/") with $filename (1234567890_orgfilename.ext) and get "images/1234567890_orgfilename.ext"; $target_path .= $filename; if(move_uploaded_file($_FILES['Image1']['tmp_name'], $target_path)) { echo "Image was uploaded successfully!"; } else{ print "Image could not be uploaded!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/69449-solved-rename-uploaded-images/#findComment-348953 Share on other sites More sharing options...
npsari Posted September 15, 2007 Author Share Posted September 15, 2007 wow, iam using your code now I removed all the bits which do nothing, and it works fine Last thing,You showd me how to rename them to UPLOADED do you know how i rename them to UPLOADED.File extension Any idea how this can be done $filename = 'UPLOADED'; < this removes the extension of the image Quote Link to comment https://forums.phpfreaks.com/topic/69449-solved-rename-uploaded-images/#findComment-348956 Share on other sites More sharing options...
Wuhtzu Posted September 15, 2007 Share Posted September 15, 2007 All you need to do is manipulate $target_path until it has the proper value. First of all you want all your images to go in the a sub directory called "images", this is why you do this: <?php $target_path = 'images/'; ?> Now if you do not want the images to go in any more sub directories you need to append the filename to the end of the $target_path variable so it becomes "images/filename.ext". This means you need to find out what you want to call your uploaded files. It seems to be something with a timestamp, followed by a _ (underscore), followed by 'UPLOADED' followed by the original extension. This is almost accomplished by this: <?php $target_path = 'images/'; $target_path .= time() . '_UPLOADED'; ?> Now your $target_path looks like this: 'images/1234567890_UPLOADED' What is missing? The extension of course! So what to do? Get the extension! http://www.scriptplayground.com/tutorials/php/Get-File-Extension/ Once you have a variable called $ext ($str in the example I linked to) you only need to append it to the rest of $target_path: <?php $target_path = 'images/'; $target_path .= time() . '_UPLOADED'; $target_path .= $ext; //Remember that $ext might not contain the . (dot) but only the extension, for example jpg not .jpg. This means you need to add the dot yourself. $target_path .= $ext; //Can be written as $target_path = 'images/' . $time . '_UPLOADED' . $ext; ?> EDIT: Changed "//Remember that $ext might now contain the . (dot)" to "//Remember that $ext might not contain the . (dot)" Quote Link to comment https://forums.phpfreaks.com/topic/69449-solved-rename-uploaded-images/#findComment-348958 Share on other sites More sharing options...
npsari Posted September 15, 2007 Author Share Posted September 15, 2007 You should write a book I used it like that, though, it doesnt work for some reason, is that a mistake on my code <?php function getExtension($Image1) { $pos = strrpos($Image1, '.'); if(!$pos) { return 'Unknown Filetype'; } $ext = substr($Image1, $pos, strlen($Image1)); return $ext; } $target_path = 'images/' . $time . '_UPLOADED' . $ext; if(move_uploaded_file($_FILES['Image1']['tmp_name'], $target_path)) { echo "Image was uploaded successfully!"; } else{ print "Image could not be uploaded!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/69449-solved-rename-uploaded-images/#findComment-348962 Share on other sites More sharing options...
npsari Posted September 15, 2007 Author Share Posted September 15, 2007 ill work on it though you helped today Quote Link to comment https://forums.phpfreaks.com/topic/69449-solved-rename-uploaded-images/#findComment-348964 Share on other sites More sharing options...
Wuhtzu Posted September 15, 2007 Share Posted September 15, 2007 Woaah! There was no reason to mark the thread/topic as SOLVED. All I was trying to do was to make you understand the code you were using... There is no reason why you should not end up with something working. The reason why it's not working is because you defined the "get extension code" as a function but you do not call the function and therefore it does nothing. To correct the code you posted: <?php //You can call the function argument anything, it does not have anything to do with your current script and $image1... call it $filename since it can be used to get the ext form any filename... function getExtension($filename) { $pos = strrpos($filename, '.'); if(!$pos) { return 'Unknown Filetype'; } $ext = substr($filename, $pos, strlen($filename)); return $ext; } //You need to call the function when the extension is needed $target_path = 'images/' . $time . '_UPLOADED' . getExtension($_FILES['Image1']['tmp_name']); if(move_uploaded_file($_FILES['Image1']['tmp_name'], $target_path)) { echo "Image was uploaded successfully!"; } else{ print "Image could not be uploaded!"; } ?> But there is no real reason for creating a function... you only need the code once. Do something like this: <?php //Assing a more nifty name to $_FILES['Image1']['tmp_name'] $image = $_FILES['Image1']['tmp_name']; //Define the upload dir: $upload_dir = 'images/'; //Get the files extension $pos = strrpos($image, '.'); $ext = substr($image, $pos, strlen($image)); //Create the final filename $output_filename = time() . '_UPLOADED' . $ext; //Construct the target path $target_path = $upload_dir . $output_filename; if(move_uploaded_file($image, $target_path)) { echo "Image was uploaded successfully!"; } else{ print "Image could not be uploaded!"; } ?> But I must recommend you to get your hands on some PHP books or search the internet for some basic PHP tutorials. You are missing alot of fundamental stuff. If it does not work post again. Quote Link to comment https://forums.phpfreaks.com/topic/69449-solved-rename-uploaded-images/#findComment-348969 Share on other sites More sharing options...
eZe616 Posted September 15, 2007 Share Posted September 15, 2007 //Get the files extension $pos = strrpos($image, '.'); $ext = substr($image, $pos, strlen($image)); couldn't he use the $ext = explode($image) and then use $ext[1] where he needs the extensions? Quote Link to comment https://forums.phpfreaks.com/topic/69449-solved-rename-uploaded-images/#findComment-348971 Share on other sites More sharing options...
Wuhtzu Posted September 15, 2007 Share Posted September 15, 2007 He sure could. There are lots of ways to obtain the "extension" of a filename: #1 <?php $pos = strrpos($image, '.'); $ext = substr($image, $pos, strlen($image)); ?> #2 <?php $ext = explode($image); //$ext[1] will hold the extension ?> Just to mention a few. Some of them might be faster than others but I don't know which is fastest. I've always used the position method... #3 <?php $path_info = pathinfo($filename); //$path_info['extension'] will hold the extension ?> Quote Link to comment https://forums.phpfreaks.com/topic/69449-solved-rename-uploaded-images/#findComment-348977 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.