Laravel unisharp file manager combine file and image manager - laravel

Last week I discovered the Unisharp file manager plugin.
After a lot of tweaking on the plugin i managed to get the plugin working in my browser without opening a new page (Like you supposed to get with the plugin)
A view from my browser:
The first image are my images that i have uploaded and the second picture is my files that i have uploaded.
Now my problem is that I'am trying to get them both in 1 file manager so the files and images are together in one file manager.
I have tried to tweak the config/lfm.php, public/photos/ into public/files/ like you see here below
just like where the files are linked to but when i do that the images and files that i have previous uploaded wont load
So if you got any suggestions on how to combine the files and images manager together or got a other suggestion for an other plugin it would be great,
Greetings,
Noah

Full disclosure: I am the package maintainer, work for UniSharp.
You have done it well!
The only problem is, images have thumbs while files don't(files show icons).
When the get parameter "type" is set to be "Image"(link here), files and images will show but the thumbs of files will left broken.
Solution: You can set default image like below, in case of broken file thumbs.
<img onclick="useFile('{{ $file_name }}')" onError="this.src='/img/default.jpg'">

So after seeing the comment #Stream Huang. I manage to get it working, its true that the image got thumbs and the files like a pdf dont.
First thing that I did was at the UploadController#Upload change
if ('Images' === $this->file_type) {
$this->makeThumb($dest_path, $new_filename);
}
After that I made a variable that get the extension/file name of the file that you just uploaded and filter it on images.
$fileName = $file->getMimeType();
if ($fileName == "image/jpeg" || $fileName == "image/png") {
$this->makeThumb($dest_path, $new_filename);
}
In the ItemController I added a [$key] to the array
ItemController#getFileInfos (original)
$file_info[$key] = [
'name' => $file_name,
'size' => $file_size,
'created' => $file_created,
'type' => $file_type,
'icon' => $icon,
];
to:
$file_info[$key] = [
'name' => $file_name,
'size' => $file_size,
'created' => $file_created,
'type' => $file_type,
'icon' => $icon,
'title' => $file_data[$key]->title,
'alt' => $file_data[$key]->alt,
'id' => $file_data[$key]->id,
];
The title,alt and id are comming from the database (When I upload a image I first need to enter a title, alt and the image before uploading it. The data that I have I store it in the database so I can use that data for like a news article).
I also changed a couple of blades mainly the item and the index
item.blade.php (original)
#if($type == 'Images')
<img id="{{ $file_name }}" src="{{ asset($thumb_src) }}" class="pointer" onclick="useFile('{{ $file_name }}')">
#else
<i class="fa {{ $file['icon'] }} fa-5x" style="height:200px;cursor:pointer;padding-top:60px;" onclick="useFile('{{ $file_name }}')"></i>
#endif
item.blade.php (edit)
$file_name = $file_info[$key]['name'];
$filebroken = substr($file_name, -3);
$extension = array_pop($filebroken);
The file_info[$key]['name'] is a file name that is getting from the database just like image.jpg , i get the last 3 characters of the name so its most likely .jpg or .png if its a image.
After that I'm checking if $extension variable is a jpg or a png, else i make it a icon of it.
#if($extension == 'png' || $extension == 'jpg')
<img id="{{ $file_name }}" src="{{ asset($thumb_src) }} class="pointer" onclick="useFile('{{ $file_name }}')">
#else
<i class="fa {{ $file['icon'] }} fa-5x" onclick="useFile('{{ $file_name }}')"></i>
#endif

Related

user uploaded images should be stored in the project like public/userprofileimage.jpg or it will be affected by version control

Consider the case where a user uploaded his profile image and it was placed in the project as public/userprofileimage.jpg when the repository was at version 1.0.0.
The project was then pulled from GitHub, changed, or new code was added, and it was pushed to the repository, becoming version 2.0.0.
How can I add the latest uploaded photos to the repository each time a user uploads something?
Will the user-uploaded picture be in 2.0.0?
or the image will be lost 
What should I do with the user-uploaded images if that's the case so they don't get lost in version control?
if($request->hasFile('image1') && $request->hasFile('image2') && $request->hasFile('image3') && $request->hasFile('image4')){
$formFields['media'] =
$request->file('image1')->store('postUploads','public') . ','
. $request->file('image2')->store('postUploads','public') . ','
. $request->file('image3')->store('postUploads','public') . ','
. $request->file('image4')->store('postUploads','public');
}
and did that
php artisan storage:link
l retrieve the image like that
<img src="{{asset('storage/' . $image)}}" class="md:w-48 m-2 rounded" alt="">
is that the right way?
thanks
A better approach
use Illuminate\Support\Facades\Validator;
public function store(Request $request){
$validate = Validator::make($request->all(), [
$request->file('image1') => 'required|mimes:jpg,png,jpeg',
$request->file('image2') => 'required|mimes:jpg,png,jpeg',
$request->file('image3') => 'required|mimes:jpg,png,jpeg',
$request->file('image4') => 'required|mimes:jpg,png,jpeg',
]);
if( $validate->fails() ){
return response($validate->errors(), 400);
}
//anything below here means validation passed. You can then store your images
$path1 = $request->file('image1')->store('profile_pictures','public');
$path2 = $request->file('image2')->store('profile_pictures','public');
$path3 = $request->file('image3')->store('profile_pictures','public');
$path4 = $request->file('image4')->store('profile_pictures','public');
}
Note that this can even be further simplified by saving your images to an array. I did not use that approach as I am not sure whether all the images are profile images or will be used differently.
Your images will be stored in /storage/profile_pictures and Laravel will automatically generate an image name for you.
On your view you can call the images using the asset helper as below
<img src="{{ asset($path1) }}"/>
This is assuming you are sending the image paths individually, which also can be simplified based on your application. Hope this give you an idea.

