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>
Related
Im trying to upload a file through a form to my php server and then display the name of the file. ATM I'm getting an error when I'm trying to submit the form:
Objekt was not found! Error 404
<html>
<body>
<form method="post" enctype="multipart/form-data" action="upload.php">
<input type="file" name="file" size="35">
<br>
<br>
<input type="submit" value="Upload" name="submit">
</body>
</html>
<?php
header('Content-type: text/plain');
if(isset($_FILES["file"])){
$file = $_FILES["file"];
echo("File: ".$file);
}
?>
if you want to upload/do any operation on same file then remove action from form. then change your code as below to echo file name
<html>
<body>
<form method="post" enctype="multipart/form-data" >
<input type="file" name="file" size="35">
<br>
<br>
<input type="submit" value="Upload" name="submit">
</body>
</html>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
if(isset($_FILES["file"])){
$file = $_FILES["file"]["name"];
echo "File: ".$file;
}
}
?>
I'm trying to upload files to my server but it doesn't work at all. Here is test code:
<?php
echo count($_FILES['upload']['name']);
?>
<!DOCTYPE html>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input name="upload[]" type="file" accept=".mp3" multiple="multiple" />
<br>
<input type="submit" value="Upload">
</form>
</body>
</html>
It always prints 0, file upload is enabled on my server.
The problem is that you're not counting the $_FILES['upload'].
Simple fix for your problem
Use:
echo count($_FILES['upload']);
Instead of:
echo count($_FILES['upload']['name']);
Edit:
Remove [] from the input's name.
<?php
echo count($_FILES['upload']);//only this modified//
?>
<!DOCTYPE html>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input name="upload[]" type="file" accept=".mp3" multiple="multiple" />
<br>
<input type="submit" value="Upload">
</form>
</body>
</html>
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
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>
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>