Skip to content

Instantly share code, notes, and snippets.

@Ema4rl
Last active May 28, 2018 05:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ema4rl/81d058fa171e08ec883c29085c1c7903 to your computer and use it in GitHub Desktop.
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
<?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;
}
}
@hahn
Copy link

hahn commented Mar 2, 2017

silly question: how to use this library?
thanks

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