Have you tried downloading the image off of S3 and opening it using a text editor to see what the contents is? It may contain the URL like you said but good to check.
Your syntax appears to be correct for the Filesystem, have you tried using dd($file) rather than echo?
You're right, a text editor shows these files as paths, e.g. "/tmp/phpGZ7FD1"
dd($file) outputs:
UploadedFile {#27 ▼
-test: false
-originalName: "Screenshot from 2015-01-20 11:12:20.png"
-mimeType: "image/png"
-size: 30755
-error: 0
}
The problem remains the same: how to upload the file to S3.
Ideally I want to do this without storing the file in a temporary folder on my server. I'm not even sure where the tmp folder is.
Again, any help would be much appreciated.
I have come up with a crude solution
View (as before)
Controller:
public function postUploadMedia(Request $request)
{
//Get Uploaded File (using Facade, which is more in-line with official documentation)
$file = \Illuminate\Support\Facades\Request::file('file');
//Perform some Validation on file type, size etc. here...
//Set Filename (example)
$filename = "uploadedfile";
//Set Directory
$directory = "media";
/************************
* Move File to Local Folder, copy to S3, then delete.
* Note storage_path().'/app' is defined in filesystems.php in config
************************/
// Move File
$file->move(storage_path().'/app', $filename);
//Get File and Put on S3
$uploadedfile = Storage::get($filename);
Storage::disk('s3')->put($directory.'/'.$filename, $uploadedfile);
//Delete File from local folder
Storage::delete($filename);
//Return view here
}
Note that I am also saving the filename and location to a mysql table (not shown in code above).
This works, but feels a bit rough, and not very laravel-like.
One of my concerns is what happens to the '/tmp/astag131' files demonstrated in my opening post. I have been unable to find these files or the /tmp folder on my system or in the Laravel directory (is it the root /tmp file??).
It might be that these files/folder are being deleted immediately, but I am concerned that its hidden somewhere (potentially insecure) and filling up with junk, never to be rebooted as it's a production server.
I'm still open to better suggestions
Edit: Thanks to the posts below I can see that this method may result in a snappier experience for users, as they will not have to wait for the upload to S3. I have marked it as the solution to my problem.
Nothing wrong with your approach. In fact, it's got a nice advantage. Uploading to S3 takes time and you don't really want the user to have to wait around for that, so you save it locally (not in the temp folder; move it to an uploads folder somewhere) and then show it to the user straight away, who can then go off and be happy someplace else on your site. Then in a background queue you upload the file to S3 and once that's done you can update the database with the S3 stuff and delete the local file.
I just took a look at how I'm uploading it in my own code. I'm running it through Intervention Image and uploading it without storing locally looks like:
$file = $request->file('file');
$image = Image::make($image); // facade from the Intervention Image package
$payload = (string)$image->encode(); // encode it as string
Flysystem::disk('s3')->put($path, $payload);
I also perform some resizing and so on, but it allows me to go from upload form to directly uploading to Amazon. The only issue is that it does wait for the upload to complete before providing a response to the browser, so a solution where you store it and then queue a job then respond to the user, allowing the queue to perform the actual upload may be better.
Thank you everyone for your thoughtful input, you've been very helpful.
I'm going to make a small edit to my post and mark it as the solution. Intervention Image looks great, I'll consider adding it to my project in the future.
Larave file system or disk ? which I will choose ? Could you suggest me ? I am newbie in laravel . I know, how they are working. But I do not know which is better and what are the situation of using one of them.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community