I have a simple HTML form containing a file input. When the form is submitted without a file, printing the $_POST array shows me all of the data submitted. When a file is submitted, however, $_POST doesn't print out any of the submitted data.
Can somebody tell me why? This is my code:
<?php
print_r($_POST);
?>
<form action="test.php" method="post" enctype="multipart/form-data">
<label for="myfile">Video File:</label>
<input type="file" name="myfile" />
<br /><br />
<label for="mytitle">Title:</label><br />
<input type="text" name="mytitle" size="55" maxlength="60" />
<br /><br />
<input type="submit" name="mysubmit" value="Submit Video for Approval" />
</form>
Your script seems fine. Please check your server configuration. Perhaps you exceed POST limits (set with post_max_size in php.ini)
You have to use $_FILES to access the uploaded files.
var_dump($_FILES); // Your uploaded files
var_dump($_POST); // Your entered data
Related
i have an issue with sending files form in magento CE 1.9.2.1. When i trying to attach files in php $_FILES array is empty but sometimes not. Please help me to find solution. Here is html form:
<form action="<?php echo $this->getUrl('quote/index/save', array('_secure'=>true)); ?>" id="get_a_quote" method="post" name="get_a_quote" enctype="multipart/form-data">
<div id="wizard">
<label for="attachment">Attachment</label>
<input type="file" name="attachment[]" multiple="multiple" />
<input id="submit" type="submit" name="save" value="Get A Quote" />
</div>
</form>
in IndexController my save action code is:
print_r($_FILES); die;// result Array() but sometimes Array(bla bla bla)
My server info is:
post_max_size = 8M
upload_max_filesize = 16М
<form method="post" enctype="multipart/form-data">
<h3>Add video</h3>
<div class="field_wrap">
<p>Name</p>
<input type="text" name="title"/>
</div>
<div class="field_wrap">
<p>Video</p>
<input name="video" type="file" />
</div>
<p><button>Add</button></p>
</form>
This is the form I use to upload a video file and a title, so when I DON'T upload a file the $_POST variable is filled with form data, but If I upload a file the $_POST variable is empty, why is that?
You are looking in the wrong spot, try looking in $_FILES not $_POST
<p><button>Add</button></p>
should be
<p><input type="submit" name="submit" value="Add"></p>
Using this all the post data is accessible as
$_POST and file data with $_FILES
I've got the following form:
<form method="post" action="index.php">
product name:
<input type="text" name="product_name" value="<?php echo $product_name;?>"/>
<br /> <br />
product details
<textarea rows = "6" cols = "30" name="product_details" > <?php echo $product_details;?></textarea>
<br /> <br />
product price
<input type="text" name = "product_price" value="<?php echo $product_price;?>"/>
<br /> <br />
CN:
<input type="text" name = "product_cn" value="<?php echo $product_cn;?>"/>
<br /> <br />
image
<input type="file" name="fileField" />
<br /> <br />
<input type="submit" name="submit" value="register product" />
</form>
my problem is, whenever i try to process the image using this code:
move_uploaded_file($_FILES['fileField']['tmp_name'], "../product_images/$newname");
I get the following error:
Notice: Undefined index: fileField
why is that?
Thanks in advance!
You have to add enctype='multipart/form-data' to the form
Quote from this topic about this
When you make a POST request, you have to encode the data that forms the body of the request in some way.
HTML forms provide two methods of encoding. The default is
application/x-www-form-urlencoded, which is more or less the same as a
query string on the end of the URL. The other, multipart/form-data, is
a more complicated encoding but one which allows entire files to be
included in the data.
enctype="multipart/form-data"
Add that attribute to your form tag
<form method="post" action="index.php" enctype="multipart/form-data">
multipart/form-data No characters are encoded. This value is required
when you are using forms that have a file upload control
http://www.w3schools.com/tags/att_form_enctype.asp
I have a basic username & password form which also allows you to upload an image with it. There's a create button, which takes the user to uploader.php which both uploads the image and inputs the username & password into the database.
Within the form tag:
< form enctype="multipart/form-data" method="POST" action="uploader.php?uploader=avatar&username=< ?php echo $_POST['username']; ?>" >
The problem:
The username won't post, nor any other posts for that matter. All fields are inside the form. I have checked PHP file upload form cannot submit a POST variable? and within php.ini post_max_size = 8M, and upload_max_filesize = 2M
Use <input type="hidden"/> to post username and other info.
<form enctype="multipart/form-data" method="POST" action="uploader.php">
<input type="hidden" name="uploader" value="avatar"/>
<input type="hidden" name="username" value="<?php echo $_POST['username']; ?>" />
...
</form>
Sample.php
<form enctype="multipart/form-data" method="POST" action="uploader.php">
<br/>Username : <input type="text" name="username"/>
<br/>Password : <input type="password" name="password"/>
<input type="hidden" name="uploader" value="avatar"/>
<br/>File : <input type="file" name="file"/>
<br/><input type="submit"/>
</form>
uploader.php
<?php
print_r($_POST) // debug $_POST
print_r($_FILES) // file
//OR
echo $_POST["username"];
$file=$_FILES["file"];
print_r(file);
?>
It sounds like you want to submit the username and password and upload a file all in the one submit.
If this is what you want, you need something like the following:
<form enctype="multipart/form-data" method="POST" action="uploader.php">
<input type="text" name="username" value="" />
<input type="password" name="password" value="" />
<input type="file" name="uploaded" />
...
</form>
The username and password will be available in $_POST[] and the file will be present in $_FILES[].
I had this problem when the files I was attempting to upload were larger than the max filesize PHP was accepting. Look at:
ini_get('post_max_size')
and
ini_get('upload_max_filesize')
to see if your file is too big. My solution was to use empty($_POST) to determine if the file was too big (or some other posting problem occurred) and throw an Exception.
Oddly I had the same issue, until I add the enctype="multipart/form-data" attribute.. After that, all worked
I have this problem when I'm using PHP5/HTML on Apache-Tomcat6.
Here's an example for one of the forms I use in my site:
<form enctype="multipart/form-data" method="post" action="hello.php" >
<label>Title* :</label>
<input type="text" name="title" />
<label>Image:</label>
<input type="file" name="image" /><br />
<input type="submit" value="Add"/>
</form>
Whenever I add the 'enctype' attribute to any form; neither the $_FILES['image'] is returned nor the $_POST variables. As long as the 'enctype' is not there, everything (except for the file input of course) works as expected. Can any one guide me please?
You won't be able to post data with a method of get on your form.
In test.html:
<form enctype="multipart/form-data" method="post" action="hello.php" >
<label>Title* :</label>
<input type="text" name="title" />
<label>Image:</label>
<input type="file" name="image" /><br />
<input type="submit" value="Add"/>
</form>
In hello.php:
<?php
print_r($_POST);
print_r($_FILES);
Depending upon your server configuration, this will combine $_GET, $_POST, and $_COOKIE, but you'll still want to post with file inputs.
print_r($_REQUEST);