Ok so the below creates a new folder IF I cmod the folder I want to create it in to 777. Nothing else works, but Im worried 777 is not secure. I dont really understand cmod that well but as far as I know 777 means that the whole world can write to my folder, and I dont want that. Am I doing something wrong? Shouldnt this work at cmod 775 or something like that? What IS the correct cmod for folders when you want to use php to create more folders inside them anyway?

	
//make sure all is lowercase
$username_l = strtolower($username);
$folder = strtolower($username{0});
// set up ftp connection
$conn_id = ftp_connect($ftp_server); 
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 
// check connection
if ((!$conn_id) || (!$login_result)) { echo "FTP connection has failed!";
exit; } 
//create folder
if (!file_exists("users/$folder/$username_l")) {
	mkdir("users/$folder/$username_l");
	chmod("users/$folder/$username_l", 0777); }
// close the FTP stream 
ftp_close($conn_id); 

    Don't take my word for this because I'm just guessing...but maybe you could try chmoding to 777, then doing what you need, then chmoding back to something a bit more safe. In other words, lmake the PHP do and undo it every time it needs to write inside the folders.

    Just a guess!

      You could also set the owner of the "parent" folder to be whatever is the owner of the process which runs your web server.

        Umm.. you could circumvent all that by actually using the FTP functions in PHP to create and rename the directories, rather than using mkdir() and chmod(). [man]ftp_mkdir/man [man]ftp_chdir/man.

        These have better functionality, and since you're already starting the FTP session, use them that way the folders will be made under the FTP Users account, not PHP, and you don't have to worry about chmod-ing anything to create folders!!

        ~Brett

          How about later on when I want to upload a file? I need something like ftp_mkdir except for files. Just plain "copy" means permission denied, which I understand as Im now cmodded to 755.
          The only command in the manual that looks half sutiable is ftp_fput, but Im struggling with parameters. I have

          ftp_fput ($conn_id, $_FILES['imagefile']['tmp_name'], "users/$folder/$mysite_username_l/".$_FILES['imagefile']['name'], FTP_ASCII) 
          

          and error is
          Warning: ftp_fput() expects parameter 1 to be resource, string given.
          If I redefine $conn_id right above the line (i dont understand why i need to do this since Ive defined it just a few lines higher) the error changes to
          Warning: ftp_fput() expects parameter 3 to be resource, string given.

            ftp_fput ( resource ftp_stream, string remote_file, resource handle, int mode [, int startpos] )

            ftp_stream
            The link identifier of the FTP connection.

            remote_file
            The remote file path.

            handle
            An open file pointer on the local file. Reading stops at end of file.

            mode
            The transfer mode. Must be either FTP_ASCII or FTP_BINARY.



            Well, this should work:

            <?php
            $file = $_FILES['imagefile']['tmp_name'];
            $fp = fopen('users/'.$folder.'/'.$mysite_username_l.'/'.$_FILES['imagefile']['name'], "r");
            
            $conn = ftp_connect($ftp_server);
            $login = ftp_login($conn, $ftp_user_name, $ftp_user_pass);
            
            if(ftp_fput($conn, $file, $filename, FTP_ASCII))
            {
              // Success
            }
            else
            {
              // Failure
            }
            
            ftp_close($conn);
            ?>

            ~Brett

              It didnt work.. so then I realised $filename wasnt defined, so I changed it to this...

              $file = $_FILES['imagefile']['tmp_name']; 
              $newfile = '/users/'.$folder.'/'.$mysite_username_l.'/'.$_FILES['imagefile']['tmp_name'];
              $fp = fopen('users/'.$folder.'/'.$mysite_username_l.'/', "r"); 
              
              // set up basic ftp connection
              $conn_id = ftp_connect($ftp_server); 
              $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 
              
              // check connection
              if ((!$conn_id) || (!$login_result)) { echo "FTP connection has failed!";
                  	exit; } 
              
              if(ftp_fput($conn_id, $file, $newfile, FTP_ASCII)) { echo "Sucess"; } 
              
              else { echo "Failure"; } 
              
              ftp_close($conn_id); 
              

              ... and Im still getting the same error 🙁

                Ok... now Ive gotten to THIS

                $file = $_FILES['imagefile']['tmp_name']; 
                $fp = fopen($home_dir.'/users/'.$folder.'/'.$mysite_username_l.'/', "r"); 
                
                // set up basic ftp connection
                $conn_id = ftp_connect($ftp_server); 
                $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 
                
                // check connection
                if ((!$conn_id) || (!$login_result)) { echo "FTP connection has failed!";
                exit; } 
                
                if(ftp_fput($conn_id, $file, $fp, FTP_ASCII)) { echo "Sucess"; } 
                else { echo "Failure"; } 
                
                ftp_close($conn_id); 
                

                It echoes sucess... but doesnt copy the file :S it just isnt there.

                  Does anyone think that perhaps the the structure of $file is wrong? =\ Maybe im grasping at straws now.

                    Ahh theres nothing like several hours of reading really boring stuff to help ya solve ya own problems. Here is the final working code for anyone whos interested.

                    // set up basic ftp connection
                    $conn_id = ftp_connect($ftp_server); 
                    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 
                    
                    // check connection
                    if ((!$conn_id) || (!$login_result)) { echo "FTP connection has failed!";
                        	exit; } 
                    
                    $filename = $_FILES['imagefile']['name'];
                    $file = $_FILES['imagefile']; 				
                    $destpath = $home_dir2."users/".$folder."/".$mysite_username_l."/";
                    $destfile = $destpath.$filename;	
                    $tempfile = $file['tmp_name'];
                    
                    $upload = ftp_put($conn_id, $destfile, $tempfile, FTP_BINARY);
                    
                    if (!$upload) { echo "FAILED"; }
                    else { echo "OMG IT WORKED"; }
                    
                    //close connection
                    ftp_close($conn_id); 
                    
                    
                      Write a Reply...