php undefined index coming on uploading file with ['name'] - php

<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"];
}
?>

Related

Upload files with multiple inputs

I have a form where i want to upload files with multiple inputs.My form looks like:
<form action="" method="post">
<input type="file" name="tax" />
<input type="file" name="ta" />
<input type="submit" name="submit" />
</form>
I do not know how to process this form..
You form doesn't work until you don't include 'enctype="multipart/form-data"', because it is necessary to use input type file.
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="tax" />
<input type="file" name="ta" />
<input type="submit" name="submit" />
</form>
Now browse the file and submit the form. You will get all file data inside $_FILES. so to check what you get inside the file data, you can use :
echo '<pre>';
print_r($_FILES)
I'm not sure whether you have gone through the tutorials before, however below is the code which will help you to process it.
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="tax" />
<input type="file" name="ta" />
<input type="submit" name="submit" />
</form>
If you upload the file, you can get the files from,
$_FILES global array, i.e. $_FILES['tax'] and $_FILES['ta'].
More info can be found on php.net
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="tax" />
<input type="file" name="ta" />
<input type="submit" name="submit" />
</form>
<?php
//print_r($_POST);
if(isset($_POST['submit'])){
$name = $_FILES['tax']['name'];
$name1 = $_FILES['ta']['name'];
$temp_name = $_FILES['tax']['tmp_name'];
$temp_name1 = $_FILES['ta']['tmp_name'];
var_dump($_FILES);
if(isset($name)){
if(!empty($name)){
var_dump($_FILES);
$location = 'images/'.$name;
if(move_uploaded_file($temp_name, $location)){
echo 'File uploaded successfully';
}
}
} else {
echo 'You should select a file to upload !!';
}
if(isset($name1)){
if(!empty($name1)){
var_dump($_FILES);
$location = 'images/'.$name1;
if(move_uploaded_file($temp_name1, $location)){
echo 'File uploaded successfully';
}
}
} else {
echo 'You should select a file to upload !!';
}
}
?>

Call the upload file

In html I have an upload button for a photo:
<p>
<input type="file" name="datafile" size="40">
</p>
<br>
<p>
<input type="submit" name="submit" id="submit" value="submit" />
</p>
After that I want to take the results using the echo and insert the result in a table in the center of page.
I made this but it is not working properly. Any suggestion?
<table align="center">
<tr>
<?php echo $_POST['datafile']; ?>
</tr>
</table>
Your code should be
<?php
if(isset($_POST['submit'])){
print_r($_FILES); // you will get your data in $_FILES variable.
}
?>
<form action="file_upload.php" method="post" enctype="multipart/form-data">
<p>
<input type="file" name="datafile" size="40">
</p>
<br>
<p>
<input type="submit" name="submit" id="submit" value="submit" />
</p>
</form>
You should have enctype="multipart/form-data in your form tag
<form action="upload.php" method="post" enctype="multipart/form-data">
And get the uploaded file via $_FILES: http://www.w3schools.com/php/php_file_upload.asp
$_FILES['datafile']
Read more here http://www.w3schools.com/php/php_file_upload.asp

Cannot display content of $_FILES

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.

Wordpress - file upload form in page - empty $_FILES array

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>

How can I make photo change once uploaded with this form

I am trying to work around this, I know it might need mysql, php or something but i will like to know how i can make the imageuploaded.jpg in this html change anytime a new one is uploaded with the html form below? before voting the question down please give a suggestion at least. I am new to html
<fieldset>
<legend>User Photo </legend>
<p align="center"><img src="imageuploaded.jpg" /></p>
</fieldset>
</td>
<form name="" method="post" enctype='multipart/form-data'>
<input id="browse" type="file" name="image">
<input id="upload" type="submit" name="Submit"value="upload" />
</form>
<form name="insert" method="post">
<p>
You can use something like this:
<?php
if ($_POST && $_FILES['image']['name']) {
$avatar = $_FILES['image']['name'];
} else {
$avatar = "noimage.jpg";
}
?>
<fieldset>
<legend>User Photo </legend>
<p align="center"><img src="<?php echo $avatar;?>" /></p>
</fieldset>
</td>
<form name="" method="post" enctype='multipart/form-data'>
<input id="browse" type="file" name="image">
<input id="upload" type="submit" name="Submit"value="upload" />
</form>
<p>

Categories