<? $folder_name = $_POST['name']; mkdir("/path/$folder_name", 0777); ?> i think the above code makes a directory as the user inputs the folder name... Now, what i want is, if the user gives space in between the text like "my design works" then i want this text to renamed to "my_design_works" .. so how to do that?? and if that folder exists then i want the new folder to be renamed automatically with new name... i know i m very bad at PHP so.. waiting for your help guys....
$folder_name = $_POST['name']; $folder_path = 'path to where folder should be'; $folder_location = $folder_path .$folder_name; if(is_dir($folder_location)) { $new_folder = preg_replace('#[\s]+#','_',$folder_name); $new_folder_location = $folder_path .$new_folder; rename($folder_location,$new_folder_location); }else { //mkdir } Code (markup): Something like that, please check did it off top of my head. Edit: I highly suggest you cleanse your post data. Puts some checks and balances like no html code, or anything that can be harmful as well as make an array of foldernames you dont want them to make as they could possibly overwrite another persons folder or important folder you have in there.
the easiest way I guess is <? $folder_name = str_replace(" ","_",$_POST['name']); mkdir("/path/$folder_name", 0777); ?> Code (markup):
yeah thats what i was looking for but what if the folder name which you are going to create already exists in the server?? if there's already exists same folder name then can the new folder be renamed to FOLDER_NAME_456752344(it wud be random number) if not found then only FOLDER_NAME?? how to do that??
Look at the code I provided, is_dir checks to see if the directory exists. If so then you can rename it and then add the random numbers at the end of the folder name. I used preg_replace so it replaces multiple spaces with one underscore instead of to underscores in case the person who entered the folder name puts a double space by mistake.
i just want to rename automatically if there's already folder name with current one... once users submits the form, it should not stop but it should automatically rename with random numbers at the end of folder name.. are you getting me??
I guess Im missing something. My code takes the users input, checks for a duplicate. If a duplicate then renames it into the format you want it in, else it creates a new folder.
So you want me to do all the code? You can easily go to google and type in php random generate numbers and find numerous examples. Got to learn some where "dude".
Here is a function that I use in my apps for getting random generated numbers: function randomString($length) { $charset = "abcdefghijklmnopqrstuvwxyz"; $charset .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $charset .= "0123456789"; for ($i=0; $i<$length; $i++) $key .= $charset[(mt_rand(0,(strlen($charset)-1)))]; return $key; } Code (markup): All you got to do is remove all the $charset letters and just have the numbers and that will create a random number. However you need to also check to see if that directory already exists. If it exists then most likely you dont want it to do anything. But you really need to put it all together and test so you can learn more.
well modified this a little bit... man i dont know how i did this... i am very poor at PHP... here's the code... but i get error Undefined variable: key when it finds existing folder... but not when it doesnt finds existing folder..... <? $folder_name = $_POST['name']; $folder_name = preg_replace('#[\s]+#','_',$folder_name); $folder_path = 'folder/'; $new_name = $folder_path.$folder_name; function randomString($length) { $charset = "0123456789"; for ($i=0; $i<$length; $i++) $key .= $charset[(mt_rand(0,(strlen($charset)-1)))]; return $key; } if (file_exists($new_name)) { $new_folder_location = $new_name.'_'.randomString(10); mkdir($new_folder_location, 0777); } else { mkdir($new_name, 0777); } ?> PHP:
That is a warning, not an error. Stating the variable $key has no value. Just add $key = ''; underneath $charset = "0123456789"; in the randomString function.
no i didnt mean that but... i mean that code do the right works but when it finds the existing folder and when it makes random name... it actually makes the folder with random number but at last it gives variable key error thanks