【问题标题】:wordpress wp_handle_upload not moving the filewordpress wp_handle_upload 不移动文件
【发布时间】:2017-10-01 19:49:41
【问题描述】:

谁能告诉我为什么这个函数没有将上传的文件移动到任何地方。

function handle_logo_upload($option){
    if(!function_exists('wp_handle_upload'))
{

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


            if(!empty($_FILES["site_logo_custom"])){

$theFile=$_FILES["site_logo_custom"];

$overrides=array('test_form'=>false);

$urls=wp_handle_upload($theFile,$overrides);
$temp=$urls["url"];
return $temp;

                                    }


                                    return $option;
                                }

我真的找不到太多关于 wp_handle_upload 函数的信息。 谢谢!!!

【问题讨论】:

  • 这个函数在哪里?而且它没有被我看到的人调用?您如何将此功能作为表单操作运行?
  • 嗨 Sajjadur,它在我主题的功能模板中。这是设置页面的一个字段,我在谷歌搜索了一段时间后在一些教程中发现了这种方法。我找到了另一种基于 jQuery 的方法,但我更喜欢将数据集合留在 php 上。

标签: wordpress upload


【解决方案1】:

我假设你的表格格式有点像这样:

form action="" enctype="multipart/form-data" method="post"> //action is current post
 <input type="file" name="file">
 <input type="submit" name="submit">
</form>

并使用 wp_handle_upload() 将文件上传到 wordpress 上传文件夹;您可以使用以下代码的功能....

function handle_logo_upload($file){

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

$movefile = wp_handle_upload($uploadedfile, array('test_form' => false)); 

if ( $movefile ){
    echo $movefile['url'];
    //or return
    return $movefile['url'];
  }

}
if (isset($_POST['submit'])) {
   handle_logo_upload($_FILES['file']);
}

【讨论】:

  • 如果你想进行额外的安全检查,那么你可以在你的函数中进行。为了简单起见,我没有添加安全检查。
  • 嗨 Sajjadur,我的代码是一样的。我在设置 API 中创建的表单中使用它。我的意图是在主题设置页面中设置一个徽标上传按钮。
  • 然后使用wordpress媒体上传器获取图片的url并将其保存到{prefix}_settings数据库中......
  • 我看了一下,那只是一个 jquery 插件,让事情变得更加清晰和流畅,它仍然无法解决我上传的混乱。
  • 可能是基于特定站点的问题,然后导致代码在我的站点上运行..然后就在 wp_debug 上并获得错误结果。
【解决方案2】:

猜测你的函数被完美调用了。

function handle_logo_upload($option){
    if(!function_exists('wp_handle_upload'))
    {

        require_once(ABSPATH .'wp-admin/includes/file.php');
    }
    //you are using empty version make sure your php version is higher than 5.2
    if(!empty($_FILES["site_logo_custom"])){
        $move_logo = wp_handle_upload( $_FILES["site_logo_custom"], array('test_form' => false) );
        if ( $move_logo && !isset($move_logo['error']) ) {
            $wp_upload_dir = wp_upload_dir();
            $attachment = array(
                'guid' => $wp_upload_dir['url'] . '/' . basename($move_logo['file']),
                'post_mime_type' => $move_logo['type'],
                'post_title' => preg_replace( '/\.[^.]+$/', '', basename($move_logo['file']) ),
                'post_content' => '',
                'post_status' => 'inherit'
            );
            $logo_attach_id = wp_insert_attachment($attachment, $move_logo['file']);
            $image_attributes = wp_get_attachment_image_src( $logo_attach_id );
            if ( $image_attributes ) {
              return $image_attributes[0]; 
             }
             else
             {
                return $option;
             }
        }else{
            return $option;
        }
    }else{
        return $option;
    }

}          

请阅读我用代码编写的 cmets

【讨论】:

  • 再次返回,使用您提供的功能 Arkshay 发生了一些变化,但仍然没有用。我的 php 版本是 7.1.1 。目前该函数返回此字符串:88974.ngsversion.1463419670135.adapt.676.1.jpg。它似乎被保存为一个选项。我可以使用 get_option('site_logo_custom') 来调用它。但我在选项数据库表中找不到它。该文件仍未移动到任何地方。
  • $image_attributes,打印 r 你是否获得了该数组中的完整图像路径?请检查
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-17
  • 1970-01-01
  • 2018-02-21
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
相关资源
最近更新 更多