Last active
December 20, 2015 00:49
-
-
Save cuheguevara/6044784 to your computer and use it in GitHub Desktop.
CodeIgniter Custom Uploads #libraries
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 | |
public function myupload($file_name, $opt) { | |
$this->load->helper('directory'); | |
$opt_folder_uploads = ( ($opt["opt_folder_uploads"] == NULL) || ($opt["opt_folder_uploads"] == "") ? "./media/uploads/news/" . date("Y-m") : $opt["opt_folder_uploads"] ); | |
$opt_input_text_name = ( ($opt["opt_input_text_name"] == NULL) || ($opt["opt_input_text_name"] == "") ? "userfile" : $opt["opt_input_text_name"] ); | |
$opt_type = ( ($opt["opt_type"] == NULL) || ($opt["opt_type"] == "") ? "images" : $opt["opt_type"] ); | |
$opt_prefix = ( ($opt["opt_prefix"] == NULL) || ($opt["opt_prefix"] == "") ? NULL : $opt["opt_prefix"] ); | |
$opt_suffix = ( ($opt["opt_suffix"] == NULL) || ($opt["opt_suffix"] == "") ? NULL : $opt["opt_suffix"] ); | |
$opt_maxsize = ( ($opt["opt_maxsize"] == NULL) || ($opt["opt_maxsize"] == "") ? "50000" : $opt["opt_maxsize"] ); | |
$opt_max_width = ( ($opt["opt_max_width"] == NULL) || ($opt["opt_max_width"] == "") ? "1024" : $opt["opt_max_width"] ); | |
$opt_max_height = ( ($opt["opt_max_height"] == NULL) || ($opt["opt_max_height"] == "") ? "768" : $opt["opt_max_height"] ); | |
if (!is_dir($opt_folder_uploads)) { | |
mkdir($opt_folder_uploads, DIR_WRITE_MODE, true); | |
} | |
exec("sudo chmod 777 " . $opt_folder_uploads); | |
$config['upload_path'] = $opt_folder_uploads; | |
$config['remove_spaces'] = FALSE; | |
switch ($opt_type) { | |
case "images": | |
$config['allowed_types'] = 'gif|jpg|png|JPG|PNG'; | |
$config['max_width'] = $opt_max_width; | |
$config['max_height'] = $opt_max_height; | |
break; | |
case "video": | |
$config['allowed_types'] = 'mkv|avi|flv|mp4|mpg|mpeg|wmv'; | |
break; | |
case "doc": | |
$config['allowed_types'] = 'doc|docx|xls|xlsx|txt|rtf'; | |
break; | |
case "audio": | |
$config['allowed_types'] = 'mp2|mp1|midi|wav|mp3'; | |
break; | |
} | |
$config['max_size'] = $opt_maxsize; | |
$name = ""; | |
if ((($opt_prefix == NULL) || ($opt_prefix == "")) && (($opt_suffix == NULL) || ($opt_suffix == ""))) { | |
$name = $file_name["name"]; | |
} | |
if ((($opt_prefix == NULL) || ($opt_prefix == "")) && (($opt_suffix != NULL) || ($opt_suffix != ""))) { | |
$name = $file_name["name"] . $opt_suffix; | |
echo substr($file_name["name"], strrpos($file_name["name"], '.') + 1); | |
} | |
if ((($opt_prefix != NULL) || ($opt_prefix != "")) && (($opt_suffix != NULL) || ($opt_suffix != ""))) { | |
$name = $opt_prefix . basename($file_name["name"], substr($file_name["name"], strrpos($file_name["name"], '.'))) . $opt_suffix . '.' . pathinfo($file_name["name"], PATHINFO_EXTENSION); | |
} | |
if ((($opt_prefix != NULL) || ($opt_prefix != "")) && (($opt_suffix == NULL) || ($opt_suffix == ""))) { | |
$name = $opt_prefix . $file_name["name"]; | |
} | |
$config['file_name'] = $name; | |
$config['file_path'] = $file_name["tmp_name"]; | |
$this->load->library('upload', $config); | |
$this->upload->initialize($config); | |
$filesupload = ""; | |
if (!$this->upload->do_upload($opt_input_text_name)) { | |
$error = array('error' => $this->upload->display_errors()); | |
$error = array_filter($error); | |
$filesupload = $error; | |
} else { | |
$filesupload = $opt_folder_uploads . '/' . $config['file_name']; | |
} | |
if (($filesupload == "") || (@empty($filesupload)) || ($filesupload == null)) { | |
return "empty"; | |
} | |
if (is_array($filesupload)) { | |
if (array_key_exists("error", $filesupload)) { | |
return "error" . $filesupload["error"]; | |
} | |
} else { | |
return $filesupload; | |
} | |
} | |
?> | |
<?php | |
// Controllers | |
$folder_uploads = './media/uploads/news/' . date('Y-m'); | |
$options_upload = array( | |
"opt_folder_uploads" => $folder_uploads, | |
"opt_input_text_name" => "userfile", | |
"opt_type" => "images", | |
"opt_prefix" => "test-", | |
"opt_suffix" => "-ok", | |
"opt_maxsize" => "", | |
"opt_max_width" => "", | |
"opt_max_height" => "", | |
); | |
$photo = ($_FILES['userfile']['name'] != "") ? $this->myupload($_FILES['userfile'], $options_upload) : "empty"; | |
echo $photo; | |
// will give return string value "error", "empty", "filename" | |
?> | |
<form method="post" action="controller/action" enctype="multipart/form-data"> | |
<input type="file" name="userfile" id="userfile" /> | |
<button type="submit">Save</button> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment