0

I've got a problem with an empty $_POST array while i'm uploading a file.

Here's the form :

<div class="popup">
    <h3>UPLOAD</h3>
        <a class="close" href="#">&times;</a>
        <div class="content">
            <form
                id="uploadForm"
                method="post" 
                enctype="multipart/form-data" 
                action="resources/upload.php?dir=<?php if(isset($_GET['dir'])){echo $_GET['dir'];} ?>" 
                enctype="multipart/form-data">

                <input type="file" name="fichier" id="file" class="inputfile"/>
                <label for="file">Choisir un fichier</label><br>
            </form>
            <button onClick="submitUpload();" id="smbtBtt">Uploader</button>
        </div>
</div>

This is the submitUpload fonction :

function submitUpload()
{
    if(document.getElementById("file").value != ""){
        document.getElementById("uploadForm").submit();
    }
    else
    {
        alert("Please select a file!");
    }

}

Then in the upload.php file, i just var_dump() the array $_FILES and $_POST

It's 6AM in the morning, my 5h straight of programming, so that's why i'm asking :).

Thanks a lot !

7
  • you have use var formData= new formdata(); function to get all file data.. Commented Feb 13, 2017 at 5:39
  • use submit button before form closed..check this change Commented Feb 13, 2017 at 5:46
  • @Gulshan Doesn't change anything... :/ Commented Feb 13, 2017 at 5:50
  • 1
    you need to check $_FILES rather than $_POST Commented Feb 13, 2017 at 5:53
  • You are using file type field elements in your form. So you need to use $_FILES instead of $_POST Commented Feb 13, 2017 at 5:54

4 Answers 4

1

Here in your form, you are using file type field element. You have to use '$_FILES' instead of $_POST. Try this

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

Comments

1

Sorry guys, I wasn't able to make the $FILES and the $POST arrays work at the same time, so I find an other solution without one of them. Still tanks to every one.

3 Comments

what solution you found.There must be some error in your code because of which you are not able to use POST and FILE at the same time
in your current code you don't have anything else apart from file and that's why $_POST is empty
@Anant That's ok, I've resolved it. Now i got some ACL errors ... T_T
0

use this code. May be it helps you

$('form#uploadForm').submit(function(e){
    var formData = new FormData(this);

$.ajax({
url: "upload_file.php",            
type: "POST",
data:  formData,
mimeType:"multipart/form-data",
contentType: false,
cache: false,
processData:false,
success: function(data)
{
    alert(data);
}
});
return false;
});

2 Comments

Can I still use my fields as before ?
@Alteus check formData variable..it get your all data of input fileds
0
<div class="popup">
    <h3>UPLOAD</h3>
        <a class="close" href="#">&times;</a>
        <div class="content">
            <form
                id="uploadForm"
                method="post" 
                enctype="multipart/form-data" 
                action="resources/upload.php?dir=<?php if(isset($_GET['dir'])){echo $_GET['dir'];} ?>" 
                enctype="multipart/form-data">

                <input type="file" name="fichier" id="file" class="inputfile"/>
                <label for="file">Choisir un fichier</label><br>
            </form>
            <button onClick="submitUpload();" id="smbtBtt">Uploader</button>
        </div>
</div>

<script type="text/javascript">
    function submitUpload()
{
    if(document.getElementById("file").value != ""){
        document.getElementById("uploadForm").submit();
    }
    else
    {
        alert("Please select a file!");
    }
}
</script>

Upload.php

<?php
$test=$_FILES["fichier"]["tmp_name"];

echo $test;

?>

2 Comments

I need to user the POST methode. You can't transfer files with the GET one. And It's my POST array in php that is empty..
you can get $_GET['fichier'] using get method.. if you are using POST method i will update answer as soon as possible

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.