简体   繁体   中英

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(ie 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?

ftp_chmod requires 3 arguments. The second one is to be of int eger 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.

I have coded a library for that.

Example of usage in your case:

<?php

use MathiasReker\FilePerm;

require __DIR__ . '/vendor/autoload.php';

(new FilePerm(['./students'])) // <-- root directory
    ->setDefaultModeFile(0644) // <-- file permission
    ->setDefaultModeFolder(0777) // <-- folder permission
    ->scan()
    ->fix();

Full documentation: https://github.com/MathiasReker/php-file-permissions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM