簡體   English   中英

如何使用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);
    }}}
    

同樣,當我為文件 2 編寫相同的代碼時,它不起作用。 僅上傳第一個文件,而不上傳第二個文件。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM