【发布时间】: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