duanjue9296 2017-06-22 06:32
浏览 80
已采纳

无法通过自定义WordPress插件上传文件

I am developing custom WordPress plugin. In this plugin, I am trying to upload files into wp-content\uploads\passports (folder path).
I don't face any error message when uploading file. Although it shows File has been uploaded successfully. message, but there is no any uploaded file in the folder.
Here is my code:

trip-form.php

<tr><td cospan="4">Please attach a scanned copy of your:</td></tr> 
<tr>
  <td>Passport:&nbsp;</td>
  <td colspan="3"><input type="file" name="passport" id="passport"/></td>
</tr>
<tr>
    <td></td>
    <td colspan="3"><div id="dropBox">
        <p>Select file to upload</p>
    </div></td>
</tr>

script.js

// Add events
$j('input[type=file]').on('change', fileUpload);

// File uploader function
function fileUpload(event){
    alert('fileUpload');
    //notify user about the file upload status
    $j("#dropBox").html(event.target.value+" uploading...");

    //get selected file
    files = event.target.files;

    //form data check the above bullet for what it is  
    var data = new FormData();         

    var web_url = document.location + '';
    var path = web_url.substring(0,web_url.lastIndexOf('/'));

    path = path.substring(0,path.lastIndexOf('/'));
    path = path + '/wp-content/plugins/trip-form/pages/uploadfile.php';     

    //file data is presented as an array
    for (var i = 0; i < files.length; i++) {
        var file = files[i];

        var fileExtension = ['jpeg', 'jpg', 'pdf'];
        if ($j.inArray($j('#passport').val().split('.').pop().toLowerCase(), fileExtension) == -1) {
            $j("#dropBox").html("Only formats are allowed : "+fileExtension.join(', '));
        }else{
            //append the uploadable file to FormData object
            data.append('file', file, file.name);

            //create a new XMLHttpRequest
            var xhr = new XMLHttpRequest();     

            //post file data for upload
            xhr.open('POST', path, true);  
            xhr.send(data);
            xhr.onload = function () {
                //get response and show the uploading status
                var response = JSON.parse(xhr.responseText);
                if(xhr.status === 200 && response.status == 'ok'){
                    $j("#dropBox").html("File has been uploaded successfully.");
                }else{
                    $j("#dropBox").html("Some problem occured, please try again.");
                }
            };
        }
    }
}

uploadfile.php

<?php
    $parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
    require_once( $parse_uri[0] . 'wp-load.php' );


    //generate unique file name
    $fileName = time().'_'.basename($_FILES["file"]["name"]);

    //file upload path, targetdir is correct
    $targetDir = "../../../uploads/passports/";
    $targetFilePath = $targetDir . $fileName;

    //upload file to server
    if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){
    //if (file_exists ( $targetDir."testing.txt" )){
        $response['status'] = 'ok';
    }else{
        $response['status'] = 'err';
    }

    //render response data in JSON format
    echo json_encode($response);
?>

I have no idea what is wrong. Any help would be appreciated.
Thanks.

  • 写回答

1条回答 默认 最新

  • duanqie5741 2017-06-22 06:40
    关注

    Can you please change your uploads folder permission. Make it 755.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R