Multiple files uploading in multidimensional array

前端 未结 1 1891
名媛妹妹
名媛妹妹 2021-01-24 01:43

I have a php form with unknown number of rows where user can upload files. For this purpose I use two dimensional array.

1条回答
  •  耶瑟儿~
    2021-01-24 01:56

    There were some changes to be made, in addition to the ones in the comments. Major issues are:

    1. You were trying to get both files and post data into one single array, when both $_POST and $_FILES are, in PHP nature, totally separated entities. So you're ending up trying to access a single array row when there is actually two row arrays inside both these superglobals.
    2. Your $target_file was never declared, and your $target_dir has too many slashes.
    3. The item 1 made you access $val the wrong way.

    These are the final corrections I came up with, keeping the logic for your own environment. The explanation to each part is commented inside the code.

    HTML form

    
    
    
    
    	
                
    File:

    PHP Post Script

     900000000) { // using the proper index reference for file
                die("Sorry, your file is too large."); //die if error
            }
    
            // Check if there are error msg in $_FILES
            if ($_FILES['row']['error'][$val] != 0) {
                die("Sorry, your file was not uploaded."); // die if error
                // if everything is ok, try to upload file
            } else {
                // point your move_files with the final name of the actual file to be moved
                if (move_uploaded_file($_FILES['row']['tmp_name'][$val], $target_dir.'/'.$_FILES['row']['name'][$val])) {
                    echo "The file ". basename($_FILES['row']['name'][$val]). " has been uploaded.
    "; } else { die("Sorry, there was an error uploading your file."); } } }

    Final Output (after uploading two dummy files)

    Array
    (
        [row] => Array
            (
                [0] => 0
                [1] => 1
            )
    
        [submit] => Upload
    )
    Array
    (
        [row] => Array
            (
                [name] => Array
                    (
                        [0] => dummyfile1.docx
                        [1] => dummyfile2.mpp
                    )
    
                [type] => Array
                    (
                        [0] => application/vnd.openxmlformats-officedocument.wordprocessingml.document
                        [1] => application/vnd.ms-project
                    )
    
                [tmp_name] => Array
                    (
                        [0] => C:\Windows\Temp\php73DA.tmp
                        [1] => C:\Windows\Temp\php73DB.tmp
                    )
    
                [error] => Array
                    (
                        [0] => 0
                        [1] => 0
                    )
    
                [size] => Array
                    (
                        [0] => 0
                        [1] => 180224
                    )
    
            )
    
    )
    The file dummyfile1.docx has been uploaded.
    The file dummyfile2.mpp has been uploaded.
    

    0 讨论(0)
提交回复
热议问题