Skip to content

Instantly share code, notes, and snippets.

@rolandinsh
Last active September 8, 2022 15:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save rolandinsh/5143047 to your computer and use it in GitHub Desktop.
Save rolandinsh/5143047 to your computer and use it in GitHub Desktop.
Codeigniter file upload permissions (CHMOD)
<?php
$config['upload_path'] = './uploads/'; // upload path
/* OR any other form, e.g.:
$config['upload_path'] = $_SERVER["DOCUMENT_ROOT"].'/uploads/'; // upload path
$config['upload_path'] = __DIR__.'/uploads/'; // upload path
*/
$config['allowed_types'] = 'zip'; // array or string of file extensions
$config['max_size'] = '100'; // max file size
$this->load->library('upload', $config); // do the job
$zdata = ['upload_data' => $this->upload->data()]; // get data
$zfile = $zdata['upload_data']['full_path']; // get file path
chmod($zfile,0777); // CHMOD file or any other permission level(s)
// ... rest of code ...
/*
Copyright 2013,2020 Rolands Umbrovskis ( https://umbrovskis.com/ )
Released under the https://simplemediacode.com/license/
*/
@MathiasReker
Copy link

Nice script. Thanks for sharing. :-)
I would like to share a library I have coded with the same goal.

Example of usage:

<?php

use MathiasReker\PhpChmod\Scanner;

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

(new Scanner())
    ->setDefaultFileMode(0644)
    ->setDefaultDirectoryMode(0755)
    ->setExcludedFileModes([0400, 0444, 0640])
    ->setExcludedDirectoryModes([0750])
    ->scan([__DIR__])
    ->fix();

Full documentation: https://github.com/MathiasReker/php-chmod

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment