文件上传似乎工作,但文件不在目标文件夹中

时间:2013-03-23 02:33:08

标签: php jquery file-upload asyncfileupload

所以我正在研究一个项目,我不敢尝试上传文件。我已经完成了多个教程,并将代码复制到netbeans中,看看是否会发现任何错误。一切似乎都没问题,但上传后该文件不在目标文件夹中,我无法弄清楚原因。

我是ajax POST文件。这是我的html和jQuery代码:

<html>
<head>
    <Script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>

    <script language="javascript" type="text/javascript">

        function submit(){

            var $request;
            var $data = new FormData(document.forms.namedItem("upload")); 

            // abort pending request
            if ($request) {
                $request.abort();
            }

            var $form = $("#form");
            var $file = $("#file");

                // request to /main.php
                var $request = $.ajax({
                    url: "main.php",
                    type: "post",
                    data: $data,
                    processData: false, //tell jQuery not to process the data
                    contentType: false //tell jQuery not to set the contentType
                });

                // on success
                $request.done(function (response, textStatus, jqXHR){
                    $("#responseField").html(response).show();
                });

                //on failure
                $request.fail(function (jqXHR, textStatus, errorThrown){
                    console.error(
                        "The following error occured: "+
                        textStatus, errorThrown
                    );
                });

    };
    </script>

</head>
<body>
    <h1>Prototype Upload Page</h1><br/>
    <form enctype="multipart/form-data" method="post" id="upload">
        Select .csv file: <input type="file" name="file" id="file">
    </form>
    <input type="submit" id="submit" name="submit" value="submit" onclick="submit()"/>
    <br><textarea id="responseField" rows="4" cols="75"> file upload response: </textarea><br/>
</body>

帖子被发布到我服务器上的main.php。这是代码。

<?php

if (!empty($_FILES["file"]["name"])) {
    uploadFile();
}

Function uploadFile() {
    if ($_FILES["file"]["error"] > 0) {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
        }
      else {
        echo "Upload: " . $_FILES["file"]["name"] . "\n";
        echo "Type: " . $_FILES["file"]["type"] . "\n";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) .  "kB\n";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "\n";

        if (file_exists("uploads/" . $_FILES["file"]["name"])) {
          echo $_FILES["file"]["name"] . " already exists. ";
          }
        else {
          move_uploaded_file($_FILES["file"]["tmp_name"],"C:/inetpub/wwwroot/test1/afterbreak/uploads/test.csv");
          echo "Stored in: " . "uploads/" .$_FILES["file"]["name"];
          }
        }
}
?>

我无法弄清楚问题是什么。我花了很多时间浏览教程,试图让它工作。如果有人能提供帮助,我们将不胜感激!

1 个答案:

答案 0 :(得分:1)

您移动上传的文件move_uploaded_file($_FILES["file"]["tmp_name"],"C:/inetpub/wwwroot/test1/afterbreak/uploads/test.csv");时有一个硬编码目的地。我想你想要"uploads/" . $_FILES["file"]["name"]