如何在现有文件夹中创建文件夹后在 php 中设置 755 权限



>我在Codeigniter控制器中有以下代码,可以将一些文件上传到远程ubuntu服务器的"上传"文件夹中。在这种情况下,上传文件时会在上传文件夹中创建一个名为 officer_id 的新文件夹,并应对其应用 755 权限。否则拒绝上传文件。

public function addFiles()
{
$this->checkPermissions('add', 'officer');
$bc = array(array('link' => '#', 'page' => 'Attachments'));
$meta = array('page_title' => 'Officers - Files', 'bc' => $bc);
$this->data['officer'] = $this->Officer_model->getOfficer();

$this->form_validation->set_rules('officer', "Officer", 'required');
if ($this->form_validation->run() == true) {
$files = $this->multi_upload($_FILES['file'], './uploads/' . $this->input->post('officer'));
if (!empty($files)) {
foreach ($files as $fname) {
$fdata[] = array(
'officer' => $this->input->post('officer'),
'file_name' => $fname,
'status' => 1,
);
}
if ($this->db->insert_batch('tbl_officer_files', $fdata)) {
$this->session->set_flashdata('message', 'Officer Attachments Updated Successfully ..!!');
redirect('officer/addFiles');
} else {
$this->session->set_flashdata('message', 'Officer Attachments Updation Failed ..!!');
redirect('officer/addFiles');
}
}
}
$this->render('officer/addFiles', $meta, $this->data);
}
public function saveUpload()
{
$this->multi_upload($_FILES['file']);
if ($this->upload->do_upload('file')) {
$data = array('upload_data' => $this->upload->data());
$inputFileName = './uploads/' . $data['upload_data']['file_name'];
echo json_encode(array('success' => 'Files have been uploaded successfuly ..!!'));
} else {
$error = array('error' => $this->upload->display_errors());
echo json_encode($error);//array('error' => 'You are not allowed to upload such a file.',
}
}

我如何修改我的代码以启用此功能(如 chmod ......谁能帮忙?

上传后,您可以使用chmod("test.txt",0755);更新文件的权限。

您可以按以下步骤逐步执行此操作。


//this function returns uploaded file path
$uploadedd_file = file_upload_function(...); 
if(!file_exists($uploadedd_file)){

die('Error in uploading file');
}
// Setting file permission
$set_permission = chmod($uploadedd_file,0755);

if(!$set_permission){
// delete file if permission is not set
unlink($uploadedd_file);
}

// optional step to validate
$new_permission = substr(sprintf('%o', fileperms($uploadedd_file)), -4);
echo "all good goes here, file uploaded and permission is set to " . $new_permission;

阅读更多 关于chmod: https://www.w3schools.com/php/func_filesystem_chmod.asp https://www.php.net/manual/en/function.chmod.php 我希望这有效,让我知道。

相关内容

  • 没有找到相关文章

最新更新