创建一个包含多个图像上传的PHP联系表单

时间:2016-09-28 09:45:27

标签: php html forms upload jpeg

我需要创建一个html联系表单,用于收集一些数据并通过PHP脚本上传8个图像

这是我的HTML:

<form action="formmail.php" onsubmit="return controlloform()" id="form" name="form" method="POST" enctype="multipart/form-data">
    <input name="email" id="email" placeholder="Email address" type="text" value="" maxlength="40">
    <input name="tel" id="tel" type="text" placeholder="Order number" value="" maxlength="20">
    <textarea name="msg" placeholder="Message" value="" maxlength="300"></textarea>
    Upload a photo:
    <p>
        <p>
            <input name="file" id="file" class="button" type="file" value="">
            <p>

                <p>
                    <td colspan="2">
                        <input type="submit" class="button" value="Submit" />
                    </td>
                    </tr>
                    </table>
</form>

我不知道如何创建上传脚本,我需要做的是上传2个4个图像块,检查文件的扩展名(仅允许jpg和png)和大小(图像最大500 kb) )。

我已经找到了一些上传脚本(不是100%适合我需要的)但我不知道如何将上传部分包含在代码的其余部分中

如果有人能帮助我会很棒

非常感谢

1 个答案:

答案 0 :(得分:0)

此代码进行多次上传

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

    // Loop through each file
    for($i=0; $i<$total; $i++) {
      //Get the temp file path
      $tmpFilePath = $_FILES['photo_file']['tmp_name'][$i];

      //Make sure we have a filepath
      if ($tmpFilePath != ""){
        //Setup our new file path
        $newFilePath = "./uploadFiles/" . $_FILES['photo_file']['name'][$i];

        //Upload the file into the temp dir
        if(move_uploaded_file($tmpFilePath, $newFilePath)) {

          //Handle other code here

        }
      }
    }

如果要检查文件大小,请插入此代码

if ($_FILES["photo_file"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}

此代码进行验证

$allowed =  array('gif','png' ,'jpg');
$filename = $_FILES['photo_file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed) ) {
    $uploadOk = 0;
}