I'm getting the blob URL as
blob:http://localhost:3000/a7e2a2d5-6c2d-462c-acbf-171ff64e1e2d
from 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?
Asked
Active
Viewed 1.0k times
0

CodeZombie
- 2,027
- 3
- 16
- 30
-
could you provide some relevant data like how you get this URL, how you store your data on db?? – Sohel0415 Feb 28 '18 at 05:04
-
@Sohel0415 yeah sure,i will edit my question now – CodeZombie Feb 28 '18 at 05:05
-
can you check `dd ($_FILES)` if it exists in the file object, from where you get blob? I mean upload ? If its just URL and you want to download then you can use `file_put_contents` – Ganesh Ghalame Feb 28 '18 at 05:06
-
@GaneshGhalame i'm not getting the file object from the frontend as i mentioned above i'm just getting the blob URL .Please help me out – CodeZombie Feb 28 '18 at 05:16
-
and you wanted to store that blob file by downloading from that url correct? – Ganesh Ghalame Feb 28 '18 at 05:19
-
@Sohel0415 i want to save file in the server and add the image path in the database.so,the question is how to get the file from url – CodeZombie Feb 28 '18 at 05:20
-
@GaneshGhalame yes correct – CodeZombie Feb 28 '18 at 05:20
-
its really easy check here https://stackoverflow.com/questions/724391/saving-image-from-php-url – Ganesh Ghalame Feb 28 '18 at 05:21
2 Answers
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
-
-
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
-
-
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