Last active
May 28, 2018 05:31
-
-
Save Ema4rl/81d058fa171e08ec883c29085c1c7903 to your computer and use it in GitHub Desktop.
CodeIgniter MY_Upload Extension to create filepath directory if not exist and make it really writable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
defined('BASEPATH') OR exit('No direct script access allowed'); | |
/** | |
* File Uploading Class Extension | |
* | |
* @package CodeIgniter | |
* @subpackage Libraries | |
* @category Uploads | |
* @author Harrison Emmanuel (Eharry.me) | |
* @link https://www.eharry.me/blog/post/my-codeigniter-upload-extension/ | |
*/ | |
class MY_Upload extends CI_Upload { | |
/** | |
* Validate Upload Path | |
* | |
* Verifies that it is a valid upload path with proper permissions. | |
* | |
* @return bool | |
*/ | |
public function validate_upload_path() | |
{ | |
if ($this->upload_path === '') | |
{ | |
$this->set_error('upload_no_filepath', 'error'); | |
return FALSE; | |
} | |
if (realpath($this->upload_path) !== FALSE) | |
{ | |
$this->upload_path = str_replace('\\', '/', realpath($this->upload_path)); | |
} | |
if ( ! is_dir($this->upload_path)) | |
{ | |
// EDIT: make directory and try again | |
if ( ! mkdir ($this->upload_path, 0777, TRUE)) | |
{ | |
$this->set_error('upload_no_filepath', 'error'); | |
return FALSE; | |
} | |
} | |
if ( ! is_really_writable($this->upload_path)) | |
{ | |
// EDIT: change directory mode | |
if ( ! chmod($this->upload_path, 0777)) | |
{ | |
$this->set_error('upload_not_writable', 'error'); | |
return FALSE; | |
} | |
} | |
$this->upload_path = preg_replace('/(.+?)\/*$/', '\\1/', $this->upload_path); | |
return TRUE; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
silly question: how to use this library?
thanks