PHP file upload can't send POST data on same form - php

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

Related

jquery mobile my action php page is not getting input file

hellow my this is my form in jquery mobile i also turbed off the data-ajax and define the enctype but still i am not getting my file in my action page
<form id="form_id" action="action/cat-manag-add.php" enctype="multipart/form-data" data-ajax="false" method="post">
<label class="TSG-font" for="category_image">Category image:</label>
<input class="required" type="file" name="file" id="file" value="" >
<form>
this my action page getting all $_POST values but not getting the $_FILES
$title=$_POST['title'];
$description=$_POST['description'];
$file=$_FILES['file'];
kindly help me
Remove value="" in input type file.
And try var_dump($_FILES); check what is print in you action page

input type file Isset returning false even when set

I am uploading a picture, but isset() returns false even when I clearly picked a file. Here is my code.
<form id="form1" name="form1" method="POST" enctype="multipart/form-data" action="addroom.php">
<p>Upload pictures:</p>
<p>Bed:</p>
<input type="file" name="image1"/>
<p>Comfort room</p>
<input type="file" name="image2"/>
<p>Living room:</p>
<input type="file" name="image3"/>
<input type="submit" id="add" name="add" value="Add Room Category"/>
</form>
<?php
if(isset($_POST['image1']))
$file1=$_FILES['image1']['name'];
else
echo "image not selected";
Uploaded files are stored in $_FILES. $_POST['image1'] does not exist because that is a file, not a text (or other) field.
You want to use either:
if(isset($_POST['add']))
or
if(isset($_FILES['image1']))
Try this
if(isset($_FILES["image1"]["name"])){...}

POST array is empty when containing a file

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

How to send iframe id/name to a php file?

I have a iframe with a name, in this case an email address that changes from users.
I need to deal with this var:
<iframe name="asd#asd.lol" src="index.html">
index.html is a simple data form like this:
<form ENCTYPE="multipart/form-data" action="upload.php" method="POST">
<p>Upload: <input type="file" name="file1"> <br /></p>
<input type="submit" value="Upload">
</form>
I need to pass the value "asd#asd.lol" to the "upload.php" file.
Is this possible?
Is it possible for you to add a querystring parameter into the index.html file?
So:
<iframe name="asd#asd.lol" src="index.html?email=asd#asd.lol">
Then in the index.html file set a hidden variable:
<form ENCTYPE="multipart/form-data" action="upload.php" method="POST">
<p>Upload: <input type="file" name="file1"> <br /></p>
<input type="hidden" name="email" id="email" value="">
<input type="submit" value="Upload">
</form>
If this is a plain HTML file you could set the value of the hidden field with some javascript.
Then use $_POST["email"] to get the email value out of the hidden field.
<form ENCTYPE="multipart/form-data" action="upload.php" method="POST" onsubmit='$(this).append('<input type="hidden" name="iframeEmail" value="'+$("iframe[src='index.html']").attr('name')+'">')'>
<p>Upload: <input type="file" name="file1"> <br /></p>
<input type="submit" value="Upload">
</form>
Try this out ,if it wont work ,than place the iFrame and ID and find it with the ID on jQuery Selector

Tomcat 6 & PHP with enctype - No input data returned

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

Categories