How can I upload image to different project on the laravel? - php

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

Related

tinymce - change the url path of the uploaded images

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'])

laravel - saving and accessing an uploaded file

I am studying some laravel code that I downloaded and I am getting some problem.
This supposed to be the functions to save,delete and download the files but the problem is.
The files are being saved in a folder named with a number on "storage\app\public\project-files\" (i.e. storage\app\public\project-files\11), both destroy and download methods are referencing different paths, I tried to change but didn't worked, download show FileNotFoundException and destroy just remove from the database but not from the folder
So is this code wrong? How It supposed to be?
I've read about using artisan:link but seems odd to me run this command every time I want upload a file to make a link
PS. I cheched the routes, so the methods are being called
Thanks
public function store(Request $request)
{
if ($request->hasFile('file')) {
$file = new ProjectFile();
$file->user_id = $this->user->id;
$file->project_id = $request->project_id;
$request->file->store('public/project-files/'.$request->project_id);
$file->filename = $request->file->getClientOriginalName();
$file->hashname = $request->file->hashName();
$file->size = $request->file->getSize();
$file->save();
$this->project = Project::find($request->project_id);
return view('project-files');
}
public function destroy($id)
{
$file = ProjectFile::find($id);
File::delete('storage/project-files/'.$file->project_id.'/'.$file->hashname);
ProjectFile::destroy($id);
$this->project = Project::find($file->project_id);
return view('project-files');
}
public function download($id) {
$file = ProjectFile::find($id);
return response()->download('storage/project-files/'.$file->project_id.'/'.$file->hashname);
}
You are storing files in storage so i assume you have uploaded image in the following path
project\storage\app\public\project-files
if this is the path then you can delete using
Storage::delete('public/project-files/1.JPG');
for Downlaoding file
$path= storage_path('app/public/project-files/3.JPG');
return response()->download($path);

How to Delete Images from Public/Images Folder in laravel 5 (URL Data)

how to delete images file from public/images folder in laravel 5 ??
i found some example from this site, but i know they are just using the file name in their record table, but i'm using something like URL e.g localhost/project/uploads/filename.jpg on my record table. so if i tried like this :
$image_path = $data->image; // the value is : localhost/project/image/filename.format
if(File::exists($image_path)) {
File::delete($image_path);
}
the file is not deleted
help pls, thanks
If you want to delete image from your server, you have to reference location of file in directory server, means you could not reference by url link to delete it.
Commonly, Laravel 5 file is locate in public folder.
Example: your files are located in public/images
$image_path = "/images/filename.ext"; // Value is not URL but directory file path
if(File::exists($image_path)) {
File::delete($image_path);
}
If I can delete image from server by reference URL then Google is the first target :)
You can use the normal PHP delete file keyword ( #unlink )
$image_path = "the name of your image path here/".$request->Image;
if (file_exists($image_path)) {
#unlink($image_path);
}
This is what I do to delete an image:
public function SliderDelete(String $slider_id)
{
$slider = Slider::findOrFail($slider_id);
$image_path = public_path("\storage\images\sliders\\") .$slider->photo;
if(File::exists($image_path)) {
File::delete($image_path);
}
else{
$slider->delete();
//abort(404);
}
$slider->delete();
return response()->json(['success'=>'Slider deleted successfully!']);
}
in laravel 8 you do it like
// import Storage class
use Illuminate\Support\Facades\Storage;
Storage::disk('public')->delete('path-of-file');
you can use disk of your choice like
Storage::disk('s3')->delete('path-of-file');
$filename = public_path($fileloc);
if(File::exists($filename)) {
File::delete($filename);
}
call this function and pass two parameter
$filepath = path where your file exist
$filename = name of your file
public static function UnlinkImage($filepath,$fileName)
{
$old_image = $filepath.$fileName;
if (file_exists($old_image)) {
#unlink($old_image);
}
}
public function destroy($id)
{
$imagePath = YourModelName::select('image')->where('id', $id)->first();
$filePath = $imagePath->image;
if (file_exists($filePath)) {
unlink($filePath);
YourModelName::where('id', $id)->delete();
}else{
YourModelName::where('id', $id)->delete();
}
}
To be able to delete the image it should be in the following form :
"/uploads/img1.jpg" where uploads directory is in the public directory then use the following code:
$image_path = puplic_path("/uploads/img1.jpg")
if (file_exists($image_path)) {
File::delete($image_path);
}
Here is the correct and easy way .
if (file_exists(public_path() . '/storage/gallery/images/'. $item->image)) {
unlink(public_path() . '/storage/gallery/images/'. $item->image);
}

Laravel storing temporary filename in database

The upload script is working, the file also gets saved by the correct/desired name. However, while storing data in database, it stores .tmp filename instead
controller code:
public function store(CreateChapterRequest $request)
{
if($request['chapter_content_type']==6) {
//upload file
$record_save = $this->processFile($request->file('chapter_document'));
$request['chapter_document']=$record_save;
}
Chapter::create($request->all());
}
protected function processFile($requestData)
{
$input['chapter_document'] = time().'.'.$requestData->getClientOriginalExtension();
$destinationPath = public_path('uploads/chapters/');
$requestData->move($destinationPath, $input['chapter_document']);
return $input['chapter_document'];
}
It's storing file name as C:\project\xampp\tmp\phpD837.tmp. What's wrong?
It works with $record=new Chapter(); $record->save($request->all()); but not with ::create()
Don't know why!

How to display image from Storage(Laravel 5.2)

I try to display images which are uploaded in my storage/app directory.I successfully can upload images in storage but when i try to retrieve it shows a broken image.
To retrieve, my function is :
public function getUserImage($filename)
{
$file = Storage::disk('local')->get($filename);
return new Response($file, 200);
}
My view:
<img src="{{route('image.show',['filename'=>'123.jpg'])}}"/>
When i try to display the image , it shows status 200 (Failed to load response data) but image is not being displayed.Just a broken image is shown.
I have no idea what i am missing .Could you give me any advice ?
Thanks in advance
If you want send the image to view
In your controller:
public function index()
{
// get the filename from somewhere
$filename = getFilename();
$file = Storage::disk('local')->get($filename);
return View::make('your_view')
->with('file', $file);
}
In your view
<img src="/path_where_is_the_file/{{$file}}" />
Hope it helps!
Send filename to view and Use src like this
src='/path_to_file/filename.jpg'

Categories