Jump to content

Multiple file upload - script try to upload file multiple times.


Voor

Recommended Posts

Hello! :D

 

Again I have to ask the mighty power of internet for help with a bloody PHP code ^^ So what I need is a file upload with 5 input fields (I can not be bothered to do it different way ^^") and hey it work! Problem is it show crapload of errors as well ^^" I used script from this website and modified it to my needs here it is:

 

HTML:

<div id="selectorWrapper">		<div id="selector">		<input type="file" name="img[]" id="files" onchange="PreviewImage();"/>		</div>		<div id="delwrap"><button type=button id="del" onclick="empty();" >Delete</button></div>		</div>		<div id="selectorWrapper">		<div id="selector2">		<input type="file" name="img[]" id="files2" onchange="PreviewImage2();"/>		</div>		<div id="delwrap"><button type=button id="del2" onclick="empty2();" >Delete</button></div>		</div>		<div id="selectorWrapper">		<div id="selector3">		<input type="file" name="img[]" id="files3" onchange="PreviewImage3();"/>		</div>		<div id="delwrap"><button type=button id="del3" onclick="empty3();" >Delete</button></div>		</div>		<div id="selectorWrapper">		<div id="selector4">		<input type="file" name="img[]" id="files4" onchange="PreviewImage4();"/>		</div>		<div id="delwrap"><button type=button id="del4" onclick="empty4();" >Delete</button></div>		</div>		<div id="selectorWrapper">		<div id="selector5">		<input type="file" name="img[]" id="files5" onchange="PreviewImage5();"/>		</div>		<div id="delwrap"><button type=button id="del5" onclick="empty5();" >Delete</button></div> 				</div>

PHP:

$target_dir = "adimg/";$allfiles = $_FILES['img']['name'];$allfilestmp =$_FILES['img']['tmp_name'];foreach($allfiles as $singlefile){foreach($allfilestmp as $singlefiletmp){$target_file = $target_dir . uniqid() . basename($singlefile);$uploadOk = 1;$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);// Check if image file is a actual image or fake imageif($singlefiletmp != ""){if(isset($_POST["ad1post"])) {    $check = getimagesize($singlefiletmp);    if($check !== false) {        echo "File is an image - " . $check["mime"] . ".";        $uploadOk = 1;    } else {        echo "File is not an image.";        $uploadOk = 0;    }}// Check if file already existsif (file_exists($target_file)) {    echo "Sorry, file already exists.";    $uploadOk = 0;}// Check file sizeif ($singlefile > 500000) {    echo "Sorry, your file is too large.";    $uploadOk = 0;}// Allow certain file formatsif($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "JPG" && $imageFileType != "PNG" && $imageFileType != "JPEG" ) {    echo "Sorry, only JPG, JPEG, PNG files are allowed.";    $uploadOk = 0;}// Check if $uploadOk is set to 0 by an errorif ($uploadOk == 0) {    echo "Sorry, your file was not uploaded.";// if everything is ok, try to upload file} else {    if (move_uploaded_file($singlefiletmp, $target_file)) {        echo "The file ". basename($singlefile). " has been uploaded.";    } else {        echo "Sorry, there was an error uploading your file.";    }}}}}}

 

And here are errors:

File is an image - image/jpeg.The file cubic2black.jpg has been uploaded.File is an image - image/jpeg.The file cubic2black.jpg has been uploaded.File is an image - image/jpeg.The file cubic2black.jpg has been uploaded.File is an image - image/jpeg.The file cubic2black.jpg has been uploaded.File is an image - image/jpeg.The file cubic2black.jpg has been uploaded. <<< This is an success image upload msg.

 

Warning: getimagesize(D:xampptmpphp624D.tmp): failed to open stream: No such file or directory in D:xampphtdocsstronapostad.php on line 53File is not an image.Sorry, your file was not uploaded.Warning: getimagesize(D:xampptmpphp624E.tmp): failed to open stream: No such file or directory in D:xampphtdocsstronapostad.php on line 53File is not an image.Sorry, your file was not uploaded.Warning: getimagesize(D:xampptmpphp624F.tmp): failed to open stream: No such file or directory in D:xampphtdocsstronapostad.php on line 53File is not an image.Sorry, your file was not uploaded.Warning: getimagesize(D:xampptmpphp6250.tmp): failed to open stream: No such file or directory in D:xampphtdocsstronapostad.php on line 53File is not an image.Sorry, your file was not uploaded.Warning: getimagesize(D:xampptmpphp6260.tmp): failed to open stream: No such file or directory in D:xampphtdocsstronapostad.php on line 53File is not an image.Sorry, your file was not uploaded.Warning: getimagesize(D:xampptmpphp624D.tmp): failed to open stream: No such file or directory in D:xampphtdocsstronapostad.php on line 53File is not an image.Sorry, your file was not uploaded.Warning: getimagesize(D:xampptmpphp624E.tmp): failed to open stream: No such file or directory in D:xampphtdocsstronapostad.php on line 53File is not an image.Sorry, your file was not uploaded.Warning: getimagesize(D:xampptmpphp624F.tmp): failed to open stream: No such file or directory in D:xampphtdocsstronapostad.php on line 53File is not an image.Sorry, your file was not uploaded.Warning: getimagesize(D:xampptmpphp6250.tmp): failed to open stream: No such file or directory in D:xampphtdocsstronapostad.php on line 53File is not an image.Sorry, your file was not uploaded.Warning: getimagesize(D:xampptmpphp6260.tmp): failed to open stream: No such file or directory in D:xampphtdocsstronapostad.php on line 53File is not an image.Sorry, your file was not uploaded.Warning: getimagesize(D:xampptmpphp624D.tmp): failed to open stream: No such file or directory in D:xampphtdocsstronapostad.php on line 53File is not an image.Sorry, your file was not uploaded.Warning: getimagesize(D:xampptmpphp624E.tmp): failed to open stream: No such file or directory in D:xampphtdocsstronapostad.php on line 53File is not an image.Sorry, your file was not uploaded.Warning: getimagesize(D:xampptmpphp624F.tmp): failed to open stream: No such file or directory in D:xampphtdocsstronapostad.php on line 53File is not an image.Sorry, your file was not uploaded.Warning: getimagesize(D:xampptmpphp6250.tmp): failed to open stream: No such file or directory in D:xampphtdocsstronapostad.php on line 53File is not an image.Sorry, your file was not uploaded.Warning: getimagesize(D:xampptmpphp6260.tmp): failed to open stream: No such file or directory in D:xampphtdocsstronapostad.php on line 53File is not an image.Sorry, your file was not uploaded.Warning: getimagesize(D:xampptmpphp624D.tmp): failed to open stream: No such file or directory in D:xampphtdocsstronapostad.php on line 53File is not an image.Sorry, your file was not uploaded.Warning: getimagesize(D:xampptmpphp624E.tmp): failed to open stream: No such file or directory in D:xampphtdocsstronapostad.php on line 53File is not an image.Sorry, your file was not uploaded.Warning: getimagesize(D:xampptmpphp624F.tmp): failed to open stream: No such file or directory in D:xampphtdocsstronapostad.php on line 53File is not an image.Sorry, your file was not uploaded.Warning: getimagesize(D:xampptmpphp6250.tmp): failed to open stream: No such file or directory in D:xampphtdocsstronapostad.php on line 53File is not an image.Sorry, your file was not uploaded.Warning: getimagesize(D:xampptmpphp6260.tmp): failed to open stream: No such file or directory in D:xampphtdocsstronapostad.php on line 53File is not an image.Sorry, your file was not uploaded.

 

 

 

 

 

 

It looks like it try to upload single files multiple times and I have no clue how to sort it out to stop it.

 

I will be glad for any help given ^^

Edited by Voor
Link to comment
Share on other sites

Your error tells me your files aren't there. If they are, I check exact spelling including case.

Link to comment
Share on other sites

Your error tells me your files aren't there. If they are, I check exact spelling including case.

 

Thank You for Your reply :) I found solution. Problem was an array ^^ Input fields were passing empty value into array. So system were like "Oh there is more stuff in array so I need to loop through it". It was doing loop over empty values. I removed empty values from array before upload and it works fine :) Cheers for help anyway ^^

Edited by Voor
Link to comment
Share on other sites

  • 1 month later...

I have a problem with the script that should upload and submit files: the script allows to choose a file but does not submit it. what is the issue? thank you.

 

<?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;
}
}
// 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 {
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.";
}
}
?>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...