我想将文件上传到Wordpress中的uploads文件夹

时间:2020-05-27 06:35:42

标签: wordpress file-upload

我尝试过但我无法解决它 这是我的代码

<form enctype="multipart/form-data" id="patientEnquiry" method="post" class="form" action="" > 

<div class="form-group">
<input type="text" name="subject" id="subject"  required="required" maxlength="100" class="form- 
 control">
 </div>   
 <input type="file" id="passport" name="passport" class="required">
 </form>
 <button class="request"  id="patientsub" >submit</button>

这就是我放在functions.php中的内容

$target_dir = get_home_path().'wp-content/uploads/';

$file = $_FILES['passport']['name'];
$path = pathinfo($file);
$filename = $path['filename'];
$ext = $path['extension'];
$temp_name = $_FILES['passport']['tmp_name'];
$path_filename_ext = $target_dir.$filename.".".$ext;

// Check if file already exists
if (file_exists($path_filename_ext)) {
echo "Sorry, file already exists.";
}else{
move_uploaded_file($temp_name,$path_filename_ext);
echo "Congratulations! File Uploaded Successfully.";
}

当我尝试回显$ file时;我什么都没收到,请帮助我

1 个答案:

答案 0 :(得分:0)

也许问题出在获取您需要使用绝对路径的路径。

$target_dir = str_replace('\\', '/', ABSPATH)."wp-content/uploads/";