The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

multiple file upload with slim framework

Sonu Sindhu's Avatar

Sonu Sindhu

04 Jul, 2012 04:51 AM

<input type="file" name="upload" value="" multiple/>
<input type="submit" value="Upload Now"/>

I have a form like this and I want to upload multiple image with single upload input file can anyone help me regarding this.

Thanks so much in advanced

  1. Support Staff 1 Posted by Brian Nesbitt on 05 Jul, 2012 02:46 AM

    Brian Nesbitt's Avatar

    Rudimentary example code. Put this into your index.php and create a writeable uploads directory.

    You'll notice the name property of the input tag for the file needs to include the brackets to indicate an array of uploads name="uploads[]".

    Note: the multiple tag doesn't work in IE, if you care.

    require 'vendor/autoload.php';
    
    $app = new Slim();
    
    $app->get('/', function () use ($app) {
        echo <<<__HTML
    <!DOCTYPE html>
    <html>
    <head>
        <title>Multiple Upload</title>
    </head>
    <body>
        <p>Please upload some images.</p>
        <form action="/" enctype="multipart/form-data" method="post">
            <input type="file" name="uploads[]" multiple="multiple"/><br/>
            <input type="submit" value="Upload Now"/>
        </form>
    </body>
    </html>
    __HTML;
    });
    
    $app->post('/', function () {
        if (!isset($_FILES['uploads'])) {
            echo "No files uploaded!!";
            return;
        }
    
        $imgs = array();
    
        $files = $_FILES['uploads'];
    
        $cnt = count($files['name']);
        for($i = 0 ; $i < $cnt ; $i++) {
            if ($files['error'][$i] === 0) {
                $name = uniqid('img-'.date('Ymd').'-');
                if (move_uploaded_file($files['tmp_name'][$i], 'uploads/' . $name) === true) {
                    $imgs[] = array('url' => '/uploads/' . $name, 'name' => $files['name'][$i]);
                }
    
            }
        }
    
        $imageCount = count($imgs);
        if ($imageCount == 0) {
            echo 'No files uploaded!!  <p><a href="/">Try again</a>';
            return;
        }
    
        $plural = ($imageCount == 1) ? '' : 's';
    
        echo <<<__HTML
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8"/>
        <title>Multiple Upload</title>
    </head>
    <body>
        <h1>Thanks for uploading $imageCount file{$plural}.</h1>
        <h2>File List</h2>
    __HTML;
    
        foreach($imgs as $img) {
            printf('%s <img src="%s" width="50" height="50" /><br/>', $img['name'], $img['url']);
        }
    
        echo <<<__HTML
        <p><a href="/">Upload more</a>
    </body>
    </html>
    __HTML;
    
    });
    
    $app->run();
    
  2. 2 Posted by Sonu Sindhu on 06 Jul, 2012 06:05 PM

    Sonu Sindhu's Avatar

    Thanks for this

  3. 3 Posted by Sonu Sindhu on 06 Jul, 2012 06:10 PM

    Sonu Sindhu's Avatar

    Thanks so much Brian Nesbitt .

  4. Support Staff 4 Posted by Brian Nesbitt on 07 Jul, 2012 02:00 AM

    Brian Nesbitt's Avatar

    Your welcome !

  5. Brian Nesbitt closed this discussion on 07 Jul, 2012 02:00 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac