I cant save a image using the storage method.
If I do: $file->move(public_path().'/', $file->getClientOriginalName());
The image will get saved and I can open it in finder.
But if I save the image using:
$extension = $file->guessExtension();
$filename = 'thumnail_'.$id.'.'.$extension;
if ($file) {
$s3 = Storage::disk('public')->put($filename, $file);
}
It will also get saved to the folder but then I cant open it because the file is corrupted
And to be clear "file" is sent to my model from a controller: $file = $request->file('images');
Related
I am using a file upload plugin in Vue to upload some image and pdf files through an API and it has an option to create a blob field when uploading the files.
The request sent to Laravel is as follows. I can access the blob from the browser by copying and pasting the URL on the browser.
On the Server side code, I am trying to save the file with the following code but the saved file is some corrupted 64byte file rather than the actual image. How would we store the blob as a normal file in the filesystem?
if ($request->has('files')) {
$files = $request->get('files');
$urls = [];
foreach ($files as $file) {
$filename = 'files/' . $file['name'];
// Upload File to s3
Storage::disk('s3')->put($filename, $file['blob']);
Storage::disk('s3')->setVisibility($filename, 'public');
$url = Storage::disk('s3')->url($filename);
$urls[] = $url;
}
return response()->json(['urls' => $urls]);
}
Try by replacing this:
Storage::disk('s3')->put($filename, $file['blob']);
with this:
Storage::disk('s3')->put($filename, base64_decode($file['blob']));
I already given 777 permission to my images folder. Charts Table is also saving all record of image. I am going to attach table:charts structure here.
public function store(Request $request)
{
$input = $request->all();
$tradeID= Auth::user()->trade()->create($input);
if($file = $request->file('file'))
{
$name = time() . $file->getClientOriginalName();
$file->move('images', $name);
$photo = Chart::create(['file'=>$name]);
$input['photo_id'] = $photo->id;
}
$tradeID->chart()->create($input);
}
Try to change destination path from relative to absolute
public function store(Request $request)
{
$input = $request->all();
$tradeID= Auth::user()->trade()->create($input);
if($file = $request->file('file'))
{
$name = time() . $file->getClientOriginalName();
$file->move( public_path() . '/images/', $name); // absolute destination path
$photo = Chart::create(['file'=>$name]);
$input['photo_id'] = $photo->id;
}
$tradeID->chart()->create($input);
}
try this
if($file = $request->file('file'))
{
$name = time() . $file->getClientOriginalName();
$path = $file->storeAs('images', $name,'public');
$photo = Chart::create(['file'=>$name]);
$input['photo_id'] = $photo->id;
}
you can retreive the file with /storage/public/file_name or /file_name depending on where you told laravel to store the public files
You are not actually storing the file, you are actually moving a non-existent file.
What you might consider
You, most probably want to access uploaded file from public path. If so, you should store this file to particular folder in your storage directory and create a symlink to that directory in your public path.
Here's an example:
Storage::disks('public')->put($filename, $uploadedFile); // filename = 'images/imageFile.jpg'
This creates a file in your storage/app/public/images directory.
Then, you can create a symlink, laravel provide a simple command to do so.
php artisan storage:link
This creates a symlink to your public disk.
Then, you can access the image at http://your-site-server/storage/images/imageFile.jpg
Hope it helps.
$name = time().'.'. $file->getClientOriginalName(); //depends on ur requirement
$file->move(public_path('images'), $name);
Are you trying to move uploaded file or existing file ?
To move file you need to do:
File::move($oldPath, $newPath);
the old path and new path should include full path with filename (and extension)
To store an uploaded file you need to do:
Storage::disk('diskname')->put($newPath, File::get($file));
the new path is full path with filename (and extension).
disk is optional , you can store file directly with Storage::put()..
Disks are located in config/filesystems.php
The File::get($file) , $file is your input from post request $file = $request->file('file') , basically it gets contents of uploaded file.
UPD#1
Okay , you can do this:
$request->file('file')->store(public_path('images'));
Or with Storage class:
// get uploaded file
$file = $request->file('file');
// returns the original file name.
$original = $file->getClientOriginalName();
// get filename with extension like demo.php
$filename = pathinfo($original)['basename'];
// get public path to images folder
$path = public_path('images');
// concat public path with filename
$filePath = $path.'/'.$filename;
// store uploaded file to path
$store = Storage::put($filePath, File::get($file));
I want to upload an image and retrieve in from database when i need this.My code is perfectly working to upload image but it's path did not save in database.In database it's showing Null.Here is my code:
if ($request->hasFile('profilePic')) {
$file = array('profilePic' => Input::file('profilePic'));
$destinationPath = 'img/'; // upload path
$extension = Input::file('profilePic')->getClientOriginalExtension();
$fileName = rand(11111,99999).'.'.$extension; // renaming image
Input::file('profilePic')->move($destinationPath, $fileName);
}else{
echo "Please Upload Your Profile Image!";
}
$profile->save();
My Question:
How to save image path into database?
And how to retrieve image from database?
Updated: Profile.php
class Profile extends Model
{
protected $table = "profiles";
public $fillable = ["firstName","lastName","middleName","DOB","gender","featuredProfile","email","phone","summary","profilePic"];
}
In your code there should be like this:
$profile->profilePic = $fileName;
& then
$profile->save();
1.How to save image path into database?
To save your full image path in a database field, the code is:
$profile->profilePic = $destinationPath.$fileName;
$profile->save();
2.And how to retrieve image from database?
In your view file using blade engine, write HTML code like this. In the image src you have to provide profilePic data as shown in the below HTML code:
<img src="{{asset($profile->profilePic)}}"/>
try this
$path = $file->getRealPath();;
$pos = strpos($path,'/public/');
if ($pos !== false) {
$path = substr($path, $pos + 1);
}
$input['file'] = $path;
I'm trying to upload a file. Heres my controller
if ($request->hasFile('photo')) {
$destinationPath = 'images/quizzes/'; // upload path
$extension = $file->getClientOriginalExtension(); // getting image extension
$fileName = rand(11111,99999).'.'.$extension; // renameing image
$request['photo'] = $fileName;
$request['image_path'] = $destinationPath;
$file->move($destinationPath, $fileName); // uploading file to given path
$quiz = Quiz::create($request->all())
}else{
\Session::flash('flash_message_delete','Please select files to upload.');
return redirect()->back();
}
It gives me FileNotFoundException in File.php line 37:The file "" does not exist. This was working fine the other day, idk what happes..i try to interchange the arrangement like:
$destinationPath = 'images/quizzes/'; // upload path
$extension = $file->getClientOriginalExtension(); // getting image extension
$fileName = rand(11111,99999).'.'.$extension; // renameing image
$request['photo'] = $fileName;
$request['image_path'] = $destinationPath;
$quiz = Quiz::create($request->all());
$file->move($destinationPath, $fileName); // uploading file to given path
I wont have error like this, and it save to the database, THE PROBLEM is the photo field instead of the $filename it has wierd /tmp/somestrings inserted.
I am saving canvas to a file. Code creates a png file in the upload folder. It is working correctly on local machine but when i try to run this on the server, I am not able to find the file in the upload folder. am i giving wrong path ?
After file creation i am printing alert, so i get file creation alert but file is not just created in the upload folder .
if ( isset($_POST["image"]) && !empty($_POST["image"]) ) {
// get the image data
$data = $_POST['image'];
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
//Image name
$filename ="image". md5(uniqid()) . '.png';
$file ='../upload/'.$filename;
// decode the image data and save it to file
file_put_contents($file,$data);
}
make sure directory '../upload' exists or create it first