简体   繁体   English

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

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

Hi I am facing a problem while uploading two files using php.嗨,我在使用 php 上传两个文件时遇到问题。 I have this html input form with two files field我有这个带有两个文件字段的 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>

This is my php code.这是我的php代码。 Here I take the file data from the form but only the first one is uploaded, second file is not.在这里,我从表单中获取文件数据,但只上传了第一个文件,没有上传第二个文件。

        <?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);
    }}}
    

Similarly when I write the same code for file two it doesn't work.同样,当我为文件 2 编写相同的代码时,它不起作用。 Only the first file is uploaded not the second file.仅上传第一个文件,而不上传第二个文件。

    $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);
    }}}

} ?> } ?>

Kindly guide me and help me in this code.请指导我并帮助我完成此代码。

If you use the same name for the file input field but use the array style syntax for the name you can assign your own identifier within the square braces which will be available in the POST / FILES array later.如果您对文件输入字段使用相同的名称,但对名称使用数组样式语法,您可以在方括号内分配您自己的标识符,稍后将在 POST / FILES 数组中提供该标识符。 This identifier can be used to separate the different types of files so you can fork the logic as appropriate to your needs.此标识符可用于分隔不同类型的文件,以便您可以根据需要分叉逻辑。

The following shows a basic usage of this methodology - it might prove of interest but it might not.下面显示了这种方法的基本用法 - 它可能会被证明很有趣,但也可能不会。

<?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过输入表单使用php更新mysql中的两个字段 - How to update two fields in mysql using php via a input form 使用相同形式但使用jquery php的不同输入字段上传多个文件 - multiple file upload using same form but different input fields using jquery php 如何使用php将html表单输入字段发送到电子邮件 - How to send html form input fields to email using php 如何使用html表单和PHP提高文件上传速度? - How to improve upload speed of files using html form and PHP? 使用PHP将来自两个不同输入字段的两个图像上传到数据库 - Upload two images from two different input fields to database with PHP 使用PHP从表单输入更新HTML文件 - Updating HTML files using PHP from form input 使用 PHP 的两个输入文件上传两个文件 - Upload two files using two input files with PHP 如何使用 php 创建基于输入创建更多表单字段的文件上传表单? - How to create a file upload form that creates more form fields based on input using php? Yii2:如何从两个字段中上传两个图像的形式 - Yii2: how to upload two images from two fields in form 如何在同一页面上使用html和php从下拉选择更改事件的输入字段中从数据库中获取选定数据 - how to get selected data from database in input fields on select change event of dropdown using html and php in the same page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM