使用用户输入的名称上传多个文件

时间:2020-06-06 15:56:45

标签: php html file-upload

我想上传多个具有用户输入名称的文件。对于单个文件,我可以像这样更改名称

<form action="submit.php" method="post" class="form-validate" enctype="multipart/form-data">
    <div class="form-group">
        <label class="form-label" for="name">File Name</label>
        <input type="text" name="filename" class="form-control" required /> 
        <input type="file" name="file" required>
    </div>
    <button type="submit" name="add" class="btn btn-primary mt-1">Submit</button>
</form>

submit.php

if(isset($_POST['add'])) {
    $filename = $_POST['filename'];
    $extension = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);
    //insert query to db
    move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/quotes/".$filename.".".$extension."");
}

这适用于单个图像。

但是对于多张图片,我以这种形式给了5个文件上传选项

<div class="form-group">
    <label class="form-label" for="file2">File Name</label>
    <input type="text" name="filename2" class="form-control"  />
    <input type="file" name="file2">
</div>

<div class="form-group">
    <label class="form-label" for="file3">File Name</label>
    <input type="text" name="filename32" class="form-control"  />
    <input type="file" name="file3">
</div>

<div class="form-group">
    <label class="form-label" for="file4">File Name</label>
    <input type="text" name="filename4" class="form-control"  />
    <input type="file" name="file4">
</div>

<div class="form-group">
    <label class="form-label" for="file5">File Name</label>
    <input type="text" name="filename5" class="form-control"  />
    <input type="file" name="file5">
</div>

我想提供文件名,所以我正在使用此方法而不是

<input type="file[]" multiple name="file">

但是在这里,除了第一次上传外,任何文件/文件名都可以为空。因此,我该如何插入数据库并上传到欲望文件夹。 每次上传之前,我是否需要检查文件名是否为空/文件?请建议

还是可以通过<input type="file[]" multiple name="file">方法更改每个文件的名称?

1 个答案:

答案 0 :(得分:0)

一个简短的示例,说明了我尝试解释的名称,但显然失败了。如果运行此命令,将会看到类似于以下内容的输出:

<?php
    error_reporting( E_ALL );
    ini_set( 'display_errors', 1 );

    function pre( $data ){
        printf('<pre>%s</pre>',print_r($data,true)); 
    }


    if( $_SERVER['REQUEST_METHOD']=='POST' ){
        ob_clean();
        pre($_POST);
        pre($_FILES);
        exit();
    }
?>
<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title></title>
    </head>
    <body>

        <form method='post' enctype='multipart/form-data'>
            <div class='form-group'>
                <label class='form-label'>File Name</label>
                <input type='text' name='filename[]' class='form-control'  />
                <input type='file' name='file[]'>
            </div>

            <div class='form-group'>
                <label class='form-label'>File Name</label>
                <input type='text' name='filename[]' class='form-control'  />
                <input type='file' name='file[]'>
            </div>

            <div class='form-group'>
                <label class='form-label'>File Name</label>
                <input type='text' name='filename[]' class='form-control'  />
                <input type='file' name='file[]'>
            </div>

            <div class='form-group'>
                <label class='form-label'>File Name</label>
                <input type='text' name='filename[]' class='form-control'  />
                <input type='file' name='file[]'>
            </div>

            <input type='submit' />

        </form>
    </body>
</html>

典型输出:

Array
(
    [filename] => Array
        (
            [0] => borat
            [1] => mouse
            [2] => thumbs
            [3] => suspense
        )

)
Array
(
    [file] => Array
        (
            [name] => Array
                (
                    [0] => 2480-1653-2.3558884.jpg.gallery.jpg
                    [1] => 84679948.jpg
                    [2] => CBUESFMUsAAM534.jpg
                    [3] => FullThrottle.jpg
                )

            [type] => Array
                (
                    [0] => image/jpeg
                    [1] => image/jpeg
                    [2] => image/jpeg
                    [3] => image/jpeg
                )

            [tmp_name] => Array
                (
                    [0] => C:\Program Files (x86)\PHP7.2\uploadtemp\php3719.tmp
                    [1] => C:\Program Files (x86)\PHP7.2\uploadtemp\php371A.tmp
                    [2] => C:\Program Files (x86)\PHP7.2\uploadtemp\php371B.tmp
                    [3] => C:\Program Files (x86)\PHP7.2\uploadtemp\php371C.tmp
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                )

            [size] => Array
                (
                    [0] => 43124
                    [1] => 80518
                    [2] => 58358
                    [3] => 68344
                )

        )

)

由此,您可以使用数组索引将文件链接到相应的文本