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'>
Related
<form action='' method='post'>
<input type="file" class="form-control" name="kk">
<input type="submit" class="form-control" name="submit">
</form>
<?php
if(isset($_POST['submit'])){
echo $_FILES["kk"]["name"];
}
?>
hi,i'm new to php,In this code undefined index kk coming on uploading file,i just want the file name...please help me out
add enctype="multipart/form-data" to form tag
You need to pass enctype="multipart/form-data" when you have to upload file
<form action='' method='post' enctype="multipart/form-data">
<input type="file" class="form-control" name="kk">
<input type="submit" class="form-control" name="submit">
</form>
<?php
if(isset($_POST['submit'])){
echo $_FILES["kk"]["name"];
}
?>
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 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.
I'm trying to create a simple fileupload form in my custom Wordpress template:
<form action="<?php echo $current_url = home_url(add_query_arg(array(),$wp->request)); ?>" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
On submit, the $_FILES array is empty.
I think the problem is in the action, as the url that is generated is the nice URL, instead of http://domain.com/somepost.php
What should I put into the action, so I get the $_FILES?
Try this
<?php
if(isset($_POST['submit'])){
if($_FILES["file"]["error"] > 0){
echo $_FILES["file"]["error"];
}
else{
echo $_FILES['file']["name"]; // your file name
}
}
?>
<form method="post" action="#" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" name="submit"/>
</form>
Why won't this print "success" when I submit the form? I'm pretty sure it should.
<?php
if (count($_POST) > 0) {
echo "success!!";
}
?>
<form method="post" enctype="multipart/form-data">
<input type="file" name="userfile" />
<input type="submit" value="upload" />
</form>
At a guess, the submit field has no name, so it won't be included in $_POST. Your file upload will be placed in $_FILES, see Handling file uploads.
It is also good practice to NOT ommit the action attribute.
If you want the form to submit to itself, try
<form method="post" action="?" enctype="multipart/form-data">
or
<form method="post" action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']); ?>" enctype="multipart/form-data">
Further reading on second method Disclaimer: Link to my own blog
<?php
if (count($_POST['submit']) > 0) {
echo "success!!";
}
?>
<form method="post" enctype="multipart/form-data" action="">
<input type="file" name="userfile" />
<input type="submit" value="upload" name="submit"/>
</form>