dongzha5934 2015-12-22 14:21
浏览 47

使用WordPress将文件上载到特定文件夹

I need help for how to upload a file in WordPress, I don't want to upload files into the default path of media files i.e. into uploads/../.., I want to upload my files into wp-content/uploads/my_folder, I just created one file in wp-admin folder and added some functionality there, from that form I want to upload my file (not creating plugin).

Is it possible to do like this? If yes then how? If no then what I want to do for uploading file?

I tried the following solution for it:

    $path_array  = wp_upload_dir();
    $upload_path = $path_array['baseurl'].'/myfoldername/';
    $target_path = $upload_path."/".$file_name;
    $file_name = $_FILES['fieldname']['name'];
    $tmp_name = $_FILES["fieldname"]["tmp_name"];

    upload_user_file($_FILES,$upload_path); // Called this function

In functions.php of my theme, I defined the above called function upload_user_file() like as follows:

function upload_user_file( $file = array(),$path) {
    if(!empty($file)) 
    {
        $uploaded=move_uploaded_file($file['fieldname']['tmp_name'],$path.$file['fieldname']['name']);
       if($uploaded) 
       {
           echo "Uploaded successfully ";
       }
       else
       {
            echo "Some error in upload ";
            print_r($file['error']);
       }
    }
}

Please help me for this issue.

Thanks.

  • 写回答

1条回答 默认 最新

  • dousu1916 2017-08-30 12:33
    关注

    It's not working because of FILES array loses its values very soon.. Upload it in the same code where you called function it will work...

    Thanks cale_b its really helpful.

    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像