php media uploadimg,WordPress上传图像函数media_handle_upload详解

重要:本文最后更新于2020-05-15 08:25:52,某些文章具有时效性,若有错误或已失效,请在下方留言或联系代码狗。

最近几篇文章好像涉及到wordpress文件上传的功能,为了方便大家学习wordpress,以及对大家的网站安全考虑,在此介绍下wordpress官方提供的媒体上传函数media_handle_upload。使用它你不必考虑像PHP的文件上传函数那样是否会存在传马漏洞,wordpress的开发者已经帮你写好了验证,下面一起来学习下吧。

函数介绍

media_handle_upload( string $file_id, int $post_id, array $post_data = array(),array $overrides = array('test_form' => false) )

保存post上传的文件,并绑定一篇文章。

原型

函数位于wp-admin/includes/media.php文件中,在线地址:

https://developer.wordpress.org/reference/files/wp-admin/includes/media.php/

参数

$file_id

(string) (必须) 文件发送的$_FILES 数组的索引。

$post_id

(int) (必须) 要将媒体项目附加到的帖子的POST ID。必需的,但可以设置为0,创建与POST无关的媒体项。

$post_data

(array) (可选) 覆盖一些附件。默认值: array()

$overrides

(array) (可选) 重写函数wp_handle_upload() 。默认值: array(‘test_form’ => false)

返回值

(int|WP_Error) 成功返回附件ID,失败返回wp_error错误对象。

使用示例

从网站前端的表单上传附件。上传表单可能如下所示:

PHP保存文件代码:

// Check that the nonce is valid, and the user can edit this post.

if (

isset( $_POST['my_image_upload_nonce'], $_POST['post_id'] )

&& wp_verify_nonce( $_POST['my_image_upload_nonce'], 'my_image_upload' )

&& current_user_can( 'edit_post', $_POST['post_id'] )

) {

// The nonce was valid and the user has the capabilities, it is safe to continue.

// These files need to be included as dependencies when on the front end.

require_once( ABSPATH . 'wp-admin/includes/image.php' );

require_once( ABSPATH . 'wp-admin/includes/file.php' );

require_once( ABSPATH . 'wp-admin/includes/media.php' );

// Let WordPress handle the upload.

// Remember, 'my_image_upload' is the name of our file input in our form above.

$attachment_id = media_handle_upload( 'my_image_upload', $_POST['post_id'] );

if ( is_wp_error( $attachment_id ) ) {

// There was an error uploading the image.

} else {

// The image was uploaded successfully!

}

} else {

// The security check failed, maybe show the user an error.

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在el-upload组件中实现带参上传,你可以按照以下步骤进行操作: 1. 首先,在el-upload组件上添加属性:action,指定上传的地址。例如:action="/uploadController/uploadImg"。 2. 其次,创建一个对象,用于存储上传所需的参数。例如,你可以在data()方法中添加一个uploadParams对象,包含需要传递的参数。比如:uploadParams: { param1: value1, param2: value2 }。 3. 然后,将上传参数作为el-upload组件的data属性的一部分。例如:data="{ param1: uploadParams.param1, param2: uploadParams.param2 }"。 4. 最后,在触发上传的时候,调用submit()方法。例如,你可以在methods中定义一个submitImg()方法,并在该方法内部使用this.$refs.upload.submit()来触发上传。 这样,当你选择图片并触发上传时,el-upload组件会将带有参数的请求发送到指定的地址。请确保你的服务器端能够正确解析和处理这些参数。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [【前端相关】elementui使用el-upload组件实现自定义上传](https://blog.csdn.net/weixin_44299027/article/details/130840926)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [关于el-upload上传参数,后台接受对应参数的方法](https://blog.csdn.net/weixin_41753664/article/details/116673917)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值