Symfony url中的图像是一个字符串,未上载文件instanceOf¿;为什么?
我一直在寻找解决这个问题的办法。 这是一个img=> 我想使用OneUploaderBundle将此图像存储为zip文件。 因此,当我使用file_get_contents或CURL从Url获取图像时,它会正确返回图像,但当我将此文件传递到$zip->addFile()时;或者使用Symfony\Component\HttpFoundation\File\UploadedFile的uoload服务都返回一个错误,因为它们接收到一个字符串作为第一个参数 我想问题是该文件不是UploadeFile的实例,但我不知道如何转换它或在没有表单的情况下使用FilebagSymfony url中的图像是一个字符串,未上载文件instanceOf¿;为什么?,symfony,image-uploading,ziparchive,Symfony,Image Uploading,Ziparchive,我一直在寻找解决这个问题的办法。 这是一个img=> 我想使用OneUploaderBundle将此图像存储为zip文件。 因此,当我使用file_get_contents或CURL从Url获取图像时,它会正确返回图像,但当我将此文件传递到$zip->addFile()时;或者使用Symfony\Component\HttpFoundation\File\UploadedFile的uoload服务都返回一个错误,因为它们接收到一个字符串作为第一个参数 我想问题是该文件不是UploadeFile的
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;
}
以及服务:
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;
}
问题是来自
getimg
的结果是一个包含图像数据的(二进制)字符串。为了把它作为一个整体传递下去
它可能看起来像这样:
$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');
您是否尝试获取图像并将其传递到上载的文件?Awsome@dbrumann。只是修一下。文件内容和新上载文件中的临时路径必须相同。类似于sys\u get\u temp\u dir()。/filename.jpg‘是的,你是对的。我只是将文件名添加到您稍后请求的文件中,而忘记了该方法调用。抢手货