Exemplo n.º 1
0
 /**
  * Checks to see if the MIME type of the uploaded file is acceptable.
  * @param string|array a MIME type pattern, or an array of such patterns
  * @return boolean true if the MIME type matched any of the patterns;
  *         false if otherwise
  * @uses ::mime_type_matches() the underlying implementation
  */
 function mime_type_matches($pattern)
 {
     return mime_type_matches($pattern, $this->get_mime_type('application/octet-stream'));
 }
Exemplo n.º 2
0
 /** @access private */
 function _grab_value_from_upload()
 {
     $error_code = $this->_get_upload_error();
     if ($error_code !== null) {
         // There was a problem uploading the file.
         $this->set_error($this->_get_upload_error_message());
     }
     if (!$this->has_error) {
         // No errors so far; check the size of the uploaded file.
         $max_size = $this->max_file_size;
         if ($this->file["size"] <= 0) {
             $this->set_error("It doesn't look like that file has " . "anything in it.");
         } else {
             if ($this->file["size"] > $max_size) {
                 $readable_size = format_bytes_as_human_readable($max_size);
                 $filename = strip_tags(htmlspecialchars($this->file["name"]));
                 $this->set_error("The file you want to upload " . "({$filename}) exceeds the maximum upload size of " . "{$readable_size}.");
             }
         }
     }
     if (!$this->has_error && !empty($this->acceptable_types)) {
         $mime_type = get_mime_type($this->file["path"], 'application/octet-stream');
         if (!mime_type_matches($this->acceptable_types, $mime_type)) {
             $this->set_error("The file you want to upload is not " . "in an acceptable format.");
         }
     }
     // check acceptable extensions if provided.
     if (!$this->has_error && !empty($this->acceptable_extensions)) {
         $filename_parts = explode('.', $this->file["name"]);
         $extension = strtolower(end($filename_parts));
         if (!in_array($extension, $this->acceptable_extensions)) {
             $this->set_error("The file you want to upload is not " . "in an acceptable format.");
         }
     }
     $value = null;
     if (!$this->has_error) {
         $this->_state = "received";
         $filename = $this->file["name"];
         $this->tmp_web_path = $this->_generate_temp_name($filename);
         $value = $this->tmp_full_path = $_SERVER['DOCUMENT_ROOT'] . $this->tmp_web_path;
         if (!rename($this->file["path"], $value)) {
             $this->set_error("Your file was received, but could not " . "be saved on the server.");
         } else {
             if (!empty($this->file["modified_path"])) {
                 if ($this->file["path"] != $this->file["modified_path"]) {
                     // We used the original file, so we remove the
                     // modified (e.g., rescaled) file.
                     // TAKE NOTE: This removal is not optional!
                     if (@unlink($this->file["modified_path"])) {
                         unset($this->file["modified_path"]);
                     }
                 }
             } else {
                 if (!empty($this->file["original_path"])) {
                     if ($this->file["path"] != $this->file["original_path"]) {
                         // Bring the original file along for the ride as well.
                         $new_orig = $this->_generate_temp_name($filename, "-original");
                         if (rename($this->file["original_path"], $new_orig)) {
                             $this->file["original_path"] = $new_orig;
                         }
                     }
                 }
             }
             $this->file["path"] = $this->file["tmp_name"] = $value;
             $this->_upload_success($value, $this->tmp_web_path);
         }
     }
     return $value;
 }
Exemplo n.º 3
0
/**
 * Determines the MIME type of an actual file using any available techniques:
 * <code>getimagesize()</code>, the PHP Fileinfo extension, the UNIX
 * <code>file(1)</code> utility, or the mime.types file.
 * 
 * @param string $path the path to the file whose MIME types is desired
 * @param string $default what to return if the type cannot be determined
 * @param string $filename if <code>$path</code> does not reflect the actual
 *        name of the file (e.g., the path has no file extension), pass the
 *        true filename here
 * @return string the determined MIME type, or <code>$default</code> if the
 *         type could not be determined
 */
function get_mime_type($path, $default = null, $filename = null)
{
    $type = _get_mime_type_fileinfo($path);
    if (!$type || $type == 'application/octet-stream') {
        $type = _get_mime_type_unix($path);
    }
    if (!$filename) {
        $filename = basename($path);
    }
    $extension = strtolower(ltrim(strrchr($filename, '.'), '.'));
    $type = _sanity_check_mime_type($extension, $type);
    if (!$type || $type == 'application/octet-stream') {
        if (!$filename) {
            $filename = $path;
        }
        $type = mime_type_from_extension($extension);
    }
    if (!$type || mime_type_matches('image/*', $type)) {
        // If we detected an image type, verify it using getimageinfo().
        // If the image was not valid, the type will be reset to NULL.
        $type = _get_mime_type_image($path);
    }
    return $type ? $type : $default;
}