/** * Upload Files * * @access public * @param string The directory to place the uploaded files * @return boolean */ function upload_files($dir) { //If the upload directory is useable and there are files to upload if (directory_usable($dir) && isset($_FILES)) { //Foreach file that has been uploaded foreach ($_FILES as $name => $file) { //If no errors with the file if (upload_check_errors($name)) { if (!move_uploaded_file($file['tmp_name'], $dir . $file['name'])) { trigger_error('Could not move file', E_USER_ERROR); return; } } } return TRUE; } }
/** * Perform the file upload * * @access public * @return bool */ function do_upload($field = 'userfile') { // Is $_FILES[$field] set? If not, no reason to continue. if (!isset($_FILES[$field])) { $this->set_error('no_file_selected'); return FALSE; } // Is the upload path valid? if (!directory_usable($this->upload_path)) { $this->set_error('bad_destination'); return FALSE; } // Was the file able to be uploaded? If not, determine the reason why. if (!is_uploaded_file($_FILES[$field]['tmp_name'])) { $error = !isset($_FILES[$field]['error']) ? 4 : $_FILES[$field]['error']; switch ($error) { case 1: // UPLOAD_ERR_INI_SIZE $this->set_error('file_exceeds_limit'); break; case 2: // UPLOAD_ERR_FORM_SIZE $this->set_error('file_exceeds_form_limit'); break; case 3: // UPLOAD_ERR_PARTIAL $this->set_error('file_partial'); break; case 4: // UPLOAD_ERR_NO_FILE $this->set_error('no_file_selected'); break; case 6: // UPLOAD_ERR_NO_TMP_DIR $this->set_error('no_temp_directory'); break; case 7: // UPLOAD_ERR_CANT_WRITE $this->set_error('unable_to_write_file'); break; case 8: // UPLOAD_ERR_EXTENSION $this->set_error('stopped_by_extension'); break; default: $this->set_error('no_file_selected'); break; } return FALSE; } //Clean the file name and also get the extension $this->process_filename($_FILES[$field]['name']); //Set the full file path $file_path = $this->upload_path . $this->file_name . '.' . $this->file_ext; // Set the uploaded data as class variables $this->file_temp = $_FILES[$field]['tmp_name']; $this->file_size = $_FILES[$field]['size']; $this->file_type = preg_replace("/^(.+?);.*\$/", "\\1", $_FILES[$field]['type']); // Convert the file size to kilobytes if ($this->file_size > 0) { $this->file_size = round($this->file_size / 1024, 2); } // Is the file type allowed to be uploaded? if ($this->allowed_types && !in_array($this->file_ext, $this->allowed_types)) { $this->set_error('invalid_filetype'); return FALSE; } // Is the file size within the allowed maximum? if ($this->max_size != 0 and $this->file_size > $this->max_size) { $this->set_error('invalid_filesize'); return FALSE; } /* * Move the file to the final destination * To deal with different server configurations * we'll attempt to use copy() first. If that fails * we'll use move_uploaded_file(). One of the two should * reliably work in most environments */ if (!@copy($this->file_temp, $file_path)) { if (!@move_uploaded_file($this->file_temp, $file_path)) { $this->set_error('destination_error'); return FALSE; } } //Save data about the final file $this->file_data[$field] = array('temp' => $this->file_temp, 'name' => $this->file_name . '.' . $this->file_ext, 'ext' => $this->file_ext, 'size' => $this->file_size, 'type' => $this->file_type); //Return the array return $this->file_data[$field]; }