How to get an image file from API in Symfony2 - symfony-2.7

I would like to get an image from REST API by using file_get_content() and store in Symfony2.
$image = file_get_contents('https://foobar, false, $context)
But I am stacked when I am trying to sage the file.
$image->move($app['config']['image_temp_realdir'], $filename);
This line cause "Error: Call to a member function move() on string".
I understand this file data is not a File entity, but how can I convert this into File Entity? Or any method I can use to get file instead of file_get_content()?
Thanks in advance to your help.

Sorry, the problem resolved.
Saved the file by file_put_contents() and created new File.
file_put_contents($app['config']['image_temp_realdir'].'/'.$filename, $image);
$file = new File($app['config']['image_temp_realdir'].'/'.$filename);

Related

Custom file name while keeping extension

Following this part of the docs:
https://laravel.com/docs/5.3/filesystem#file-uploads
I'm trying to store a file (an image) and specify a custom file name, but I don't want to change the extension.
If I do:
$request->file('avatar')->storeAs('avatars', 'custom_file_name');
The file will save as custom_file_name (with no extension), rather than custom_file_name.png).
How can I specify a custom file name while keeping the original file extension?
You can get extension of uploaded file with:
$extension = $request->photo->extension();
And apply it manually to the stored file name.
The UploadedFile class also contains methods for accessing the file's fully-qualified path and its extension. The extension method will attempt to guess the file's extension based on its contents.
https://laravel.com/docs/5.4/requests#files
The code below checks if request has a file by id/name avatar and then stores it with custom_name.{uploaded_files_extension}
if($request->hasFile('avatar')) {
$imgfile = Input::file('avatar');
$custom_name = 'custom_name'.$imgfile->getClientOriginalExtension();
$request->file('avatar')->storeAs('avatars', $custom_name);
}
Just to buttress #alexey-mezenin answer,
you might have issue adding the extension
//get extension
$extension = $request->file('featured_img')->extension();
$request->file('featured_img')->storeAs('/public/album',$post->title.'.'.$extension);
With that you can have a complete code stored in your /storage/public/album

Laravel 5 file path error in console

I am creating a web application using Laravel 5.
I want to use AngularJS.
I have created a file named all-products.php in views directory which contains data in JSON format.
When I gave path to this file in http.open() it gives an error as http://localhost/user/app/resources/views/all-products.php 404 (Not Found).
Here is my JS code:
var app = angular.module("products", []);
app.controller("productsController", function($scope, $http){
$http.get("http://localhost/user/app/resources/views/all-products.php").success(function(response) {
$scope.records = response.records;
});
});
Please tell me how I can solve this ?
Thanks in advance.
You need to put the file in your public directory e.g.
http.get("http://localhost/all-products.json")
Would be a file at path public\all-product.json (assuming your homepage is http://localhost).
Or, if the file is dynamically created, you need to create a route, and return the .json data from there using Response::json(['some data]);

Import file in laravel

Firstly, I Want to read excel file which user is uploading and saving data to DB.
I have tried load function using excel API.
Excel::load($file_name_real, function($input) {
$results = $input->all();
// $input->dump();
})->download('xls');
But its not happening So now I want to move File to server then will read file then will unlink. By following function
$input1->move('/laravel');
But its not moving file.

blobstore images get_serving_url

I am new to the Google App Engine and I am trying to use the Blobstore to store images that I want to display later on.
The image storage works fine. Now I want to dynamically change some images in my html code. Therefore I need a method of getting the images out of the blobstore and passing them. I am using Python. I found the get_serving_url-command, which seemed to be the perfect fit. Sadly, this causes an Error and I seem to be unable to fix it.
My basic code looks like this:
blob_key = "yu343mQ7kT4344N434ewQ=="
if blob_key:
blob_info = blobstore.get(blob_key)
if blob_info:
img = images.Image(blob_key=blob_key)
url = images.get_serving_url(blob_key)
...
Everytime the function gets called, I get the following Error in my Log Console.
File "C:\Program Files
(x86)\Google\google_appengine\google\appengine\ext\remote_api\remote_api_stub.py",
line 234, in _MakeRealSyncCall
raise pickle.loads(response_pb.exception())
AttributeError: 'ImagesNotImplementedServiceStub' object has no
attribute 'THREADSAFE'
I have no idea how to fix it or if I am doing something terribly wrong.
I am very grateful for your support! Thank you in advance!
Have a nice day
You probably need an instance of BlobKey so if you are getting blob_info successfully try:
img = images.Image(blob_key=blob_info.key())
url = images.get_serving_url(blob_info.key())

Need help transferring a jpg from web server to S3 - PHP and CodeIgniter

I have a Flash application that captures an image and passes an encoded image to my web server. The web server then decodes the image and saves it to a tmp directory. That part works fine. Next I want to move this image from the web sever to my S3 account but am having trouble. I am using the code below. Any help is appreciated.
In the CodeIgniter Controller Constructor:
$this->load->library('S3');
In the function (also within the controller for now)
/************** S3 upload example***************/
if (!defined('awsAccessKey')) define('awsAccessKey', 'xxxxxxx');
if (!defined('awsSecretKey')) define('awsSecretKey', 'xxxxxxx');
$s3 = new S3(awsAccessKey, awsSecretKey);
if($s3->putObjectFile($filePathTemp, "bucket", $filePathNew, "ACL_PUBLIC_READ_WRITE")){
#unlink($filePathTemp);
}
I can't even get the $s3 variable to return anything in an "echo" statement by returning a value on the first line of the S3 constructor. I can't access the S3 object/class, but the following statement returns a "1":
echo "S3 --> " . class_exists('S3');
Thanks in advance for your time.
-Tim
I'm assuming (you don't say: you should specify which one) you are using this. Your usage looks correct: I'm assuming your keys are wrong. If
print_r($s3->listBuckets());
doesn't return anything check your server logs.

Resources