来自 url 的图像是一个字符串,而不是 UploadFile instanceOf ¿为什么?
Image from url is a string not UploadFile instanceOf ¿why?
我一直在寻找这个问题的解决方案。
这是一个 img => https://www.siweb.es/images/logo-light.png
我想使用 OneupUploaderBundle 将此图像存储为 zip 文件。
因此,当我从 Url 使用 file_get_contents 或 CURL 获取图像时 returns 图像正确但是当我将此文件传递给 $zip->addFile();或使用 Symfony\Component\HttpFoundation\File\UploadedFile 的 uoload 服务都 returns 一个错误,因为他们正在接收一个字符串作为第一个参数。
我想问题是文件不是 UploadeFile 的 instanceOf,但我不知道如何转换它或在没有表单的情况下使用 Filebag。
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
得到的结果是一个包含图像数据的(二进制)字符串。为了将其作为 UploadedFile you have to store the image in a (temporary) file first and then pass the path to it in the constructor.
传递
它可能看起来像这样:
$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');
我一直在寻找这个问题的解决方案。 这是一个 img => https://www.siweb.es/images/logo-light.png 我想使用 OneupUploaderBundle 将此图像存储为 zip 文件。 因此,当我从 Url 使用 file_get_contents 或 CURL 获取图像时 returns 图像正确但是当我将此文件传递给 $zip->addFile();或使用 Symfony\Component\HttpFoundation\File\UploadedFile 的 uoload 服务都 returns 一个错误,因为他们正在接收一个字符串作为第一个参数。
我想问题是文件不是 UploadeFile 的 instanceOf,但我不知道如何转换它或在没有表单的情况下使用 Filebag。
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
得到的结果是一个包含图像数据的(二进制)字符串。为了将其作为 UploadedFile you have to store the image in a (temporary) file first and then pass the path to it in the constructor.
它可能看起来像这样:
$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');