0

Please tell me there is a tag <input name = "upload []" type = "file" multiple = "multiple" /> How to make a php condition if input has a file. Thanks.

1 Answers1

0

Somthing like this would do the trick

<?php
    if(isset($_FILES['upload']['tmp_name']))
    {
        // Number of uploaded files
        $num_files = count($_FILES['upload']['tmp_name']);

        /** loop through the array of files ***/
        for($i=0; $i < $num_files;$i++)
        {
            // check if there is a file in the array
            if(!is_uploaded_file($_FILES['upload']['tmp_name'][$i]))
            {
                $messages[] = 'No file uploaded';
            }
            else
            {
                // copy the file to the specified dir 
                if(@copy($_FILES['upload']['tmp_name'][$i],$upload_dir.'/'.$_FILES['upload']['name'][$i]))
                {
                    /*** give praise and thanks to the php gods ***/
                    $messages[] = $_FILES['upload']['name'][$i].' uploaded';
                }
                else
                {
                    /*** an error message ***/
                    $messages[] = 'Uploading '.$_FILES['upload']['name'][$i].' Failed';
                }
            }
        }
    }
?>
Patrick Simard
  • 2,294
  • 3
  • 24
  • 38
  • Why `copy` and not http://php.net/manual/en/function.move-uploaded-file.php? I'd also recommend not using `@` for production code. If you have errors you should handle it appropriately – user3783243 Nov 20 '18 at 04:11