示例#1
0
/**
 * @param $file A $_FILES item
 */
function awpcp_upload_image_file($directory, $filename, $tmpname, $min_size, $max_size, $min_width, $min_height, $uploaded = true)
{
    $filename = sanitize_file_name($filename);
    $newname = wp_unique_filename($directory, $filename);
    $newpath = trailingslashit($directory) . $newname;
    if (!file_exists($tmpname)) {
        return sprintf(__('The specified image file does not exists: %s.', 'AWPCP'), $filename);
    }
    $ext = strtolower(awpcp_get_file_extension($filename));
    $imginfo = getimagesize($tmpname);
    $size = filesize($tmpname);
    $allowed_extensions = array('gif', 'jpg', 'jpeg', 'png');
    if (empty($filename)) {
        return __('No file was selected.', 'AWPCP');
    }
    if ($uploaded && !is_uploaded_file($tmpname)) {
        return __('Unknown error encountered while uploading the image.', 'AWPCP');
    }
    if (empty($size) || $size <= 0) {
        $message = "There was an error trying to find out the file size of the image %s.";
        return __(sprintf($message, $filename), 'AWPCP');
    }
    if (!in_array($ext, $allowed_extensions)) {
        return sprintf(__('The file %s has an invalid extension and was rejected.', 'AWPCP'), $filename);
    } elseif ($size < $min_size) {
        $message = __('The size of %1$s was too small. The file was not uploaded. File size must be greater than %2$d bytes.', 'AWPCP');
        return sprintf($message, $filename, $min_size);
    } elseif ($size > $max_size) {
        $message = __('The file %s was larger than the maximum allowed file size of %s bytes. The file was not uploaded.', 'AWPCP');
        return sprintf($message, $filename, $max_size);
    } elseif (!isset($imginfo[0]) && !isset($imginfo[1])) {
        return sprintf(__('The file %s does not appear to be a valid image file.', 'AWPCP'), $filename);
    } elseif ($imginfo[0] < $min_width) {
        $message = __('The image %s did not meet the minimum width of %s pixels. The file was not uploaded.', 'AWPCP');
        return sprintf($message, $filename, $min_width);
    } elseif ($imginfo[1] < $min_height) {
        $message = __('The image %s did not meet the minimum height of %s pixels. The file was not uploaded.', 'AWPCP');
        return sprintf($message, $filename, $min_height);
    }
    if ($uploaded && !@move_uploaded_file($tmpname, $newpath)) {
        $message = __('The file %s could not be moved to the destination directory.', 'AWPCP');
        return sprintf($message, $filename);
    } else {
        if (!$uploaded && !@copy($tmpname, $newpath)) {
            $message = __('The file %s could not be moved to the destination directory.', 'AWPCP');
            return sprintf($message, $filename);
        }
    }
    if (!awpcp_create_image_versions($newname, $directory)) {
        $message = __('Could not create resized versions of image %s.', 'AWPCP');
        # TODO: unlink resized version, thumbnail and primary image
        @unlink($newpath);
        return sprintf($message, $filename);
    }
    @chmod($newpath, 0644);
    return array('original' => $filename, 'filename' => $newname);
}
示例#2
0
/**
 * Check that the given file meets the file size, dimensions and file type
 * constraints and moves the file to the AWPCP Uploads directory.
 *
 * @param $error    if an error occurs the error message will be returned by reference
 *                  using this variable.
 * @param $action   'upload' if the file was uplaoded using an HTML File field.
 *                  'copy' if the file was uplaoded using a different method. Images
 *                  extracted from a ZIP file during Ad import.
 * @return          false if an error occurs or an array with the upload file information
 *                  on success.
 * @since 3.0.2
 * @deprecated  3.4
 */
