I have a HTML form for a file upload, which sends the uploaded file to the backend directory of my wordpress site. How can I enable multiple file uploads? I tried simply adding multiple="multiple" to my file upload form (which allowed me to select multiple files) but in the directory only one file is uploaded. Here is the form:
<form action="http://www.aerex.co.uk/php-upload/" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload" multiple="multiple">
<input type="submit" value="Upload Image" name="submit">
</form>
The PHP for the action page:
<?php
$target_dir = $_SERVER['DOCUMENT_ROOT'] . '/wp-content/uploads/';
$target_file = $target_dir . '/' . basename($_FILES["fileToUpload"]["name"]);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>
You change this
<form action="http://www.aerex.co.uk/php-upload/" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload[]" id="fileToUpload" multiple="multiple">
<input type="submit" value="Upload Image" name="submit">
</form>
and your php
$target_dir = $_SERVER['DOCUMENT_ROOT'] . '/wp-content/uploads/';
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
foreach ($_FILES['fileToUpload']['name'] as $f => $name) {
if(isset($_POST["submit"])) {
$target_file = $target_dir . '/' . basename($_FILES["fileToUpload"]["name"][$f]);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"][$f], $target_file);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$check = getimagesize($target_file);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
}
}
Related
I made an upload form called upload.php with action up.php so I upload images in the folder C:/xampp/htdocs/uploadimages and in the page upload.html I would like to make a dynamic slideshow containing all new and old pictures.
upload.php
<body>
<form action="up.php" method="post" enctype="multipart/form-data">
<h2>Upload File</h2>
<label for="fileSelect">Filename:</label>
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
<p><strong>Note:</strong> Only <b> .jpg </b> and <b> .png </b> formats allowed to a max size of 3 MB.</p>
</form>
<div class="gallery">
<div class="gallery-image">
<?php
$directory = "C:/xampp/htdocs/uploadimages";
$images = glob($directory . "/*.jpg");
foreach($images as $image)
{
echo " <img src=\"basename($image)\"> ";
echo "<br>" ;
}
?>
</div>
</body>
up.php
<?php
$target_dir = "C:/xampp/htdocs/uploadimages/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if (file_exists($target_file)) {
echo "File already exists. <br>";
$uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 3 * 1024 * 1024) {
echo "Your file is too large. <br>";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png") {
echo "Only JPG and PNG files are allowed. <br>";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "";
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded. <br>";
} else {
echo "There was an error uploading your file. <br>";
}
}
?>
I tried the basename($image) inside a for-each loop but nothing worked
I want to upload two images, one of the user and second of his ID, using one submit button using mysqli. Here is my HTML.
<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php">
your image: <input type="file" name="img"><br/>
your Id card: <input type="file" name="img2">
<input type="submit" name="publish" value="upload">
</form>
</body>
</html>
All I know is to upload the single image at a time but what if want to upload these image into the database with single submit. I am not writing PHP because I don't know how to do this. I can upload multiple images at a time using an array but I want to use this method. Is it possible to do with PHP??
PHP for single upload:
<?php
$dir = "uploads/";
$t_file = $dir . basename($_FILES["img"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($t_file,PATHINFO_EXTENSION));
if(isset($_POST["upload"])) {
$check = getimagesize($_FILES["img"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>
I can do it by using javascript. All respected users, please be more helpful for those who are new to any language. Or make this website only for experts, not for beginners.
So I use PHP for my first upload and JS for my second image upload.
here is js:
<script>
function startUpload(){
document.getElementById('uploadProcess').style.visibility = 'visible';
document.getElementById('uploadForm').style.visibility = 'hidden';
return true;
}
function stopUpload(success,uploadedFile){
var result = '';
if (success == 1){
result = '<span class="sucess-msg">The file was uploaded successfully!<\/span><br/><br/>';
//Uploaded file preview
var embed = document.getElementById("UploadedFile");
var clone = embed.cloneNode(true);
clone.setAttribute('src',uploadedFile);
embed.parentNode.replaceChild(clone,embed);
}else {
result = '<span class="error-msg">There was an error during file upload!<\/span><br/><br/>';
}
document.getElementById('uploadProcess').style.visibility = 'hidden';
document.getElementById('uploadForm').innerHTML = result + '<label>File:<input name="myfile" type="file" size="30" /><\/label><label><input type="submit" name="submitBtn" class="sbtn" value="Upload" /><\/label>';
document.getElementById('uploadForm').style.visibility = 'visible';
return true;
}
</script>
The html:
<form action="upload.php" method="post" enctype="multipart/form-data" onsubmit="startUpload();">
<p id="uploadForm">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="file" name="myfile" id="fileToUpload1">
<input type="submit" value="submitBtn" name="submit">
</p>
</form>
and the upload.php:
<?php
$success = 0;
$uploadedFile = '';
//File upload path
$uploadPath = 'uploads/';
$targetPath = $uploadPath . basename( $_FILES['myfile']['name']);
if(#move_uploaded_file($_FILES['myfile']['tmp_name'], $targetPath)){
$success = 1;
$uploadedFile = $targetPath;
}
sleep(1);
?>
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
The following script was copied over form W3Schools http://www.w3schools.com/php/php_file_upload.asp.
The script is not uploading images into the uploads/ directory - is there something wrong with my script? Or is there something additional that needs to be implemented in order for the script to work?
Directory name: "uploads/"
file name: "upload.php"
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
EDITS
The above errors now occur from the following script
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
// Copy the file to target folder
if ($uploadOk) {
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], target_dir . $target_file );
}
}
?>
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
There isn't any function in your code to copy the uploaded file into the target directory.
You have to add this:
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_dir . $target_file )
Attending the question author comment in this post, I updated the code to create the folder if it does not exist.
So your code should result like:
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
// Copy the file to target folder
if ($uploadOk) {
// Check if the upload directory exists and create if necessary
if (!is_dir($target_dir)) {
mkdir($target_dir);
}
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_dir . $target_file );
}
}
I am trying to upload an image via a PHP script.
In the script all conditions are true while I upload the image but move_uploaded_file function doesn't seem to work.
What may be the problem?
My HTML code is
<form id="register_form" action="" method="post" enctype="multipart/form-
data">
Select image to Upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
PHP Script:
<?php
$target_dir = "/home/infibusiness1/Desktop/uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'],
$target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has
been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
echo "<br>File Name : ".$_FILES['fileToUpload']['tmp_name'];
}
}
?>
Ensure that the web server has write permission on the target directory?
You will need to know what user the web server is running as or if security isn't a concern try
chmod 777 /home/infibusiness1/Desktop/uploads/
Ok, i can successfully upload 1 document , make it create a new folder within the main folder. Then place the file there.
However im having trouble doing multiple files to that same folder.
I want to upload 2 documents and look like this:
docs/1/ and then the files here.
what it looks like:
docs/1
HTML:
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:<br/>
<input type="file" name="fileToUpload" id="fileToUpload"><br/>
<input type="file" name="fileToUpload" id="fileToUpload"><br/>
<input type="submit" value="Upload Image" name="submit">
</form>
PHP:
<?php
$number = 1;
$target_dir = "docs/";
$new = mkdir($target_dir . $number . "/");
$target_file = $target_dir . $number . "/";
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
foreach($_FILES['fileToUpload']['name'] as $file => $uploaded_file){
move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_file . $uploaded_file);
}
}
?>
Any Ideas?
EDIT WORKING:
<input type="file" name="pictures[]" /><br/>
<input type="file" name="pictures[]" /><br/>
<input type="file" name="pictures[]" /><br/>
<input type="submit" value="Send" />
</form>
$target_dir = "docs/";
$dir=glob($target_dir."/*",GLOB_ONLYDIR);
$number = count($dir) + 1;
$new = mkdir($target_dir . $number . "/");
$target_file = $target_dir . $number . "/";
foreach ($_FILES["pictures"]["name"] as $key => $Name)
{
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
move_uploaded_file($tmp_name, $target_file . "$name");
}
First change your file input to (note the id should be unique for all elements to avoid javascript problems):
<input type="file" name="fileToUpload[]" id="fileToUpload_0">
Since you want to upload an array of files.
foreach($_FILES['fileToUpload'][$name] as $index=>$uploaded_file) {
move_uploaded_file(
$_FILES['fileToUpload']['tmp_name'][$index],
$target_file.$uploaded_file);
}
Update: Had a momentary mix in languages and file handling logic.