multiple file upload in php not working on my server - php

i want to multiple file upload in php.. but its not working
here is my code. and link
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="files[]" multiple/>
<input type="submit"/>
Link
<?php
if(isset($_FILES['files']))
{
$errors= array();
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name )
{
$file_name = $key.$_FILES['files']['name'][$key];
$file_size =$_FILES['files']['size'][$key];
$file_tmp =$_FILES['files']['tmp_name'][$key];
$file_type=$_FILES['files']['type'][$key];
if($file_size > 2097152){
$errors[]='File size must be less than 2 MB';
}
$desired_dir="uploads";
if(empty($errors)==true)
{
if(is_dir($desired_dir)==false)
{
mkdir("$desired_dir", 0777);
}
if(is_dir("$desired_dir/".$file_name)==false)
{
move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
}else
{
$new_dir="$desired_dir/".$file_name.time();
rename($file_tmp,$new_dir) ;
}
}else{
}
}
if(empty($error)){
echo "Success";
} }
?>
here is the upload code. when i am select multiple file to upload than no any response from server you can see live on my given link.

This works for me.
Make sure your uploads directory is writeable
You have to create a loop for each files
upload.php
<?php
// Count # of uploaded files in array
$total = count($_FILES['files']['name']);
// Loop through each file
for($i=0; $i<$total; $i++) {
$tmpFilePath = $_FILES['files']['tmp_name'][$i];
if ($tmpFilePath != ""){
$newFilePath = "uploads/" . $_FILES['files']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
echo 'Upload success!';
}
}
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="files[]" multiple/>
<input type="submit"/>
</form>

Use like
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="files[]" multiple/>
<input type="submit"/>
</form>
<?php
$target_dir = "uploads/";
if(isset($_POST))
{
if(isset($_FILES["files"]["name"]) && is_array($_FILES["files"]["name"]) && $_FILES["files"]["name"]!= false)
{
foreach($_FILES["files"]["name"] as $key=>$name)
{
$target_file = $target_dir . basename($name);
$uploadOk = 1;
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
if (move_uploaded_file($_FILES["files"]["tmp_name"][$key], $target_file)) {
echo $_FILES["files"]["name"][$key] . " uploaded <br/>";
}
}
}
} ?>
If you are uploading big files then please check your php.ini settings , check https://doc.owncloud.org/server/8.0/admin_manual/configuration_files/big_file_upload_configuration.html

Related

PHP Image Upload Is Not Uploading Image to target directory

