【问题标题】:External file upload including source url as file name?外部文件上传,包括源网址作为文件名?
【发布时间】:2019-01-19 16:27:37
【问题描述】:

目前我正在使用wp_upload_bits 将图像从外部上传到我的网站并且它现在可以正常工作,但是图像将我们的源 URL 作为文件名。

$post['image'] = $_POST['image'];
$upload = wp_upload_bits($_POST['image'], null, file_get_contents($_POST['image']));
$filename = $upload['file'];
$wp_filetype = wp_check_filetype($filename, null );
$attachment = array(
    'post_mime_type' => $wp_filetype['type'],
    'post_title' => 'cover',
    'post_content' => '',
    'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
$attach_data = wp_generate_attachment_metadata( $attach_id, $hello );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $post_id, $attach_id );

以上代码在我们的 WordPress 网站中返回的内容与此类似。

我该如何解决这个问题,以便我可以通过字符串设置我的文件名?

【问题讨论】:

    标签: php wordpress rename


    【解决方案1】:

    更改:$filename = $upload['file'];

    收件人:$filename = basename($upload['file']);

    那么它将被存储为:llYtuLT-10.jpg

    【讨论】:

    • 旁注,您可能需要检查其上传 PHP 文件是否安全,file_get_contents($_POST['image']) 看起来很危险..
    • 这不起作用,现在已经修了10分钟没有运气,当我把它改成这个时,图像根本不显示。
    猜你喜欢
    • 2013-11-29
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多