Im using laravel-tinymce-simple-imageupload so that is possible to have the tinymce plugin with image upload.
But Im not understanding where to change the url path of the uploaded images so that is used an absolute path url?
As you can see, the plugin use a controller :
https://github.com/petehouston/laravel-tinymce-simple-imageupload/blob/master/src/TinymceController.php
public function uploadImage(Request $request)
{
$image = $request->file('image');
$filename = 'image_'.time().'_'.$image->hashName();
$image = $image->move(public_path('img'), $filename);
return mce_back($filename);
}
So as explained here : https://github.com/petehouston/laravel-tinymce-simple-imageupload#some-notes
You just have to create a new Controller Action in your application, route it and call it as explained :
#include('mceImageUpload::upload_form', ['upload_url' =>'YOUR_URL_FOR_HANDLING_IMAGE_UPLOAD'])
Related
The project works fine on my localhost but has issues on a live shared server.
I have tried adding this code to my index.php
// set the public path to this directory
$app->bind('path.public', function() {
return __DIR__;
});
I have tried adding this code in a new sym.php file in my public folder
<?php
$targetFolder = $_SERVER['DOCUMENT_ROOT'].'/storage/app/public';
$linkFolder = $_SERVER['DOCUMENT_ROOT'].'/public/storage';
symlink($targetFolder,$linkFolder);
echo 'Symlink process successfully completed';
?>
I have tried adding this on my web.php and then running site/linkstorage
Route::get('/linkstorage', function () {
Artisan::call('storage:link');
});
None of these solutions works
here is a snippet of my Controllers code:
public function storeBrand(Request $request){
$this->validate($request, ['brand_name'=> 'required',
'brand_url'=> 'required',
'brand_image'=>'image|nullable|max:1999']);
if($request->hasFile('brand_image')){
//1 : get filename with ext
$fileNameWithExt = $request->file('brand_image')->getClientOriginalName();
//2 : get just file name
$fileName = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
//3 : get just extension
$extension = $request->file('brand_image')->getClientOriginalExtension();
//4 : file name to store
$fileNameToStore = $fileName.'_'.time().'.'.$extension;
//upload image
$path =$request->file('brand_image')->storeAs('public/BrandImages', $fileNameToStore);
}
else{
$fileNameToStore ='noimage.jpg';
}
$brand=new Brand();
$brand->brand_name =$request->input('brand_name');
$brand->brand_url =$request->input('brand_url');
$brand->brand_image =$fileNameToStore;
$brand->save();
return redirect('/create_brand')->with('status', 'The '.$brand->brand_name.' Brand has been saved successfully. Create another one.');
Note
When an image is uploaded the path can be traced, but the image is not found, returns an empty image.
Thank you for your time and assistance.
I have got the solution:
I Wrote down this code in my web.php route file:
Route::get('/linkstorage', function () { $targetFolder = base_path().'/storage/app/public'; $linkFolder = $_SERVER['DOCUMENT_ROOT'].'/storage'; symlink($targetFolder, $linkFolder); });
After that I navigated to my url/linkstorage
It worked!!
Looking at the code in your controller, it seems correct. Perhaps the error is within your form in the Blade file associated with this method. Based on experience I tend to forget to write this and this could probably sort out your error.
Write this on the form tag.
<form action="{{ insert the route here }}" method="POST" enctype="multipart/form-data">
// insert form input fields here...
</form>
I have two project or url
First url like this :
http://myshop.dev/
Second url like this :
http://backend.myshop.dev/
If the second url, I upload image it will save the image file here :
http://myshop.dev/img/image1.jpg
Both projects use the same database
My code to upload image on the second project like this :
public function __construct(...)
{
...
$this->path = './img';
}
...
public function store(...) {
...
$filename = Input::file('photo')->getClientOriginalName();
...
Input::file('photo')->move($this->path, $filename);
...
}
It works
If I upload image it will save the image in backend-myshop\public\img
But, I want to uploaded image not saved there. But in my first project
So, I want the image file saved here : myshop\public\img
How can I do it?
Hi after uploading copy them from backend-myshop\public\img to myshop\public\img you can use this storage copy method
public function store(...) {
...
$filename = Input::file('photo')->getClientOriginalName();
...
Input::file('photo')->move($this->path, $filename);
Storage::copy('backend-myshop/public/img/'.$filename, 'myshop/public/img/'.$filename);
...
}
hope this will solve your problem
how can i upload original filename (file.jpg) to database when submitting file via form. Controller:
public function addCv(Request $request){
$cv = Cv::create($request->all());
$file = $request->file_name;
$filename = $file->getClientOriginalName();
Storage::putFileAs('public/uploads', $file, $filename);
return redirect()->back();
}
at the moment, this function uploads a path like this C:\xampp\tmp\php18DD.tmp.
Instead of that i want just filename and extension (file.extension).
Storage is working fine - storing with original name.
You could try
$file = $request->image->getClientOriginalName(); //Get Image Name
$extension = $request->image->getClientOriginalExtension(); //Get Image Extension
$fileName = $file.'.'.$extension; //Concatenate both to get FileName (eg: file.jpg)
I would suggest adding enctype="multipart/form-data" in the form tag in the view, from where you are uploading the file:
<form enctype="multipart/form-data">
You can use like below,
$this->getRequest()->files['name_of_file_field_in_post']->getClientOriginalName();
Reference: Get Uploaded File's Original Name
I need to store images in a backend for logged in users. The stored images need to be protected and not visible from the outside (public). I choosed a "storage" folder for this.
I came up with this in my Controller:
public function update(Request $request, $id)
{
//Show the image
echo '<img src="'.$_POST['img_val'].'" />';
//Get the base-64 string from data
$filteredData=substr($_POST['img_val'], strpos($_POST['img_val'], ",")+1);
//Decode the string
$unencodedData=base64_decode($filteredData);
//Save the image
$storagepath = storage_path('app/images/users/' . Auth::user()->id);
$imgoutput = file_put_contents($storagepath.'/flyer.png', $unencodedData);
return view('backend.flyers.index')->withImgoutput($imgoutput)
//->withStoragepath($storagepath);
}
after hitting the save button, which triggers the update() I am able to see the image in my view, and it is also stored in my folder (current users=10) "storage/app/images/users/10/flyer.png"
my question is how can I access the image path?
I want to show the stored image with img src="">. I have no idea what to put inside "src= ..."
While dealing with user file uploads in web applications, the major aspect is about user's content's security.
One should use secure way to upload private files of a user in web applications.
As in your case, you want to access user's image outside public folder.
This can be done in a most secure way as given below.
First of all create a directory right in the root directory of Laravel (where the public folder is located), let the directory's name be uploads. Use this directory to upload private user files.
In the case of images create an another directory inside uploads as uploads/images/ inside uploads directory so that you can have a different storage locations for different type of files.
Remember to upload the image in images directory with a different name and without their extensions so that it looks like a extension-less file.
Keep the file name and its extension in the database which can be used later to retain image's location.
Now you need to create a separate route to show user's image.
Route::get('users/{id}/profile_photo', 'PhotosController#showProfilePhoto')->name('users.showProfilePhoto');
PhotosController.php
class PhotosController extends Controller {
private $image_cache_expires = "Sat, 01 Jan 2050 00:00:00 GMT";
public function showProfilePhoto($id) {
$user = User::find($id);
$path = base_path() . '/uploads/images/';
if($user && $user->photo) // Column where user's photo name is stored in DB
{
$photo_path = $path . $user->photo; // eg: "file_name"
$photo_mime_type = $user->photo_mime_type; // eg: "image/jpeg"
$response = response()->make(File::get($photo_path));
$response->header("Content-Type", $photo_mime_type);
$response->header("Expires", $this->image_cache_expires);
return $response;
}
abort("404");
}
}
The method above inside PhotosController - showProfilePhoto($user_id) will run as soon as you access the route named - users.showProfilePhoto.
Your HTML code will look like this.
<img src="<?php echo route('users.showProfilePhoto', array('id' => $user->id)); ?>" alt="Alter Text Here">
The above code will work like a charm and the image will be shown to the user without declaring/publishing the proper image path to public.
According to me this is the secure way to deal with file uploads in web applications.
You can do this like this:
Route::get('images/{filename}', function ($filename)
{
$path = storage_path() . '/' . $filename;
if(!File::exists($path)) abort(404);
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
});
Reference:
Laravel 5 - How to access image uploaded in storage within View?
Or Alternatively you can use this library: https://github.com/thephpleague/glide
Just use composer to install it in your project
By default, this will render images from your storage, and allow you to do all sorts of things with it such as cropping, color correction etc.
Reference:
http://glide.thephpleague.com/
https://laracasts.com/discuss/channels/laravel/laravel-5-how-can-we-access-image-from-storage?page=1
Atimes you might have some images you do not wish to store in public directory for some various reasons.
Although storing your images has lots of advantages.
There are many ways you can achieve this, however I have this simple solution.
You should create a helper class like so if already don't have one
<?php namespace App\Services;
class Helper
{
public function imageToBase64($path)
{
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
return 'data:image/' . $type . ';base64,' . base64_encode($data);
}
}
Then in your view (blade)
#inject('helper', 'App\Services\Helper')
<img width="200" height="250" src="{{$helper->imageToBase64(storage_path('app/images/users/' . Auth::user()->id)}}">
It will work 100% work. Open file filesystem in app/config/filesystem.php and write like that
'profile' => [
'driver' => 'profile',
'root' => '/home/folder/public_html/projectname/public/profiles',
],
Add this file at top
use Illuminate\Support\Facades\Storage;
My variable name is
$directoryName = 'profile';
$imageName = $request->image; // image is array of base64 encoded urls
$directory_path ='profiles';
Below function save your file in public/profiles folder.
function UploadImagesByBase64($directoryName, $imageName,$directory_path)
{
$data = array();
$image = $imageName;
foreach ($image as $image_64) {
if($image_64 !=null){
$extension = explode('/', explode(':', substr($image_64, 0, strpos($image_64, ';')))[1])[1]; // .jpg .png .pdf
$replace = substr($image_64, 0, strpos($image_64, ',')+1);
// find substring fro replace here eg: data:image/png;base64,
$image = str_replace($replace, '', $image_64);
$image = str_replace(' ', '+', $image);
$imageName = Str::random(10).time().'.'.$extension;
Storage::disk($directoryName)->put($imageName, base64_decode($image));
$data[] = $directory_path.'/'.$imageName;
}
}
$imageName = implode(',', $data);
return $imageName;
}
I have tried to upload an image file in a sub-folder but I didn't get the help from internet. In Laravel website they mentioned about create folder but not explaining about sub-folders. My purpose is I need to upload an image in to sub-folder. Eg: userdata/1/1.jpg.
I have tried some code which is given below.
public function store(ProfileimageRequest $request, $id)
{
User::findOrFail($id);
$file = $request->file('file');
Storage::makeDirectory($id); //result is 1
$extension = $file->getClientOriginalExtension();
Storage::disk('local')->put($id.'.'.$extension, File::get($file)); //result is 1.jpg
}
The result of above code is creating folder "1" and image "1.jpg" but image is not creating inside the folder.
You probably have to specify that you want to the file to be uploaded into the folder. In the put() method add the folder's name with the file's name.
public function store(ProfileimageRequest $request, $id)
{
User::findOrFail($id);
$file = $request->file('file');
Storage::makeDirectory($id); //result is 1
$extension = $file->getClientOriginalExtension();
Storage::disk('local')->put($id.'/'.$id.'.'.$extension, File::get($file));
}
Let me know if this works out for you.