WP_UPLOAD_BITS仅上传特定大小

时间:2016-02-01 作者:Pamela

对于使用此代码上载的图像,我只想上载全尺寸图像和缩略图(而对于通过网站上载的图像,我需要所有尺寸)。但是,我不知道如何使其仅上载全尺寸/缩略图,因为此代码当前仅上载所有尺寸的图像:

$attachment = array(
            \'post_author\' => "test", 
            \'post_date\' => $date,
            \'post_date_gmt\' => gmdate(\'Y-m-d H:i:s\', strtotime($date) ),
            \'post_title\' => "just some title", 
            \'post_status\' => \'inherit\',
            \'comment_status\' => \'closed\',
            \'ping_status\' => \'closed\',
            \'post_name\' => "just some picture",                                           
            \'post_modified\' => $date,
            \'post_modified_gmt\' => gmdate(\'Y-m-d H:i:s\', strtotime($date) ),
            \'post_type\' => \'attachment\',
            \'guid\' =>  $upload[\'url\'],
            \'post_mime_type\' => $wp_filetype[\'type\'],
            \'post_excerpt\' => \'\',
            \'post_content\' => \'\'
        );

        $upload = wp_upload_bits( "picture.jpg", null, file_get_contents("picture.jpg") );
        if ( $upload[\'error\'] )
            echo "Failed uploading: " . $upload[\'error\'];
我能想到的唯一方法就是attachment_id 然后unlink 除了全尺寸和缩略图以外,所有尺寸都有,但看起来很凌乱。这样做的方法是什么?

1 个回复
SO网友:Zaheer Abbas
This is how you can limit the image upload size

function fineima() {
  if (isset($_POST[\'submit\'])) {
    wp_upload_bits($_FILES[\'fileToUpload\'][\'name\'], null, file_get_contents($_FILES[\'fileToUpload\'][\'tmp_name\']));
  }
  echo \'<form action="" method="post" enctype="multipart/form-data">
      <input type="file" name="fileToUpload" id="fileToUpload">
      <input type="submit" value="Upload Image" name="submit">
    </form>
  \';
}
function fineFileUploaderRenderer() {
  ob_start();
  fineimageUpload();
  return ob_get_clean();
}
add_shortcode(\'your_file_uploader\', \'fineFileUploaderRenderer\');