Laravel 5.8.17 - validation image not working as expected

I'm creating Laravel validation, and meet strange issue.
I have a form with input type file, where I can attach multiple files.
<input type="file" name="img[]" class="form-control" multiple="multiple">
In the Controller I have validation rules:
$validationRules = [
'img' => 'required|image'
];
And all the time my validation doesn't work, I always get a message:
"The img must be an image."
There is no difference when I upload 1 file or 2 or 3 at the same time - error is the same. There are another fields at the same form (text fields) and all is ok with them, problem appears only with image. Tried to remove "image"
from the validation and replace it with mimetypes or mimes - anyway system can't recognize image.
Please advice what could be a problem here, thanks a lot.
You need to make validation attribute img to img.*
$input_data = $request->all();
$validator = Validator::make(
$input_data, [
'img.*' => 'required|mimes:jpg,jpeg,png,bmp|max:20000'
],[
'img.*.required' => 'Please upload an image',
'img.*.mimes' => 'Only jpeg,png and bmp images are allowed',
'img.*.max' => 'Sorry! Maximum allowed size for an image is 20MB',
]
);

LARAVEL How to transfer file from local (public) to FTP using Laravel

I wanna ask a question, anyone know how to transfer data from local / public to ftp, I'm using Storage class
https://laravel.com/docs/5.4/filesystem
Here's my code
$file_local = Storage::disk('local')->get('public/uploads/ftp/file.pdf');
$file_ftp = Storage::disk('ftp')->put('/file.pdf', $file_local');
But my code is not working, when I open the file on ftp, the file is broken, then I open it with notepad, inside file, the content is 'public/uploads/ftp/file.pdf' the content I mean content should on local not what I was wrote,
Anyone know how to transfer file from local to ftp, sorry for my bad English, Thanks for your answer anyway
To get started, you should know the details of your FTP host, FTP username, and FTP password. Once, you are ready with the details open the .env files and add the details as below:
FTP_HOST=YOUR_FTP_HOST_VALUE
FTP_USERNAME=YOUR_FTP_USERNAME_VALUE
FTP_PASSWORD=YOUR_FTP_PASSWORD_VALUE
Make sure to replace the placeholders with the actual values. Next, open the config/filesystems.php file and add the ‘ftp’ configuration to the ‘disks’ array.
<?php
return [
......
'disks' => [
......
'ftp' => [
'driver' => 'ftp',
'host' => env('FTP_HOST'),
'username' => env('FTP_USERNAME'),
'password' => env('FTP_PASSWORD'),
'root' => 'DIR_PATH_TO_WHERE_IMAGE_STORE' // for example: /var/www/html/dev/images
],
],
];
Replace DIR_PATH_TO_WHERE_IMAGE_STORE with the actual path where you need to store images. For example, if we have folder called ‘images’ and path to this folder is /var/www/html/dev/images then this path will go as the value for ‘root’ in the above array.
For uploading files through FTP we need a form where a user can submit the image. Add the below code in your view file.
<form action="{{ url('PASS_ACTION_URL_HERE') }}" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="exampleInputFile">File input</label>
<input type="file" name="profile_image" id="exampleInputFile" multiple />
</div>
{{ csrf_field() }}
<button type="submit" class="btn btn-default">Submit</button>
</form>
You should replace the PASS_ACTION_URL_HERE with your actual route. As we are using a Laravel Storage to file uploading user need add Facade to the controller file as follows:
public function store(Request $request)
{
if($request->hasFile('profile_image')) {
//get filename with extension
$filenamewithextension = $request->file('profile_image')->getClientOriginalName();
//get filename without extension
$filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);
//get file extension
$extension = $request->file('profile_image')->getClientOriginalExtension();
//filename to store
$filenametostore = $filename.'_'.uniqid().'.'.$extension;
//Upload File to external server
Storage::disk('ftp')->put($filenametostore, fopen($request->file('profile_image'), 'r+'));
//Store $filenametostore in the database
}
return redirect('images')->with('status', "Image uploaded successfully.");
}
‘profile_image’ is the name of our file input. We build the unique name of our file and then uploading it to an external server. Notice we have used Storage::disk(‘ftp’) method. This function would store our file to the path as defined in the configuration. A user should store the value ‘$filenametostore’ in your database.
'local' => [
'driver' => 'local',
'root' => storage_path().'/app',
],
If you use the local disk (config/filesystems.php) you are looking in 'storage/app' not the public folder unless you have changed the default settings. So:
$file_local = Storage::disk('local')->get('file.pdf');
will look for 'Storage/App/file.pdf'
Make sure your pdf is in that folder and give this a try:
$file_local = Storage::disk('local')->get('file.pdf');
$file_ftp = Storage::disk('ftp')->put('file.pdf', $file_local);
EDIT: also, I notice in your original post second line of code you have rogue ' after $file_local ;)

