since i'm a newbie in PHP i'm asking this question. I can do a single insert image with a nice validation but i want to do this with 3 image. (leave the validation part). Just correct me if i'm wrong. Any help is appreciated.
Can i insert three images with the following format? It takes 7 days to ask next question, please help me out guys.
<?php
if (isset($_POST['upload']))
{
$fileName1 = $_FILES["uploaded_one"]["name"];
$fileTmp1 = $_FILES["uploaded_one"]["tmp_name"];
$fileType1 = $_FILES["uploaded_one"]["type"];
$fileSize1 = $_FILES["uploaded_one"]["size"];
$fileName2 = $_FILES["uploaded_two"]["name"];
$fileTmp2 = $_FILES["uploaded_two"]["tmp_name"];
$fileType2 = $_FILES["uploaded_two"]["type"];
$fileSize2 = $_FILES["uploaded_two"]["size"];
$fileName3 = $_FILES["uploaded_three"]["name"];
$fileTmp3 = $_FILES["uploaded_three"]["tmp_name"];
$fileType3 = $_FILES["uploaded_three"]["type"];
$fileSize3 = $_FILES["uploaded_three"]["size"];
if (!preg_match("/.(jpeg|jpg|png)$/i", $fileName1 || $fileName2 || $fileName3) )
$folder = "upload/";
$moveResult1 = move_uploaded_file($fileTmp1, "$folder/$fileName1");
$moveResult2 = move_uploaded_file($fileTmp2, "$folder/$fileName2");
$moveResult3 = move_uploaded_file($fileTmp3, "$folder/$fileName3");
$insert = "SQL INSERT QUERY TIRED TO TYPE";
$run = mysqli_query($db,$insert);
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="uploaded_one" />
<input type="file" name="uploaded_two" />
<input type="file" name="uploaded_three" />
<button name="upload">Submit</button>
</form>
And I think my preg_match() is giving error. is there a better way to do this?
Can you use just an input multiple for upload your 3 files un the same input ?
You could create an array with the name suffix and loop through the array while you check each file separately.
foreach(['one', 'two', 'three'] as $item)
$name = $_FILES["uploaded_{$item}"]["name"];
$tmp = $_FILES["uploaded_{$item}"]["tmp_name"];
$type = $_FILES["uploaded_{$item}"]["type"];
$size = $_FILES["uploaded_{$item}"]["size"];
if (!preg_match("/.(jpeg|jpg|png)$/i", $name))
$folder = "upload/";
$result = move_uploaded_file($tmp, "$folder/$name");
}