【问题标题】:PHP Upload - Multiple files & Restrict file typesPHP 上传 - 多个文件和限制文件类型
【发布时间】:2015-12-22 03:10:57
【问题描述】:

我目前有一个用户上传选项,它将上传到一个文件夹,并使用文件名更新数据库,以便稍后检索。

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

3 浏览部分(上传选项),1 是徽章照片,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>

任何帮助将不胜感激。

【问题讨论】:

标签: php file-upload


【解决方案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
    }
}

【讨论】:

  • 你的 html 表单应该使用 ""
  • 好的,我明白了。通过您所做的编辑,它不会将图像的路径上传到 db 表中的新列。它的结构方式是,它将 3 个名称写入一列,并在前面的上传名称上方。正确的? @pTi
  • 基本上我只是重组我的 if 语句以在提交时运行单独的查询,或者?我将如何做这一切。如果你能告诉我第二次我会做什么,我可以在那里展开它。
  • 根据您使用的 HTML 表单更改循环逻辑,因为它不正确
猜你喜欢
  • 1970-01-01
  • 2011-11-11
  • 1970-01-01
  • 1970-01-01
  • 2010-12-16
  • 1970-01-01
  • 2014-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多