Laravel file upload path and view rendering

I successfully upload a file and store its path with the following snippet:
/*Image Handling*/
$file = Input::file('profilePicture');
$destinationPath = public_path().'/images/';
$filename = $file->getClientOriginalName();
Input::file('profilePicture')->move($destinationPath, $filename);
//Profile image
$profileImg = $destinationPath.$filename;
then I store profileImg in database. This is what it looks like:
/Applications/MAMP/htdocs/devproject/public/images/picture.jpeg
Now I want to show this picture in one of the views. So I did this:
<a class="th" href="{{URL::to('/')}}">{{ HTML::image($details->profileImg, "work", array('style' => 'width:100px;height:100px;')) }}</a>
and this is how it is rendered:
<a class="th" href="http://localhost:8888/devproject/index.php"><img src="http://localhost:8888/devproject/Applications/MAMP/htdocs/devproject/public/images/picture.jpeg" style="width:100px;height:100px;" alt="work"></a>
This of course doesn't work because the path is incorrect but it is how it was stored. I need the path to the image to be :
/public/images/picture.jpeg
instead of this:
/Applications/MAMP/htdocs/devproject/public/images/picture.jpeg
as this would fit in the url and show the picture. Any advices on how to achieve this will be appreciated.
Don't save the whole path to the model, save just the filename:
$profileImg = $filename;
Then, instead of using $details->profileImg by itself, use:
asset('images/' . $details->profileImg)
i.e.:
{{ HTML::image('images/' . $details->profileImg, "work", array('style' => 'width:100px;height:100px;')) }}

Cakephp 1.3, Weird behavior on firefox when using $this->Html->link

Greetings,
I am getting a very weird and unpredictable result in firefox when using the following syntax:
$this->Html->link($this->Html->div('p-cpt',$project['Project']['name']) . $this->Html->div('p-img',$this->Html->image('/img/projects/'.$project['Project']['slug'].'/project.thumb.jpg', array('alt'=>$project['Project']['name'],'width'=>100,'height'=>380))),array('controller' => 'projects', 'action' => 'view', $project['Project']['slug']),array('title' => $project['Project']['name'], 'escape' => false),false);
OK I know it is big but bear with me.
The point is to get the following output:
<a href="x" title="x">
<div class="p-ctp">Name</div>
<div class="p-img"><img src="z width="y" height="a" alt="d" /></div>
</a>
I'm not sure if this validates correctly both on cakephp and html but it works everywhere else apart from firefox.
You can actually see the result here: http://www.gnomonconstructions.com/projects/browser
To reproduce the result use the form with different categories and press search. At some point it will happen!!
Although most of the time it renders the way it should, sometimes it produces an invalid output like that:
<div class="p-cpt">
name
</div>
<div class="p-img">
<img src="x" width="x" height="x" alt="x" />
</div>
Looks like it repeats the link inside each element.
To be honest the only reason I used this syntax was because cakephp encourages it.
Any help will be much appreciated :)
I am guessing that the name of some projects is null. According to the documentation, if you pass null as the second argument to the div() method, it will not generate the closing tag (and the resulting markup will be invalid).
The example of invalid markup that you pasted above appear to have come from Firebug rather than Page Source. Use Page Source to view the actual markup sent to the browser. The anchor tag is not repeated.
I rewrote your code to better see what happens. Copy it into one of your views, change 'My Project' to null, and notice how it will affect the $name_div variable:
<div class="p-cpt">My Project</div> will turn into <div class="p-cpt">.
<?php
$project['Project']['name'] = 'My Project';
$project['Project']['slug'] = 'my-project';
$image = $this->Html->image(
'/img/projects/' . $project['Project']['slug'] . '/project.thumb.jpg',
array(
'alt' => $project['Project']['name'],
'width' => 100,
'height' => 380
)
);
$name_div = $this->Html->div('p-cpt', $project['Project']['name']);
$image_div = $this->Html->div('p-img', $image);
$link = $this->Html->link(
$name_div . $image_div,
array(
'controller' => 'projects',
'action' => 'view',
$project['Project']['slug']
),
array(
'title' => $project['Project']['name'],
'escape' => false
)
);
var_dump($image);
echo 'Notice what happens below if project name is null.';
var_dump($name_div);
var_dump($image_div);
var_dump($link);
echo $link;

Resources