John Davidson

php - While uploading the randomly file then getting the error

0 comments
Message:


I have a multiple file types on my page. My issue is, if I start to upload the file or image from first then it's working.



and If I randomly upload the file then I am getting the error.



The is the full code.


HTML


<div class="container">
<form action="process.php" method="post" name="multiplefiles" enctype="multipart/form-data" id="myprofile">

<div class="input_fields_wrap">
<?php
$labelname = array('Label 1','Label 2','Label 3','Label 4','Label 5','Label 6','Label 7');
$labelCount=count($labelname);
?>
<div class="row mt-3">
<?php
for ($i=0; $i < $labelCount; $i++) {
?>

<div class="col-lg-4 mb-4 ">
<div class="documentUploadWrap">
<label><?php echo $labelname[$i];?></label>
<div class="upload_doc">
<input type="hidden" name="docUploadLabel[]" value="<?php echo $labelname[$i];?>">
<input type="file" name="docUpload[]" class="fileupload">
<input type="hidden" name="docUpload[]" value="">

<div class="uploadInfo">
<div class="upload_icon"></div>
<p>Drop your image here, or <span>browse</span></p>
<span>Supports: JPEG, PNG, PPT, PPTX, PDF <br />Max Size: 5MB</span>
</div>

</div>

</div>
</div>
<?php } ?>

</div>
<input type="submit" name="send" value="Submit" class="btn btn-primary">
</div>
</form>
</div>

Process


if (isset($_POST['send'])) {

$total = count(array_filter($_FILES['docUpload']['name']));

for($i=0; $i < $total; $i++) {

if(isset($_FILES['docUpload']['name'][$i]) && $_FILES['docUpload']['name'][$i] != "")
{
$foldername="uploads";
$uploadLabel=$_POST['docUploadLabel'][$i];

$image1 = $_FILES['docUpload']['name'][$i];
$filename = basename($image1);
$onlyfile = pathinfo($filename, PATHINFO_FILENAME);
$extension = pathinfo($filename, PATHINFO_EXTENSION);

$file = mt_rand();// random number
$newname = $onlyfile.'_'.$file.'.'.$extension;

echo $location='images/'.$foldername.'/'.$newname;

if($extension=='png' || $extension=='jpg' || $extension=='jpeg') {
compressImage($_FILES['docUpload']['tmp_name'][$i],$location,60);
}
else{
move_uploaded_file($_FILES['docUpload']['tmp_name'][$i], $location);
}
$uploadDoc=$newname;


}
else{
if (isset($_POST['docUpload'][$i]) && $_POST['docUpload'][$i]!= "") {
$uploadLabel=$_POST['docUploadLabel'][$i];
$uploadDoc=$_POST['docUpload'][$i];
}
}

$dataupload=array(
'upload_label'=>$uploadLabel,
'upload_name'=>$uploadDoc,
);
echo "<pre>";
print_r($dataupload);

}

}

echo"<pre>"; var_dump($_FILES); output. I uploaded the image in label 4 and label 6




My Answer:

Select File to Upload:


Select Image to Upload:



Rate this post

5 of 5 based on 7314 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us