如何使用 php 从同一表单中的两个 html 输入字段上传文件?

How to upload files from two html input fields in the same form using php?

您好,我在使用 php 上传两个文件时遇到问题。 我有这个 html 输入表单,其中有两个文件字段

<form class="form-group" method="post" enctype="multipart/form-data">

<input type="file" accept=".jpg, .jpeg, .png" id="img" name="displaypic" required/>     

<input type="file" accept=".pptx" name="presentation" required>

<button name="submit>Submit</submit>   
</form>

这是我的 php 代码。这里我从表单中获取文件数据,但只上传了第一个,没有上传第二个文件。

        <?php
    if(isset($_POST['submit'])){
    
        $file = $_FILES['displaypic'];
                  $fileName = $_FILES['displaypic']['name'];
                  $tempName = $_FILES['displaypic']['tmp_name'];
                  $size = $_FILES['displaypic']['size'];
                  $error = $_FILES['displaypic']['error'];
                  $format = $_FILES['displaypic']['type'];
                  $fileExt = explode('.', $fileName);
                  $fileActualExt = strtolower(end($fileExt));
                  $allowed = array('jpg', 'jpeg','png');
 
                  
            if(in_array($fileActualExt, $allowed)) {
                if ($error === 0) {
                    if ($size<2e6) {
                        $newname = $tid.".".$fileActualExt;
                        $location = 'displays/'.$newname;
                        move_uploaded_file($tempName,$location);
    }}}
    

同样,当我为文件二编写相同的代码时,它不起作用。只上传了第一个文件,没有上传第二个文件。

    $file_ppt = $_FILES['presentation'];
                  $fileName = $_FILES['presentation']['name'];
                  $tempName = $_FILES['presentation']['tmp_name'];
                  $size = $_FILES['presentation']['size'];
                  $error = $_FILES['presentation']['error'];
                  $format = $_FILES['presentation']['type'];
                  $fileExt = explode('.', $fileName);
                  $fileActualExt = strtolower(end($fileExt));
                  $allowed = array('pptx');
 
                  
            if(in_array($fileActualExt, $allowed)) {
                if ($error === 0) {
                    if ($size<10e6) {
                        $newname = $tid.".".$fileActualExt;
                        $location = 'presentations/'.$newname;
                        move_uploaded_file($tempName,$location);
    }}}
    }
    ?>

如果您对文件输入字段使用相同的名称,但对名称使用数组样式语法,您可以在方括号内分配您自己的标识符,稍后将在 POST / FILES 数组中提供.此标识符可用于分隔不同类型的文件,因此您可以根据需要分叉逻辑。

以下显示了此方法的基本用法 - 它可能会引起人们的兴趣,但也可能不会。

<?php
    $field='xfiles';     // Whatever you wish to name your file input elements
    $errors=array();
    $status=array();
    $maxfs=pow(1024,2) * 5; //5Mb or whatever.... 10e6?
    
    if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_FILES[ $field ] ) ){
        $obj=$_FILES[ $field ];
        
        foreach( $obj['name'] as $index => $void ){
            $name=$obj['name'][ $index ];
            $tmp=$obj['tmp_name'][ $index ];
            $error=$obj['error'][ $index ];
            $type=$obj['type'][ $index ];
            $size=$obj['size'][ $index ];
            $ext=strtolower(pathinfo($name,PATHINFO_EXTENSION));
            
            $allowed=(object)array(
                'displaypic'    =>  array('jpg','jpeg','png'),
                'presentation'  =>  array('ppt','pptx')
            );
            
            if( $error!==UPLOAD_ERR_OK )$errors[]=sprintf('An error [code:%d] occurred with file %s',$error,$name);
            if( !in_array( $ext, $allowed->$index ) )$errors[]=sprintf('Incorrect file extension %s for %s',$ext,$name);
            if( $size > $maxfs )$errors[]=sprintf('The file %s is too large @%d',$name,$size);
            
            
            
            if( empty( $errors ) ){
                $status[]=sprintf('<div>%s uploaded successfully - save to db, do a happy little dance or whatever else you need to do!</div>', $name );
                
                #move_uploaded_file($tmp,'/path/to/new/folder/'.$name);
                #$sql='insert into ....';
                
            }
        }
    }
?>
<!DOCTYPE html>
<html lang='en'>
    <head>
        <title>PHP: Multiple file uploads</title>
        <meta charset='utf-8' />
    </head>
    <body>
        <form class='form-group' method='post' enctype='multipart/form-data'>
            <label>Display - [accept:jpg,png]<input type='file' accept='.jpg, .jpeg, .png' name='xfiles[displaypic]' required /></label>
            <label>Presentation - [accept:ppt,pptx] <input type='file' accept='.ppt, .pptx' name='xfiles[presentation]' required /></label>
            <input type='submit' />
            <?php
                if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $status ) ){
                    echo '<h1>Success</h1>';
                    foreach($status as $msg)printf('<div>%s</div>',$msg);
                }
                
                if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $errors ) ){
                    echo '<h1>Error</h1>';
                    foreach($errors as $error)printf('<div>%s</div>',$error);
                }
            ?>
        </form>
    </body>
</html>