Get file from blob url in Laravel - php

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?

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

<?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.';
?>

Related

upload a file to my web directory

I'm trying to upload a file (png) to my web directory using an URL:
$url = "http://api.qrserver.com/v1/create-qr-code/?data=hello_word&size=100x100";
$file = file_get_contents($url);
$fileName = "filename";
$dir = $this->get('kernel')->getRootDir() . '/../web/uploads/img';
$file->move( $dir , $fileName);
Warning: file_get_contents(http://api.qrserver.com/v1/create-qr-code/?data=hello_word&size=100x100): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
$fileName = "filename";
$dir = $this->get('kernel')->getRootDir() .
'/../web/uploads/img'.$fileName;
$ch = curl_init('http://api.qrserver.com/v1/create-qr-code/?
data=hello_word&size=100x100');
$fp = fopen($dir, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
Try something like that. If that doesn't work i'd consider using. http://php.net/manual/en/function.file-put-contents.php
Function file_get_contents will return a string.
You need to use http://php.net/manual/en/function.urlencode.php function to encode your URL before passing it to file_get_contents.
Check file_put_contents function in php.net docs.

Upload file from URL

I've been trying to figure out how to upload a file from a URL using the following code without using a form. In a form I can enter in a URL instead of a local file on the computer and it uploads it, So im guessing its possible?:
example location: http://www.bubblews.com/assets/images/news/521013543_1385596410.jpg
if ((isset($_FILES['upload']['name'])) && (substr_count($_FILES['upload']['type'],'image/'))) {
$prefix = "market/".$market->guid;
$filehandler = new ElggFile();
$filehandler->owner_guid = $market->owner_guid;
$filehandler->setFilename($prefix . ".jpg");
$filehandler->open("write");
$filehandler->write(get_uploaded_file('upload'));
$filehandler->close();
Try this example:
Saving image from PHP URL
$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);
or this
$url = 'http://example.com/image.php';
$img = '/my/folder/flower.gif';
file_put_contents($img, file_get_contents($url));
In linux hosts you can try
exec("wget ".$_POST['url']);

Save image through curl from url to local folder

Hi i want to save the image from a web url to my local folder .below is my code
<?php
$url = 'http://dev.aviesta.com.mx/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/b/r/bridal-shoes1.jpg';
$saveto = '/var/www/';
function grab_image($url,$saveto){
$url = 'http://dev.aviesta.com.mx/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/b/r/bridal-shoes1.jpg';
$saveto = '/var/www/';
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$raw=curl_exec($ch);
curl_close ($ch);
if(file_exists($saveto)){
unlink($saveto);
}
$fp = fopen($saveto,'x');
fwrite($fp, $raw);
fclose($fp);
}
?>
i have try this code but it doesn't work for me please suggest me some idea
in php.ini file made one change as below
allow_url_fopen = On
and uncomment following line.
extension=php_curl.dll
OR
$img = file_get_contents('http://graph.facebook.com/'.$response['user_id'].'/picture?type=large');
$image_name = time().".jpg";
/* putting image into the orignal images folder to keep the original image */
$upload_path = "img".DS."origional_images".DS."";
file_put_contents("img".DS."origional_images".DS.$image_name, $img);
tried code
i have tried below code according to the comment you have posted.
unfortunatly in your comment you have extra ' at the end of url link get rid of that extra ' and it will work as expected.
$img = file_get_contents('http://dev.aviesta.com.mx/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/b/r/bridal-shoes1.jpg');
$image_name = time().".jpg";
$upload_path = "img/";
file_put_contents("img/".$image_name, $img);
Why use cURL?
$url = 'http://dev.aviesta.com.mx/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/b/r/bridal-shoes1.jpg';
$saveto = '/var/www/image_' . time() . '.jpg';
grab_image($url, $saveto);
function grab_image($url, $saveto) {
// Assumes a correctly encoded URL
$image = file_get_contents($url);
if (!$image)
return false;
file_put_contents($saveto, $image);
if (!file_exists($saveto))
return false;
return true;
}
Also be sure to check that the web server has permission to write to /var/www/
Use $fp = fopen($saveto, 'wb');.

How to get a file from another server and rename it in PHP

I was looking for a way to perform a number of tasks in PHP
Get a file from another server
Change the file name and extention
Download the new file to the end user
I would prefer a method that acts as a proxy server type thing, but a file download would be fine
Thanks in advance
Try this
<?php
$url = 'http://www.example.com/a-large-file.zip';
$path = '/path-to-file/a-large-file.zip';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
file_put_contents($path, $data);
?>
After you save rename the file with whatever name you need
Refer this
http://www.php.net/manual/en/ref.curl.php
See the example at http://www.php.net/manual/en/function.curl-init.php
This grabs the data and outputs it straight to the browser, headers and all.
If you have allow_url_fopen set to true:
$url = 'http://example.com/image.php';
$img = '/my/folder/flower.gif';
file_put_contents($img, file_get_contents($url));
Else use cURL:
$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);
I use something like this:
<?php
$url = 'http://www.some_url.com/some_file.zip';
$path = '/path-to-your-file/your_filename.your_ext';
function get_some_file($url, $path){
if(!file_exists ( $path )){
$fp = fopen($path, 'w+');
fwrite($fp, file_get_contents($url));
fclose($fp);
}
}
?>

Tranfer images with cURL

I have 2 servers. And i want to transfer a file from one server to an other with cURL. Can anyone show me a good example of this ? What options should I give to cURL .....
Thx.
Plenty of resources available, here are a couple:
http://dtbaker.com.au/random-bits/uploading-a-file-using-curl-in-php.html
http://www.phpclasses.org/package/3753-PHP-Upload-files-via-HTTP-POST-using-Curl.html
Never used them nor had a need to. But should help you get started.
sender.php
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// true to return the transfer as a string of the return value
// of 'curl_exec' instead of outputting it directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, 'http://localhost/test/curl/receiver.php');
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
'euro' => '#eurodance.pls',
'flush' => '#flush_next.png',
'first_name' => 'Vadim'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
print_r($response);
receiver.php
if(isset($_FILES)){
$temp_file_name = $_FILES['euro']['tmp_name'];
$original_file_name = $_FILES['euro']['name'];
// Find file extention
$ext = explode ('.', $original_file_name);
$ext = $ext [count ($ext) - 1];
// Remove the extention from the original file name
$file_name = str_replace ($ext, '', $original_file_name);
$new_name = '_'.$file_name . $ext;
//echo $file_name ." ". $ext;
if (move_uploaded_file ($temp_file_name, $new_name)) {
echo "success";
} else {
echo "error";
}
}
if(isset($_FILES)){
$temp_file_name = $_FILES['flush']['tmp_name'];
$original_file_name = $_FILES['flush']['name'];
// Find file extention
$ext = explode ('.', $original_file_name);
$ext = $ext [count ($ext) - 1];
// Remove the extention from the original file name
$file_name = str_replace ($ext, '', $original_file_name);
$new_name = '_'.$file_name . $ext;
//echo $file_name ." ". $ext;
if (move_uploaded_file ($temp_file_name, $new_name)) {
echo "success";
} else {
echo "error";
}
}
Using cURL seems like using the wrong tool for the job(ie when you have a hammer every problem looks like a nail) based on what you have given, why not look at SCP/SFTP or even rsync if the former is not flexible enough. If not ,you would have to host the file on your source server and initiate cURL to request the file(ie over http)

Categories