PHP上传 - 多个文件&限制文件类型

时间:2015-12-22 03:10:58

标签: php file-upload

我目前为用户提供了一个上传选项,它将上传到一个文件夹,并使用文件名更新数据库,然后再进行检索。

我希望能够进行多次上传,并且每个上传的文件都按照他们已选择并上传所述文件的上传字段进入特定列。

3浏览部分(上传选项),1为badgephoto,2为驾驶执照,3为社会保障卡。

如果可能的话,将每个文件上传到他们自己指定的文件夹位置。

我还想限制图像类型,PDF或文档文件的所有异常文件。

我目前有以下内容。

if(isset($_POST['btn-upload']))
{    

    $file = rand(1000,100000)."-".$_FILES['file']['name'];
    $file_loc = $_FILES['file']['tmp_name'];
    $file_size = $_FILES['file']['size'];
    $file_type = $_FILES['file']['type'];
    $folder="badge/";

    // new file size in KB
    $new_size = $file_size/1024;  
    // new file size in KB

    // make file name in lower case
    $new_file_name = strtolower($file);
    // make file name in lower case

    $final_file=str_replace(' ',$FirstName,$new_file_name);

    if(move_uploaded_file($file_loc,$folder.$final_file))
    {
        $sql="UPDATE users t1 SET t1.badgephoto='$final_file', t1.badgetype='$file_type', t1.badgesize='$new_size' WHERE t1.API='$API'";

        mysql_query($sql);
        ?>
        <script>
        alert('successfully uploaded');
        window.location.href='badgephoto.php?success';
        </script>
        <?php
    }
    else
    {
?>
        <script>
        alert('error while uploading file');
        window.location.href='badgephoto.php?fail';
        </script>
        <?php
    }
}

HTML

    <form action="badgephoto.php" method="post" enctype="multipart/form-data">
    <input type="file" name="file" accept="application/msword, text/plain, application/pdf, image/*" /> <!-- would be badgephoto -->
    <input type="file" name="file_drivers" accept="application/msword, text/plain, application/pdf, image/*" />
    <input type="file" name="file_social" accept="application/msword, text/plain, application/pdf, image/*" />
    <button type="submit" name="btn-upload">Upload Badge Photo</button>
    </form>

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

if(isset($_POST['btn-upload']))
{    
    foreach ($_FILES as $key => $value) {
    $file = rand(1000,100000)."-".$_FILES[$key]['name'];
    $file_loc = $_FILES[$key]['tmp_name'];
    $file_size = $_FILES[$key]['size'];
    $file_type = $_FILES[$key]['type'];
    $folder="badge/";

    // new file size in KB
    $new_size = $file_size/1024;  
    // new file size in KB

    // make file name in lower case
    $new_file_name = strtolower($file);
    // make file name in lower case

    $final_file=str_replace(' ',$FirstName,$new_file_name);

    $allowedTypes = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
    $detectedType = exif_imagetype($file_loc);
    $fileTypecorrect = in_array($detectedType, $allowedTypes);


        if($new_size < 5120 && $fileTypecorrect){

            if(move_uploaded_file($file_loc,$folder.$final_file))
            {
                $sql="UPDATE users t1 SET t1.badgephoto='$final_file', t1.badgetype='$file_type', t1.badgesize='$new_size' WHERE t1.API='$API'";

                mysql_query($sql);
                ?>
                <script>
                alert('successfully uploaded');
                window.location.href='badgephoto.php?success';
                </script>
                <?php
            }
        }
    }
    else
    {
?>
        <script>
        alert('error while uploading file');
        window.location.href='badgephoto.php?fail';
        </script>
        <?php
    }
}