Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function imageUpload($input_file_name = 'image', $dest_filename)
- {
- if (isset($_FILES[$input_file_name])) {
- if (!empty($_FILES[$input_file_name]['error'])) {
- $upload_error_code = $_FILES[$input_file_name]['error'];
- if ($upload_error_code == 4) {
- return false; // file not loaded (probably not choosen)
- } else {
- throw new Exception(
- $this->fileUploadCodeToMessage($upload_error_code));
- }
- } else {
- $uploaded = $_FILES[$input_file_name]['tmp_name'];
- if (!is_uploaded_file($uploaded)) {
- throw new Exception(
- "Possible file upload attack: ".$uploaded);
- }
- if (!file_exists($uploaded)) {
- throw new Exception( "File: ".$uploaded." not found.");
- }
- // is image?
- if (!$size = getimagesize($uploaded)){
- throw new Exception('Unsupported file format');
- }
- $dirname = dirname($dest_filename);
- if (!is_dir($dirname)) {
- // full for owner, read and exec(?) for others, recursive creation
- mkdir($dirname, 0755, true);
- }
- if (move_uploaded_file($uploaded, $dest_filename)) {
- chmod($dest_filename, 0644); // RW for owner, read for everybody else
- return true;
- } else {
- throw new Exception(
- "Can't move uploaded file: "
- ."\nfilesize=".filesize($uploaded)
- ."\ndisk_free_space=".disk_free_space($dirname));
- }
- }
- } else return false;
- }
- public function fileUploadCodeToMessage($code) {
- switch ($code) {
- case UPLOAD_ERR_INI_SIZE:
- return "php.ini upload_max_filesize exceeded";
- case UPLOAD_ERR_FORM_SIZE:
- return "HTML form MAX_FILE_SIZE exceeded";
- case UPLOAD_ERR_PARTIAL:
- return "The uploaded file was only partially uploaded";
- case UPLOAD_ERR_NO_FILE:
- return "No file was attached";
- case UPLOAD_ERR_NO_TMP_DIR:
- return "Missing a temporary folder";
- case UPLOAD_ERR_CANT_WRITE:
- return "Failed to write file to disk";
- case UPLOAD_ERR_EXTENSION:
- return "File upload stopped by extension";
- default:
- return "Unknown upload error (code $code)";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement