+ Reply to Thread
Results 1 to 3 of 3

Thread: PHP creating folder help

  1. #1
    thenewprogrammer is offline x10Hosting Member thenewprogrammer is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    45

    PHP creating folder help

    Trying to have folder create based on username. So if the username Frank uploads something i want his file to be uploaded to folder uploads/frank/(his file here). But im not good with php and screwing up. I have the upload code already and had it working on uploading to certain folder but cant get it to create its own folder if it doesnt exist yet.

    Code:
    <?php
    $uploaddir = './uploads/'; 
    $file = $uploaddir . basename($_FILES['uploadfile']['name']); 
    $size=$_FILES['uploadfile']['size'];
    if($size>500*1024)
    {
     echo "error file size > 500 MB";
     unlink($_FILES['uploadfile']['tmp_name']);
     exit;
    }
    move_uploaded_file($_FILES["file"]["tmp_name"],
          "'.$_GET['username'].'/private/"  . str_replace (" ", "",$_FILES["file"]["name"] . $file));
    ?>
    if you need to look at other code to just ask. Just trying to get it to create folder based on their username in directory "uploads/"their username"/theirfile. Upload there but if folder doesnt exist create it on their first upload. So if anyone could help plz do.
    Last edited by thenewprogrammer; 11-04-2009 at 01:55 PM.

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: PHP creating folder help

    1. Double check $_GET['username'] to make sure you are not hacked.

    PHP Code:

    if(    isset(  $_GET['username'] ) &&   
                
    strlentrim(  $_GET['username']  )  ) > 0   ){
        
    $user trim(  $_GET['username'] ) ;
    } else {
       
    ## handle blank username
    }


    if( ! 
    ctype_alnum $user  ) ){
      
    ## do something if $user is not alphanumeric

    2. Create dir if does not exist

    PHP Code:

    $the_path 
    '/home/cpanelusername/some/more/path/' ;

    $user_dir $the_path $user '/private' ;

    if( ! 
    is_dir $user_dir  )  ){
       
    # try to make the directory
       
    if( ! mkdir(   $user_dir ,  0755 ) ){  
           
    ### handle fact that directory not made...
       
    }


    Nothing is always absolutely so.

  3. #3
    cerbere is offline x10Hosting Member cerbere is an unknown quantity at this point
    Join Date
    Nov 2007
    Posts
    51

    Re: PHP creating folder help

    Isn't

    Code:
    if($size>500*1024)
      { ... }
    a mistake ? You're rejecting files larger than 500 kb, not 500 Mb...

+ Reply to Thread

Similar Threads

  1. Free Development Web Software
    By Danfelbm in forum Graphics & Webdesign
    Replies: 1
    Last Post: 03-01-2008, 07:01 PM
  2. Replies: 1
    Last Post: 10-31-2007, 10:44 AM
  3. Sigo con problemas con phpbb2
    By reciecho in forum Soporte
    Replies: 7
    Last Post: 10-20-2007, 06:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers