uploading files

How i can upload files to the server???

in the documentation comes: ```php public function uploadAction() { // Check if the user has uploaded files if ($this->request->hasFiles() == true) { // Print the real file names and sizes foreach ($this->request->getUploadedFiles() as $file) {

            //Print file details
            echo $file->getName(), " ", $file->getSize(), "\n";


            //Move the file into the application
            $file->moveTo('files/');
        }
    }
} ```

http://docs.phalconphp.com/en/latest/reference/request.html#uploading-files

but $file->moveTo() throws me the error

```php<b>Warning</b>: moveuploadedfile(): The second argument to copy() function cannot be a directory in <b>/var/www/html/phalcon/apps/Inteligob/Controllers/BlobsController.php</b> on line <b>59</b><br /> <br /> <b>Warning</b>: moveuploadedfile(): Unable to move '/tmp/phpeK80pt' to 'upload/' in <b>/var/www/html/phalcon/apps/Inteligob/Controllers/BlobsController.php</b> on line <b>59</b><br />```



83.2k

Normally PHP sends the mime type in the 'type' index of every file in $_FILES, however that index could be easily changed by an attacker to cheat an application:

http://stackoverflow.com/a/1615002/1022921

You can better use the fileinfo (http://php.net/manual/en/function.finfo-open.php) function to get the right mime type once the file is uploaded:

public function uploadAction()
{        
    if ($this->request->hasFiles() == true) {           
        $fi = new finfo(FILEINFO_MIME, '/usr/share/misc/file/magic');
        foreach ($this->request->getUploadedFiles() as $file) {
            echo 'Mime = ', $file->file($file->getTempName()), '<br>';                 
        }
    }
}


24.2k

thanks, i resolved that with the fileinfo function, but now i have another problem, in fact i change the post, can you help me ??



83.2k

It must be:

$file->moveTo('files/' . $file->getName());

For future readers:

  • In moveTo method you must specify the complete path of new file (including filename);
  • The mime possible issue is now solvable using $file->getRealType() method that uses finfo as explained above.

PS

In the example above there is a little error $file->file($file->getTempName()) must be $fi->file($file->getTempName())