how to remove spaces?

Discussion in 'PHP' started by badmasketa, Oct 2, 2008.

  1. #1
    <?

    $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....
     
    badmasketa, Oct 2, 2008 IP
  2. Sillysoft

    Sillysoft Active Member

    Messages:
    177
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #2
    
    $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.
     
    Sillysoft, Oct 2, 2008 IP
  3. PinoyIto

    PinoyIto Notable Member

    Messages:
    5,863
    Likes Received:
    170
    Best Answers:
    0
    Trophy Points:
    260
    #3
    the easiest way I guess is

    
    <?
    
    $folder_name = str_replace(" ","_",$_POST['name']);
    
    mkdir("/path/$folder_name", 0777);
    
    ?>
    
    Code (markup):
     
    PinoyIto, Oct 2, 2008 IP
  4. badmasketa

    badmasketa Well-Known Member

    Messages:
    351
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #4
    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?? :rolleyes::rolleyes::rolleyes:
     
    badmasketa, Oct 2, 2008 IP
  5. Sillysoft

    Sillysoft Active Member

    Messages:
    177
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #5
    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.
     
    Sillysoft, Oct 2, 2008 IP
  6. badmasketa

    badmasketa Well-Known Member

    Messages:
    351
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #6
    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??
     
    badmasketa, Oct 2, 2008 IP
  7. Sillysoft

    Sillysoft Active Member

    Messages:
    177
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #7
    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.
     
    Sillysoft, Oct 2, 2008 IP
  8. badmasketa

    badmasketa Well-Known Member

    Messages:
    351
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #8
    is that so?? wheres the random number generator code?? i dont see there dude... :p
     
    badmasketa, Oct 2, 2008 IP
  9. Sillysoft

    Sillysoft Active Member

    Messages:
    177
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #9
    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".
     
    Sillysoft, Oct 2, 2008 IP
  10. Sillysoft

    Sillysoft Active Member

    Messages:
    177
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #10
    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.
     
    Sillysoft, Oct 2, 2008 IP
  11. badmasketa

    badmasketa Well-Known Member

    Messages:
    351
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #11
    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:
     
    badmasketa, Oct 2, 2008 IP
  12. Sillysoft

    Sillysoft Active Member

    Messages:
    177
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #12
    That is a warning, not an error. Stating the variable $key has no value. Just add $key = ''; underneath $charset = "0123456789"; in the randomString function.
     
    Sillysoft, Oct 3, 2008 IP
  13. badmasketa

    badmasketa Well-Known Member

    Messages:
    351
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #13
    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
     
    badmasketa, Oct 4, 2008 IP