【问题标题】:Wordpress image upload from frontend throws 500 internal server error从前端上传 Wordpress 图片会引发 500 内部服务器错误
【发布时间】:2016-06-25 14:19:12
【问题描述】:

我有一个带有前端表单的网站,用户可以在其中添加新帖子。该表单具有一些基本的帖子详细信息和一个特色图像字段。该表单还有一些图像字段,由 acf 控制。

现在的问题是,当我使用 png 之类的图像时,一切正常。但是当我使用像 jpg 这样的图像时,它会出现内部服务器错误。在 acf 中,我将接受的文件格式添加为“jpg、png、jpeg”。但我不确定如何为特色图片添加文件格式。

这是我正在使用的代码。在这里,第一张图片将始终是特色图片。

        foreach( $_FILES as $file ) {
            if( is_array( $file ) ) {
                if($count==1){
                    $featured_image_id = upload_user_file( $file );
                }else{
                    $item_image[] = upload_user_file( $file );

                }
            }
            $count++;
          }

这里是upload_user_file函数

function upload_user_file( $file = array() ) {
    require_once( ABSPATH . 'wp-admin/includes/admin.php' );
      $file_return = wp_handle_upload( $file, array('test_form' => false ) );
      if( isset( $file_return['error'] ) || isset( $file_return['upload_error_handler'] ) ) {
          return false;
      } else {
          $filename = $file_return['file'];
          $attachment = array(
              'post_mime_type' => $file_return['type'],
              'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
              'post_content' => '',
              'post_status' => 'inherit',
              'guid' => $file_return['url']
          );
          $attachment_id = wp_insert_attachment( $attachment, $file_return['url'] );
          require_once(ABSPATH . 'wp-admin/includes/image.php');
          $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
          wp_update_attachment_metadata( $attachment_id, $attachment_data );
          if( 0 < intval( $attachment_id ) ) {
            return $attachment_id;
          }
      }
      return false;
}

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    好的,我解决了。由于某种原因,acf 中允许的文件类型对我不起作用。所以我只是删除了类型(即允许所有文件类型),一切都恢复了活力。我知道这可能并不适合所有人,但我想这会为您指明问题所在的正确方向。

    【讨论】:

    • 嘿,你能详细说明一下吗?我很确定我遇到了和你一样的问题。
    • 图片字段中有一个选项叫做“允许文件类型”。这就是文档所说的“为特定文件类型添加上传验证。输入逗号分隔列表以指定允许哪些文件类型或为所有类型留空。在 v5.1.9 中添加”。因此,如果您将其留空,它将允许所有文件类型。在极少数情况下,wordpress htaccess 文件没有更新,因此请尝试转到设置 > 永久链接并单击保存。这将更新您的 htaccess 文件。
    猜你喜欢
    • 1970-01-01
    • 2013-07-06
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    相关资源
    最近更新 更多