0

I'm getting the blob URL as blob:http://localhost:3000/a7e2a2d5-6c2d-462c-acbf-171ff64e1e2dfrom the frontend.It contains a zip file.I need to download the zip file from that url and store it in the server. $request->values['modelfile'] has this blob url. How can i get the file from that URL in the controller?

CodeZombie
  • 2,027
  • 3
  • 16
  • 30

2 Answers2

1

You can download the file from url using :

$content = file_get_contents('http://example.com/image.php'); // URL of your blob object
file_put_contents('/my/folder/flower.jpg', $content);

Reference : https://stackoverflow.com/a/724397/1740102

Or

$ch = curl_init('http://example.com/image.php');
$fp = fopen('/my/folder/flower.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

Reference : https://stackoverflow.com/a/724449/1740102

Ganesh Ghalame
  • 6,367
  • 3
  • 24
  • 29
  • I tried as you said but it returns an error `file_get_contents(blob:http://localhost:3000/fd147006-83c9-4e0a-8954-491a741e7744): failed to open stream: No such file or directory`.When i hit the url in browser ,i'm able to download the file. – CodeZombie Feb 28 '18 at 05:28
  • try by removing `blob:` from url – Ganesh Ghalame Feb 28 '18 at 05:43
  • i tried removing `blob:` also .I got an error say `file_get_contents(http://localhost:3000/0a7131cb-66a7-4a5e-8c35-6a83301af743): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found` – CodeZombie Feb 28 '18 at 05:59
  • Updated the ans try with curl – Ganesh Ghalame Feb 28 '18 at 06:02
  • i pointed the download path to various folder ,still its throwing an error `fopen(C:\xampp\htdocs\smartviewbackend\public): failed to open stream: Permission denied` – CodeZombie Feb 28 '18 at 06:15
  • If your file exists on same server where you were running your code ? (local) if yes why you are downloading it, just copy it using `copy('image1.jpg', 'del/image1.jpg');` I am not getting what you trying to do. – Ganesh Ghalame Feb 28 '18 at 06:31
  • As you suggested i have added the curl code where I have changed from `$fp = fopen('/my/folder/flower.gif', 'wb');` to `$fp = fopen(public_path(), 'wb');` – CodeZombie Feb 28 '18 at 06:36
  • you saved my day,so simple, but I guess the bolb should be sent in "data:application/pdf;base64.....",meaning the front end sould use filereader to send this bolb – aghed aljlad Apr 11 '22 at 16:05
0
    <?php
$upload_dir = "upload/";
$img = $_POST['blob_data'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = $upload_dir . mktime() . ".png";
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
?>
Ahmed Nasser
  • 2,041
  • 2
  • 7
  • 7