user2201321
user2201321

Reputation: 11

file-upload seems to work, but file isn't in destination folder

So I am working on a project and I am stuck trying to upload a file. I have gone through multiple tutorials and copied the code into netbeans to see if it would find any errors. Everything seems okay, but the file isn't in the destination folder after uploading it and I can't figure out why.

I am ajax POSTing the file. Here is my html and jQuery code:

<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>

The post gets posted to my main.php on my server. This is the code for that.

<?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"];
          }
        }
}
?>

I can't figure out what the heck the problem is. I spent a ton of time going through tutorials, trying to get this to work. If anyone could help it would be greatly appreciated!

Upvotes: 1

Views: 645

Answers (1)

Musa
Musa

Reputation: 97707

You have a hard coded destination in you move uploaded file move_uploaded_file($_FILES["file"]["tmp_name"],"C:/inetpub/wwwroot/test1/afterbreak/uploads/test.csv");. I think you want "uploads/" . $_FILES["file"]["name"].

Upvotes: 1

Related Questions