Search code examples
phprecursionftpfile-permissionsfolder-permissions

How to set all the permissions recursively to the new folder created on FTP server using PHP?


I've created a new folder on FTP server using PHP. Now I want to set all the permissions recursively(i.e. the same permissions should be applied to all the files and folders residing it.)

I tried to research about this thing then I came to know about the built-in PHP function ftp_chmod(). I searched for examples where this function is implemented but everywhere I'm getting examples of assigning permission to a file not to a folder. So can someone please help me in this regard please?

Following is the code I tried:

$ext = pathinfo($_FILES['student_image']['name'], PATHINFO_EXTENSION);     
$student_image_name = 'student_'.$t.'.'.$ext;
$_POST['student_name'] = $student_image_name;
$ftp_server="52.237.5.85"; 
$ftp_user_name="myservercreds"; 
$ftp_user_pass="MyServerCreds";

$file = $_FILES['student_image']['name'];//tobe uploaded 
$remote_file = "/Students/".$_POST['student_name']; 

// set up basic connection 
$conn_id = ftp_connect($ftp_server);  

// login with username and password 
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

if($login_result) {
  if(!is_dir('ftp://myservercreds:MyServerCreds@52.237.5.85/Students')) {
    ftp_mkdir($conn_id, "/Students");
    ftp_chmod($conn_id, 'all');//Here I'm getting issue in assigning all the permissions recursively to the newly created folder '/Student'
  }

Can someone please help me in this regard?


Solution

  • ftp_chmod requires 3 arguments. The second one is to be of integer type, specifying mode. The third is the file name. So, the correct codeline would be:

    //                  mode      file
    ftp_chmod($conn_id, 0777, '/Students'); // access for everyone for read/write
    

    Hope it helps.