how to upload multi-files using Phalcon

Hi Guys,

i can use $this->request->getUploadedFiles() to upload single file,but cannot support multi-files upload,pls kindly share how to set up the code to upload multi-files,thanks.

if ($this->request->hasFiles()) { foreach ($this->request->getUploadedFiles() as $file){ $pathname = "../public/images/user/".date("Ym",time())."/"; if(!isdir($pathname)){ mkdir($pathname); } $filename = time().rand(00000,99999).strtolower( strrchr( $file->getName() , '.' ) ); $headerpic = $pathname.$filename; $file->moveTo('.'.$header_pic); } }

edited Aug '16

What's not working exactly? This should be the way to process multiple files. Have you checked your form input name? Does it have []?

<input type="file" name="images[]" multiple>

http://www.w3schools.com/tags/att_input_multiple.asp


64.1k

you can check here if you find something usefull https://github.com/stanislav-web/phalcon-uploader

For upload multiple files you can construct your view using difrent instruction types: plain html, php, or Volt script template.

PHP example for 'form' tag

<?php echo $this->tag->form(array("controller/action", "name" => "actionForm", "method" => "post", "class" => "form-horizontal", 'enctype'=> "multipart/form-data"  )) ?>

Volt example for 'form' tag

{{ form('controller/action', 'name': 'actionForm', 'method': 'post', 'class':'form-horizontal', 'enctype': 'multipart/form-data') }}

Or using plain html

<form target='controller/action' name='actionForm' method='post' class='form-horizontal' enctype='multipart/form-data' >

You must add the input tag for file upload and use the 'multiple' parameter like this

<input type="file" name="files[]" multiple>
<?php echo $this->tag->submitButton(array( "submit" , "class" => "btn btn-primary" ) ); ?>
</form>

in the controller you mus create a action to receibe the post methot with the files data. This is an example to upload a pdf file or images.

First detect if ther are file uploads on the post, and then get a file data at a time to do some thing (copy to /some/rute/ or change name, etc.) Then you can save the data to a database or do some thing whit the file.

...
public $extIMG = array(
        'image/jpeg',
        'image/png',
        'image/bmp',
        'application/pdf',
    );
...

public function actionAction()
{
    // are there  files ?
    if( $this->request->hasFiles() == true)
    {
        foreach( $this->request->getUploadedFiles() as $file)
        {              
            // is it a valid extension?
            if ( in_array($file->getType() , $this->extIMG) && $file->getError() == 0 )
            {
                $Name      = preg_replace("/[^A-Za-z0-9.]/", '-', $file->getName() );
                $FileName  = "/some/path/to/pdfs" . $Name;

                switch( strtolower($file->getExtension()) ){
                    case "pdf":
                    // move file to needed path";
                    if(!$file->moveTo($FileName))
                    {   
                        // something  goes worong
                        $this->flash->error("Ocurrio un error al subir el documento.");
                        return $this->dispatcher->forward(
                                    array(
                                        "controller" => "incident`",
                                        "action" => "new",
                                    ));
                    }
                    // all fine the file has been moved
                    break;
                }

                // do some thing
            }   
         }
    }
}

Get fun whit the code, good luk.