Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I found a nice upload script with a progress bar. But unfortunately it will not work for me:( I always get an error: Undefined index: file1 in C:\wamp\www\upload\upload_parser.php on line 7. I spend already a couple hours to get rid of it with any success. But the script supposed to work. Does anyone has an idea why I get this error? (FYI: I downloaded it from http://www.developphp.com/view.php?tid=1351) I already checked php.ini. A print_r of $_Files shows me an empty error. I read something about that it isn't possible to upload images via Ajax. But the script works for other users. Totally weird.

I appreciate every hint. Thank you very much in advanced.

<!DOCTYPE html>
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width">
        <script>

            function _(el) {
                return document.getElementById(el);
            }

            function uploadFile() {
                 var file = _("file1").files[0];
                alert (file.name+" - "+file.size+" - "+file.type);
                var formdata = new FormData();
                formdata.append("file1", file);
                var ajax = new XMLHttpRequest();
                ajax.upload.addEventListener("progress", progressHandler, false);
                ajax.addEventListener("load", completeHandler, false);
                ajax.addEventListener("error", errorHandler, false);
                ajax.addEventListener("abort", abortHandler, false);
                ajax.open("POST", "upload_parser.php");
                ajax.send();
            }

            function progressHandler (event) {
                _("loaded_n_total").innerHTML = "Uploaded "+event.loaded+" bytes of "+event.total;
                var percent = (event.loaded / event.total) * 100;
                _("progressBar").value = Math.round(percent);
                _("status").innerHTML = Math.round(percent) +"% uploaded... please wait";
            }

            function completeHandler (event) {
                _("status").innerHTML = event.target.responseText;
                _("progressBar").value = 0;
            }

            function errorHandler (event) {
                _("status").innerHTML = "Upload Failed";
            }

            function abortHandler (event) {
                _("status").innerHTML = "Upload Aborted";
            }


        </script>
    </head>
    <body>
        <div>File Upload</div>
        <form id="upload_form" enctype="multipart/form-data" method="post">
            <input type="file" name="file1" id="file1"><br>
            <input type="button" value="Upload File" onclick="uploadFile()">
            <progress id="progressBar" value="0" max="100" style="width: 300px;"></progress>
            <h3 id="status"></h3>
            <p id="loaded_n_total"></p>
        </form>
    </body>
</html>

And the upload_parser.php file:

<?php
print_r($_FILES);

$fileName = $_FILES["file1"]["name"];
$fileTmpLoc = $_FILES["file1"]["tmp_name"];
$fileType = $_FILES["file1"]["type"];
$fileSize = $_FILES["file1"]["size"];
$fileErrorMsg = $_FILES["file1"]["error"];

if (!$fileTmpLoc) {
    echo "ERROR! Please browse for a file before clicking the upload button.";
    exit();
}

if (move_uploaded_file($fileTmpLoc, "uploads/$fileName")) {
    echo "$fileName upload is complete";
}
else {
    echo "move_uploaded_file function failed";
}

?>
share|improve this question
 
Have you tried commenting out the error giving line? –  Hego555 Nov 5 at 5:55
 
Thank you, but I can't comment it out. The php file wouldn't make any sense anymore. For clearfication, all $_FILES are empty: Notice: Undefined index: file1 in C:\wamp\www\upload\upload_parser.php on line 7, 8, 9, 10, 11 –  Stefan Nov 5 at 6:10
 
It sounds like a permission issue, try checking whether PHP has read/write permissions in the directory. –  Hego555 Nov 5 at 7:13
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.