Search code examples
symfonyimage-uploadingphp-ziparchive

Image from url is a string not UploadFile instanceOf ¿why?


I'm looking for a solution to this issue for a long time. This is an img => https://www.siweb.es/images/logo-light.png I want to store this image as zip file usin OneupUploaderBundle. So, when i get the image from Url usin file_get_contents or CURL it returns the image correctly but when i pass this file to $zip->addFile(); or an uoload service using Symfony\Component\HttpFoundation\File\UploadedFile both returns an error cause they are receiving a string as first Parameter.

I guess the problem is the file is not an instanceOf UploadeFile but i don't know how to convert it or use Filebag without a form.

public function testAction(Request $request){
    $term = 'https://www.siweb.es/images/logo-light.png';

    $image = $this->getimg($term);

    if ($image instanceof UploadedFile){
        $upload = $this->get('pablo.file_upload_service')->uploadZipFile($image,'test');
    }
    return $this->render('@pabloUser/Test/zip_test.html.twig',['upload' => $image]);
}

private function getimg($url) {
    $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';
    $headers[] = 'Connection: Keep-Alive';
    $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
    $user_agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
    $process = curl_init($url);
    curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($process, CURLOPT_HEADER, 0);
    curl_setopt($process, CURLOPT_USERAGENT, $user_agent);
    curl_setopt($process, CURLOPT_TIMEOUT, 30);
    curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
    $return = curl_exec($process);
    curl_close($process);
    return $return;
}

And the service:

public function uploadZipFile(UploadedFile $file,$folder){
    // Check if the file's mime type is in the list of allowed mime types.
    if (!in_array($file->getClientMimeType(), self::$allowedMimeTypes)) {
        $this->pushbulletService->notification('Error en la subida de archivos',sprintf('Files of type %s are not allowed.', $file->getClientMimeType()));
        throw new \InvalidArgumentException(sprintf('Files of type %s are not allowed.', $file->getClientMimeType()));
    }

    // Generate a unique filename based on the date and add file extension of the uploaded file
    $filename = sprintf('%s/%s.%s', $folder, uniqid(), $file->getClientOriginalExtension());

    $zipname = 'file.zip';
    $zip = new \ZipArchive();
    $zip->open($zipname,\ZipArchive::CREATE);
    $zip->addFile($file);
    $zip->close();

    $adapter = $this->filesystem->getAdapter();
    $adapter->write($filename, $zipname);

    return $filename;
}

Solution

  • The problem is that your result from getimg is a (binary) string containing the image data. In order to pass it on as an UploadedFile you have to store the image in a (temporary) file first and then pass the path to it in the constructor.

    It could look something like this:

    $data = $this->getimg(...);
    
    file_put_contents(sys_get_temp_dir() . '/filename.jpg', $data);
    $image = new UploadedFile(
        sys_get_temp_dir() . '/logo-light.png',
        'logo-light.png'
    );
    
    $upload = $this->get('pablo.file_upload_service')->uploadZipFile($image,'test');