0

I want to ulpload JSON and txt files to the server with a size of 200KB.

This is the script I'm using:

$allowedExts = array("json", "txt");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);

if ((($_FILES["file"]["type"] == "application/json")
        || ($_FILES["file"]["type"] == "text/txt"))
    && ($_FILES["file"]["size"] < 200000)
    && in_array($extension, $allowedExts)
) {
    if ($_FILES["file"]["error"] > 0) {
        echo "Código do erro: " . $_FILES["file"]["error"] . "<br>";
    } else {
        echo "Upload: " . $_FILES["file"]["name"] . "<br>";
        echo "Type: " . $_FILES["file"]["type"] . "<br>";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
        move_uploaded_file($_FILES["file"]["tmp_name"],
            "upload" . $_FILES["file"]["name"]);
        echo "Gravado em: " . $diretorio . $_FILES["file"]["name"];
    }
} else {
    echo "Invalid file.";
}

But I always get the "Invalid file" error. Why is it?

7
  • Use print_r($_FILES) to find out. Commented Oct 9, 2013 at 22:19
  • Which tells you that no file was uploaded. I wonder why you didn't get a notice. Have you perhaps posted your question about non-working code without enabling error_reporting beforehand?! Commented Oct 9, 2013 at 22:23
  • Please post the HTML form you're using. Commented Oct 9, 2013 at 22:25
  • I'm using <input id="fileupload" type="file" name="upload"/> With the following Jquery: $('#fileupload').change(function(){ window.location.href = 'upload.php'; $console.text("File uploaded"); }); Commented Oct 9, 2013 at 22:27
  • @mario: I get Notice: Undefined index: file. You're right... Commented Oct 9, 2013 at 22:28

2 Answers 2

1

You aren't actually ever submitting your form. Your javascript simply loads a new page in the browser window (upload.php) when you change the value of upload field.

Please read this article on how to upload files using jQuery, as it is not as straightforward as one would hope.

How can I upload files asynchronously?

Or, if your intent is to actually redirect to this a new page (rather than upload a file without reload of page in browser), then you can simply do a traditional non-AJAX form for the file upload as is outlined in the answer by @OlafErlandsen here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, the idea is to upload the file asynchronously, I didn't expect it to be this difficult. The article will help me solve the problem.
0

in your HTML form use enctype:

<form method="post" action="" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" value="Upload" />
</form>

And use this content type( to avoid problems ):

$types = array('application/json','application/x-javascript','text/javascript','text/x-javascript','text/x-json');
if( in_array( $_FILES['file']['type'] , $types ) )

And, if you use jQuery:

var formData = new FormData($('form')[0]);
$.ajax({
    url: 'file.php',
    type: 'POST',
    success: function ( response ){ console.log( "ok" ); },
    data: formData,
    cache: false,
    contentType: false,
    enctype: 'multipart/form-data',
    processData: false
});

And, try upload with this class:

https://github.com/erlandsen/upload

Good Luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.