function awpcp_upload_file($file, $constraints, &$error = false, $action = 'upload')
{
    $filename = sanitize_file_name(strtolower($file['name']));
    $tmpname = $file['tmp_name'];
    $mime_type = $file['type'];
    if (!in_array($mime_type, $constraints['mime_types'])) {
        $error = _x('The type of the uplaoded file %s is not allowed.', 'upload files', 'AWPCP');
        $error = sprintf($error, '<strong>' . $filename . '</strong>');
        return false;
    }
    $paths = awpcp_get_uploads_directories();
    if (!file_exists($tmpname)) {
        $error = _x('The specified file does not exists: %s.', 'upload files', 'AWPCP');
        $error = sprintf($error, '<strong>' . $filename . '</strong>');
        return false;
    }
    if ($action == 'upload' && !is_uploaded_file($tmpname)) {
        $error = _x('Unknown error encountered while uploading the image.', 'upload files', 'AWPCP');
        $error = sprintf($error, '<strong>' . $filename . '</strong>');
        return false;
    }
    $file_size = filesize($tmpname);
    if (empty($file_size) || $file_size <= 0) {
        $error = _x('There was an error trying to find out the file size of the image %s.', 'upload files', 'AWPCP');
        $error = sprintf($error, '<strong>' . $filename . '</strong>');
        return false;
    }
    if (in_array($mime_type, awpcp_get_image_mime_types())) {
        if ($file_size > $constraints['max_image_size']) {
            $error = _x('The file %s was larger than the maximum allowed file size of %s bytes. The file was not uploaded.', 'upload files', 'AWPCP');
            $error = sprintf($error, '<strong>' . $filename . '</strong>', $constraints['max_image_size']);
            return false;
        }
        if ($file_size < $constraints['min_image_size']) {
            $error = _x('The size of %1$s was too small. The file was not uploaded. File size must be greater than %2$d bytes.', 'upload files', 'AWPCP');
            $error = sprintf($error, '<strong>' . $filename . '</strong>', $constraints['min_image_size']);
            return false;
        }
        $img_info = getimagesize($tmpname);
        if (!isset($img_info[0]) && !isset($img_info[1])) {
            $error = _x('The file %s does not appear to be a valid image file.', 'upload files', 'AWPCP');
            $error = sprintf($error, '<strong>' . $filename . '</strong>');
            return false;
        }
        if ($img_info[0] < $constraints['min_image_width']) {
            $error = _x('The image %s did not meet the minimum width of %s pixels. The file was not uploaded.', 'upload files', 'AWPCP');
            $error = sprintf($error, '<strong>' . $filename . '</strong>', $constraints['min_image_width']);
            return false;
        }
        if ($img_info[1] < $constraints['min_image_height']) {
            $error = _x('The image %s did not meet the minimum height of %s pixels. The file was not uploaded.', 'upload files', 'AWPCP');
            $error = sprintf($error, '<strong>' . $filename . '</strong>', $constraints['min_image_height']);
            return false;
        }
    } else {
        if ($file_size > $constraints['max_attachment_size']) {
            $error = _x('The file %s was larger than the maximum allowed file size of %s bytes. The file was not uploaded.', 'upload files', 'AWPCP');
            $error = sprintf($error, '<strong>' . $filename . '</strong>', $constraints['max_attachment_size']);
            return false;
        }
    }
    $newname = awpcp_unique_filename($tmpname, $filename, array($paths['files_dir'], $paths['thumbnails_dir']));
    $newpath = trailingslashit($paths['files_dir']) . $newname;
    if ($action == 'upload' && !@move_uploaded_file($tmpname, $newpath)) {
        $error = _x('The file %s could not be moved to the destination directory.', 'upload files', 'AWPCP');
        $error = sprintf($error, '<strong>' . $filename . '</strong>');
        return false;
    } else {
        if ($action == 'copy' && !@copy($tmpname, $newpath)) {
            $error = _x('The file %s could not be copied to the destination directory.', 'upload files', 'AWPCP');
            $error = sprintf($message, '<strong>' . $filename . '</strong>');
            return false;
        }
    }
    if (in_array($mime_type, awpcp_get_image_mime_types())) {
        if (!awpcp_create_image_versions($newname, $paths['files_dir'])) {
            $error = _x('Could not create resized versions of image %s.', 'upload files', 'AWPCP');
            $error = sprintf($error, '<strong>' . $filename . '</strong>');
            # TODO: unlink resized version, thumbnail and primary image
            @unlink($newpath);
            return false;
        }
    }
    @chmod($newpath, 0644);
    return array('original' => $filename, 'filename' => awpcp_utf8_basename($newpath), 'path' => str_replace($paths['files_dir'], '', $newpath), 'mime_type' => $mime_type);
}