I have a form and the action of the form is submit.php, what I am trying to do on submit.php to upload a file image, but when I click submit and print_r $_FILE an empty array appears. What am I doing wrong? I dont want to upload the file on the same page as the form. Here is the form:
<form action="in-the-press-submit.php" method="post">
<p>
<label for="title">Title:</label>
<input type="input" name="title" id="title">
</p>
<p>
<label for="posted-on">Posted On:</label>
<input type="date" name="posted-on" id="posted-on">
</p>
<p>
<label for="description" style="vertical-align:top;">Description:</label>
<textarea rows="20" cols="70" name="description" id="description"></textarea>
</p>
<p>
<label for="image">Image:</label>
<input type="file" name="image" id="image">
</p>
<p>
<input type="submit" name="submit" id="submit">
</p>
</form>
and here is my code in the submit page:
<?php
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"],
"/image-test/" . $_FILES["file"]["name"]);
echo "Stored in: " . "/image-test/" . $_FILES["file"]["name"];
}
?>
Try this
<form action="in-the-press-submit.php" method="post" enctype="multipart/form-data">
Documentation : http://www.w3schools.com/php/php_file_upload.asp
You need enctype="multipart/form-data" in your form tag:
<form action="in-the-press-submit.php" method="post" enctype="multipart/form-data">
See the php manual on POST method uploads.
the reason that you need to include enctype=multipart/form-data in the form tag is :
application/x-www-form-urlencoded is the standard and default way to POST a form without attached files.
multipart/form-data is the standard way to POST a form with attached file(s) because this encoding allows entire files to be included in the data
In order to upload a file add
enctype = "multipart/form-data"
in form tag.
Related
I am attempting to upload multiple images at once, and then on submit display those images on the page. This is going to be used with mPDF. I am using the examples in the manual at http://mpdf1.com/manual/index.php?tid=467
It has a text box and 1 image uploader, and displays what ever was in the text box and the image on the next page. How can I convert this to use multiple images?
Page 1:
<?php
$html = '
<html>
<body>
<form action="example_userinput2.php" method="post" enctype="multipart/form-data">
Enter text:
<br />
<textarea name="text" id="text"></textarea>
<br />
<label for="file">Choose Image to upload:</label> <input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
';
echo $html;
exit;
?>
Page 2: (also more specifically what I change the areas I marked ** **, after allowing multiple images.)
<?php
if (($_FILES["file"]["type"] == "image/gif" || $_FILES["file"]["type"] == "image/jpeg")
& $_FILES["file"]["size"] < 20000) {
// If the destination file already exists, it will be overwritten
move_uploaded_file($_FILES["file"]["tmp_name"], "../tmp/" . $_FILES["file"]["name"]);
}
else {
echo "Invalid file";
}
$html ='
<html>
<body>
<div>'.$_POST['text'].'</div>
**<img src="' ."../tmp/" . $_FILES["file"]["name"].'" />**
<form action="example_userinput3.php" method="post" enctype="multipart/form-data">
<textarea style="display:none" name="text" id="text">'.$_POST['text'].'</textarea>
**<input type="hidden" name="filename" id="filename" value="'. $_FILES["file"]**["name"].'" />
<input type="submit" name="submit" value="Create PDF file" />
</form>
</body>
</html>
';
echo $html;
exit;
?>
Page 3 goes to the mPDF generator so I can convert this to PDF for another project I have in mind.
Any help would be awesome.
From php manual, to find here: http://php.net/manual/en/features.file-upload.multiple.php
<form action="example_userinput2.php" method="post" enctype="multipart/form-data">
Send these files:<br />
<input name="userfile[]" type="file" /><br />
<input name="userfile[]" type="file" /><br />
<input type="submit" value="Send files" />
</form>
On page2, you can continue with a loop and handle those files at once:
foreach ($_FILES['array_of_files'] as $position => $file) {
// should output array with indices name, type, tmp_name, error, size
var_dump($file);
}
You can do the same as with one file in the loop
You can have multiple <input type="file"> html elements set up on your page set up this way:
<input type="file" name="file[0]" />
<input type="file" name="file[1]" />
etc.
And then in PHP loop through them:
foreach($_FILES['file'] as $file){
//refer to everything as $file instead of $_FILES['file']
}
That should be enough to get you started.
Have a look at the following code:
<?php
if (isset($_POST['email']))
{
$expertmail=trim($_POST['email']);
echo $expertmail;
$expertfile=$_FILES['upfile']['tmp_name'];
echo $expertfile;
}
?>
<form action="test.php" method="post" name="users" id="users" >
<input name="upfile" id="upfile" type="file" />
<input name="email" id="email" type="text" />
<input type="submit" name="submit_button" id="submit_button" value="ΑΠΟΣΤΟΛΗ" />
</form>
Why 'echo $expertfile' does not display anything?
Thank you
POST Method Uploads gives all the information you need to handle file uploads in PHP. For your case you need: enctype="multipart/form-data":
<form action="test.php" method="post" name="users" id="users" enctype="multipart/form-data">
As Salman A points out, you will also need to check to see if a file was uploaded.
I have the following PHP/ HTML code:
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$file_name = "../images/".$_FILES["file"]["name"];}
echo $file_name;
?>
<form name="updateArticle" method="post">
<div class="custom_file_upload">
<input type="text" class="file" name="file_info" id="file_info" disabled="disabled" value="<?php echo $image_name;?>">
<div class="file_upload">
<input type="file" id="file_upload" name="file" onchange="name_change()" id="file">
</div>
</div>
The problem here is that I can't get the value of $_FILES["file"]["name"]
Why and how can I fix it?
your form hasn't specified the multipart/form-data for file uploads
change your form and set the proper enctype
<form name="updateArticle" method="post" enctype='multipart/form-data'>
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);