Problem:
I have created a upload.php file, which contains a HTML form and the PHP code needed to upload images and I use XAMPP localhost:
htdocs/web2/assets/upload.php
I want to upload images to the folder upload in the same directory:
htdocs/web2/assets/uploads/
Script:
<?php
<form action="?" method="post" enctype="multipart/form-data">
<!--wrap input button as around pre-existing image -->
<?php
echo '<label class="profile_gallery_image_in"><input type="file" name="fileToUpload" id="fileToUpload" onchange="form.submit()"/><p class="label"></p><img class="myImg" src='.$image.' height="100%" width="100%" /></label>';
?>
</form>
<!-- Commence Photo Upload -->
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(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;
}
}
?>
The form is set to auto-submit upon input type change. This appears to be working fine and the form is submitting.
However, I get no actual image uploaded and no error at all.
How do I solve this problem?
There seems to be an error in the code. You can use the below code for image uploading.
<?php
if(isset($_FILES['image'])){
$errors= array();
$dir = "images/";
$file_name = $_FILES['image']['name'];
$file_name = $dir. $file_name;
$file_size = $_FILES['image']['size'];
$file_tmp = $_FILES['image']['tmp_name'];
$file_type = $_FILES['image']['type'];
$tmp = explode('.',$_FILES['image']['name']);
$file_ext=strtolower(end($tmp));
$extensions= array("jpeg","jpg","png","gif");
if(in_array($file_ext,$extensions)=== false){
$errors[]="extension not allowed, please choose a GIF, JPEG or PNG file.";
}
if($file_size > 2097152) {
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true) {
move_uploaded_file($file_tmp, $file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
<html>
<body>
<form action = "" method = "POST" enctype = "multipart/form-data">
<input type = "file" name = "image" />
<input type = "submit"/>
</form>
</body>
</html>

How to upload two different image with two browse button and single submit button

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

Upload image and display it to user

I'm trying to upload a file to a server then display it to the user. I'm having difficulties to display the image to the user.
If you could provide code that helps me out to display the image to the user. The code should fit in the php file right under //Display image here <---------
html file
<html>
<body>
<form method="post" enctype="multipart/form-data" action="server.php">
<input type="file" name="fileToUpload" id="fileToUpload" size="35">
<br>
<br>
<input type="submit" value="Upload" name="upload">
</body>
</html>
php file
<?php
if(isset($_FILES["fileToUpload"])){
$file = $_FILES['fileToUpload'];
$fileName = $_FILES["fileToUpload"]["name"];
$fileTmpName = $_FILES["fileToUpload"]["tmp_name"];
$fileSize = $_FILES["fileToUpload"]["size"];
$fileError = $_FILES["fileToUpload"]["error"];
$fileType = $_FILES["fileToUpload"]["type"];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png');
if(in_array($fileActualExt, $allowed)){
//Image code
if($fileError === 0){
if($fileSize < 500000){
$fileDestination = 'uploads/'.$fileName;
move_uploaded_file($fileTmpName, $fileDestination);
header("Location: server.php?uploadsuccess");
//Display image here <----------
}else{
echo "Your file is too big!";
}
}else{
echo "There was an error while uploading your file!";
}
}else{
if(isset($_FILES["fileToUpload"])){
$file = $_FILES["fileToUpload"]["name"];
echo "File: ".$file;
}
}
}
?>
first you have to change .html file to .php and note that i have renamed file as index.php
<html>
<body>
<?php if(isset($_GET['filename'])){ ?>
<img src="<?php echo $_GET['filename']; ?>" />
<?php } ?>
<form method="post" enctype="multipart/form-data" action="server.php">
<input type="file" name="fileToUpload" id="fileToUpload" size="35">
<br>
<br>
<input type="submit" value="Upload" name="upload">
</body>
</html>
server.php
<?php
if(isset($_FILES["fileToUpload"])){
$file = $_FILES['fileToUpload'];
$fileName = $_FILES["fileToUpload"]["name"];
$fileTmpName = $_FILES["fileToUpload"]["tmp_name"];
$fileSize = $_FILES["fileToUpload"]["size"];
$fileError = $_FILES["fileToUpload"]["error"];
$fileType = $_FILES["fileToUpload"]["type"];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png');
if(in_array($fileActualExt, $allowed)){
//Image code
if($fileError === 0){
if($fileSize < 500000){
$fileDestination = 'uploads/'.$fileName;
move_uploaded_file($fileTmpName, $fileDestination);
// header("Location: server.php?uploadsuccess");
//Display image here <----------
header("Location:index.php?filename=$fileDestination");
}else{
echo "Your file is too big!";
}
}else{
echo "There was an error while uploading your file!";
}
}else{
if(isset($_FILES["fileToUpload"])){
$file = $_FILES["fileToUpload"]["name"];
echo "File: ".$file;
}
}
}
?>
I have done using php but better option is using ajax call .just google it you will get man examples
To be able to display the image, do this :
//Display image here <----------
echo "<img src='" . $fileLocation . "'>";
Replace $file_location with the variable containing the file location
<!-- index.php -->
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file2upload">
<input type="submit" value="upload">
</form>
<!-- sorry i didn't got php code section here , so i m posting php file code here -->
<!-- upload.php file -->
<?php
// target directory where files goes after uploading
$target_dir = "uploads/pictures/";
// target files name and extension save to this target_file variable
// $target_file specifies the path of the file to be uploaded
$target_file = $target_dir . basename($_FILES["file2upload"]["name"]);
$uploadOk = 1;
// checking that target_file is been uploading is a true image file extenion or not
// $audioFileType holds the file extension of the file (in lower case)
$picFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
echo $picFileType."<br />";
if(isset($_POST["submit"])) {
// The filesize() function in PHP is an inbuilt function which is used to return the size of a specified file.
// The filesize() function accepts the filename as a parameter and returns the size of a file in bytes on success and False on failure.
$check = getimagesize($_FILES["file2upload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// step:2
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// step: 3
// Check file size
if ($_FILES["file2upload"]["size"] > 5000000) { // 5MB manual set
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// step: 4
// Allow certain file formats
if($picFileType != "jpeg" && $picFileType != "jpg" && $picFileType != "bmp" && $picFileType != "gif" && $picFileType != "png" ) {
$uploadOk = 0;
echo "<b style='color:red;'> File to be uploading is not have image formates like : .jpeg,.jpg,.bmp, .gif, .png etc </b><br />";
}
// step: 5
// 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["file2upload"]["tmp_name"], $target_file))
{
echo "The file ". basename( $_FILES["file2upload"]["name"]). " has been uploaded. <br />";
<!-- this is the ANSWER -->
<!-- ----------------------------- -->
echo $file_name = basename( $_FILES["file2upload"]["name"]);
echo $file_size = $_FILES["file2upload"]["size"];
echo $fileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
<!-- ----------------------------- -->
}
else {
echo "Sorry, there was an error uploading your file.";
}
}
// }else{ echo "<b style='color:red;'> form have diffrent method ( post/get ) </b><br />"; $uploadOk = 0; }
?>

Add more Images by php form

I want to prepare 5 photo upload form and user need to select all 5 photo at once and the submit the form
then i need to store all 5 photos in the Target Directory by renaming image as "1.jpg, 2.jpg, 3.jpg, 4.jpg, 5.jpg". I stored only one image but after add For loop for save more than one file its not working ....please support.
// Image Upload FORM
<form action="saveinntion.php" method="post" enctype="multipart/form-data">
<h1>Upload Your Innovation</h1
<fieldset>
<legend><span class="number">4</span>Upload Images</legend>
<input type="file" name="img1" id="img1" >
</br>
<input type="file" name="img2" id="img2">
</br>
<input type="file" name="img3" id="img3">
</br>
<input type="file" name="img4" id="img4">
</br>
<input type="file" name="img5" id="img5">
</br>
</fieldset>
<button type="submit">Submit</button>
</form>
saveinntion.php file
<?php
include("dbconnection.php");
$target_dir = "Upload/";
$img=$_POST['img'];
for ($i = 0; $i < 5; $i++) {
$target_file = $target_dir . basename($_FILES['$img[]']["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if ($_FILES['$img[]']["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "jpeg" ) {
echo "Sorry, only JPG & JPEG files are allowed.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES['$img[]']["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES['$img[]']["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
header("Location: upload1.php?id=$msg");
?>
Your problem is with your naming. as #Twinfriends said in the command it is highly unlikely to upload any file
replace $_FILES['$img[]'] with $_FILES['img'.($i+1)] in your code.
line # 5
$target_file = $target_dir . basename($_FILES['img'.($i+1)]["name"]);
line #13
if ($_FILES['img'.($i+1)]["size"] > 500000) {
line #25
if (move_uploaded_file($_FILES['img'.($i+1)]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES['img'.($i+1)]["name"]). " has been uploaded.";
To renaming file.
$imageFileType = pathinfo($_FILES['img'.($i+1)]["name"],PATHINFO_EXTENSION);
$target_file = $target_dir . ($i+1).".".$imageFileType;

How to upload multiple files , create folder and place the files there PHP

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.

Categories