Upload Multiple Files In PHP

前端 未结 2 1728
终归单人心
终归单人心 2021-02-06 15:40

I wonder whether someone may be able to help me please.

Using some excellent online tutorials I\'ve put together the code below which allows a user to upload image files

相关标签:
2条回答
  • PHP

    foreach($_FILES['image']['name'] as $i => $name)
    {
        // now $name holds the original file name
        $tmp_name = $_FILES['image']['tmp_name'][$i];
        $error = $_FILES['image']['error'][$i];
        $size = $_FILES['image']['size'][$i];
        $type = $_FILES['image']['type'][$i];
    
        if($error === UPLOAD_ERR_OK)
        {
            $extension = getExtension($name);
            if( ! in_array(strtolower($extension, array('jpg', 'jpeg', 'png') )
            {
                // Error, invalid extension detected
            }
            else if ($size > $maxAllowedFileSize /* needs to be defined elsewhere */)
            {
                // Error, file too large
            }
            else
            {
                // No errors
                $newFileName = 'foo'; // You'll probably want something unique for each file.
                if(move_uploaded_file($tmp_file, $newFileName))
                {
                    // Uploaded file successfully moved to new location
                    $thumbName = 'thumb_image_name';
                    $thumb = make_thumb($newFileName, $thumbName, WIDTH, HEIGHT);
                }
            }
        }
    }
    

    HTML

    <form method="post" enctype="multipart/form-data">
        <input name="image[]" type="file" class="image-upload" />
        <input name="image[]" type="file" class="image-upload" />
        <input name="image[]" type="file" class="image-upload" />
        <input name="image[]" type="file" class="image-upload" />
        <input name="image[]" type="file" class="image-upload" />
    
        <!-- You need to add more, including a submit button -->
    </form>
    

    Note the name of the input elements. The [] at the end cause PHP to create an array and add the items to the array.

    0 讨论(0)
  • 2021-02-06 15:51

    If you don't bother paying for it, you may have a look at PHP File Uploader, it's a very nifty and useful tool, that I already used for a couple of sites.
    Not that cheap, but definitely worth the cost.

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