Devise\Media\Files\FileDownloader::download PHP Method

download() public method

Download a file from the given $url and save it to $toPath with $filename. If $filename is null then we just use the name from the $url
public download ( string $url, string $toPath, string $filename ) : string
$url string
$toPath string
$filename string
return string
    public function download($url, $toPath, $filename)
    {
        $basepath = dirname($toPath . '/' . $filename);
        $basename = basename($filename);
        // create this directory if it doesn't exist
        if (!is_dir($basepath)) {
            mkdir($basepath, 0755, true);
        }
        // use curl to download
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $data = curl_exec($ch);
        curl_close($ch);
        // save the $data into this file
        return file_put_contents($basepath . '/' . $basename, $data);
    }

Usage Example

Beispiel #1
0
 /**
  * Fire events when you see this job has been
  * called back
  *
  * @param $output
  * @param $storagePath
  * @return void
  */
 public function handle($output, $storagePath)
 {
     switch ($output['state']) {
         case 'finished':
             $filename = $output['label'];
             $fromUrl = $output['url'];
             $file = $this->FileDownloader->download($fromUrl, $storagePath, $filename);
             $this->Event->fire('devise.encoding.zencoder.finished', [$file, $output]);
             break;
         default:
             $this->Event->fire('devise.encoding.zencoder.error', [$output]);
             break;
     }
 }