Call the upload file - php

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

Related

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

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

html/php not uploading files

I have looked all over stackoverflow and have not yet found a working answer.
The HTML form allows the user to upload multiple files using one single input, the values then travel to a php file (named upload.php) to be uploaded to their final resting place.... this doesn't happen.
My HTML form:
<form method="post" action="upload.php" enctype="multipart/form-data">
<input name="upload[]" type="file" multiple="multiple" />
<input type="submit" value="Upload Files" class="btn btn-default btn-sm"/>
</form>
My PHP file:
<?php
if(count($_FILES['upload']['name'])) {
foreach ($_FILES['upload']['name'] as $file) {
move_uploaded_file($_FILES["upload"]["tmp_name"], './uploads/'.$_FILES["upload"]["name"]);
}
}
?>
try this code
<form method="post" action="upload.php" enctype="multipart/form-data">
<input name="upload[]" type="file" multiple="multiple" />
<input type="submit" name="submit" value="Upload Files" class="btn btn-default btn-sm"/>
</form>
Your PHP file
<?php
if(isset($_POST["submit"]))
{
if(count($_FILES['upload']['name'])) {
foreach ($_FILES['upload']['name'] as $key=>$file) {
move_uploaded_file($_FILES['upload']['tmp_name'][$key], './uploads/'.$file);
}
}
}
?>
I have edited this answer and below code is working for me.... hope this will help.
<!DOCTYPE HTML>
<html>
<body>
<form method="post" action="" enctype="multipart/form-data">
<input name="upload[]" type="file" multiple="multiple" />
<input type="submit" name="submit" value="Upload Files" class="btn btn-default btn-sm"/>
</form>
<div>
<?php if(isset($_POST['submit'])){
foreach($_FILES['upload']['name'] as $key=>$filename){
move_uploaded_file($_FILES["upload"]["tmp_name"][$key], '.pathtoupload/'.$filename);
}
}?>
</div>
</body>
</html>

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.

How do I echo submitted values from settings.php to mainpage.php?

I'm basically trying to get the values submitted from settings so that when the user submits it, it is displayed onto mainpage.php. Here's the code from what I have in settings.php...
<html>
<head><title></title></head>
<body>
Logout
Main Page
<form action="" method="get">
<b>Bio</b>
<br>
<textarea rows="5" cols="20" name="result" value="result"></textarea>
<br>
<input type="submit" value="Submit" name="bio">
</form>
<form action="" method="get">
<b>Hobbies</b>
<br>
<textarea rows="5" cols="20" name="result" value="result"></textarea>
<br>
<input type="submit" value="Submit" name="hobbies">
</form>
<form action="" method="get">
<b>Past School</b>
<br>
<textarea rows="5" cols="20" name="result" value="result"></textarea>
<br>
<input type="submit" value="Submit" name="school">
</form>
<form action="" method="get">
<b>Work History</b>
<br>
<textarea rows="5" cols="20" name="result" value="result"></textarea>
<br>
<input type="submit" value="Submit" name="work">
</form>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Choose Profile Picture:</label>
<input type="file" name="file" id="file">
<input type="submit" name="pic" value="Submit">
</form>
</body>
</html>
And this is what I have for mainpage.php
<?php
?>
<html>
<head><title></title></head>
<body>
Logout
Settings
</body>
</html>
Any suggestions?
As Jenna R said, it is better to use POST, not GET.
Then each variable is referenced with $_POST["name"], so $_POST["result"] and so on.
I suggest you start reading up on PHP and submitting forms securely if you are doing this for anything other than learning. This is pretty basic, so it looks like you haven't opened up the manual or a book just yet.

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