I deploy my website, and I have a bug on my upload image, this image can't saved at directory
my directory is in public_html on folder named files.
on localhost its not having a problem but after I deploy this, I have it.
my Controller on my localhost (not have a problem like this)
public function store6(Request $request)
{
$this->validate($request, [
]);
if ($request->hasfile('image'))
{
$file = $request->file('image');
$name=$file->getClientOriginalName();
$file->move(public_path().'/files/', $name);
$data = $name;
}
$user = new pemeliharaan;
$id = Auth::user()->id;
$user->user_id = $id;
$user->alat_id = $request->alat_id;
$user->pertanyaan =json_encode($request->except
(['_token','name','alat_id','status','catatan','image']));
$user->catatan = $request->catatan;
$user->image=$data;
$user->status = $request->status;
$user->save();
// dd($user);
return redirect('user/show6')->with('success', 'Data Telah Terinput');
}
this image "name" saved on DB but this file not saved, cant someone corrects my code?
If your files folder is in the root path of your project and not in the public folder of your Laravel installation, you would need to use the base_path helper instead of the public_path helper to move your file.
$file->move(base_path('files'), $name);
Try this code
if ($request->hasfile('image')) {
$file = $request->file('image');
$name = $file->getClientOriginalName();
$imagePath = $file->storeAs('/files/' . $name . '', 'public');
}
and don't forget to run php artisan storage:link if not linked.
Replace your code
public function store6(Request $request)
{
$this->validate($request, [
//your validation code
]);
if ($request->image != ''){
$path = 'files/';
$file = $request->file('image');
$name=$file->getClientOriginalName();
$file->move($path, $name);
$data = $name;
}
$user = new pemeliharaan;
$id = Auth::user()->id;
$user->user_id = $id;
$user->alat_id = $request->alat_id;
$user->pertanyaan =json_encode($request->except
(['_token','name','alat_id','status','catatan','image']));
$user->catatan = $request->catatan;
$user->image=$data;
$user->status = $request->status;
$user->save();
// dd($user);
return redirect('user/show6')->with('success', 'Data Telah Terinput');
}
Try to run this Symbolic link first
php artisan storage:link
Try to upload now.
if not work,
then check any symbolic storage folder present in public folder
If Yes, then delete it (Take backup if required before deleting it).
Now run again
php artisan storage:link
Now try again to upload.
Its worked for me.
Related
I upload my project in the host. I uploaded an image, and the image save in database. And the image did not show well, but it show in my localhost as well.
web.php
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
Auth::routes();
Route::get('/link', function(){
return Artisan::call('storage:link');
});
Controller
public function update(Request $request)
{
$user = auth()->user();
$path = $user->image ?? null;
if ($request->hasFile('image'))
{
$file = $request->file('image');
$nane = time();
$extension = $file->getClientOriginalExtension();
$fileName = $nane . '.' . $extension;
$path = $file->storeAs('images/users', $fileName, 'public');
}
$user->image = $path;
}
The image save in database.
The image uploaded in storage/app/public/images/users folder.
The image did not uploaded public/images/users.
Do you have an access to server with ssh? If not you can write like this in your route :
Route::get('/foo', function () {
Artisan::call('storage:link');
});
It will generate the symlink to your public_html, sorry for my bad english
hi i'm try to delete image from folder but i'm getting this error message
Class 'App\Http\Controllers\File' not found
when i dd() the image path it look like this : "C:\xampp\htdocs\blog\public\/images/1544525527.jpg"
here is my delete code:
$post = Post::find($id);
$file= $post->image;
$destinationPath = public_path('/images');
$filename = $destinationPath.'/'.$file;
File::delete($filename);
and i upload image like this:
if ($request->hasFile('image')) {
$image = $request->file('image');
$name = time().'.'.$image->getClientOriginalExtension();
$destinationPath = public_path('/images');
$image->move($destinationPath, $name);
$post->image = url('/public/images/').'/'.$name;
}
Try importing the class at the top of your controller file:
use Illuminate\Support\Facades\File;
there are two solutions for this, first you can import the class by add this code in controller file
use Illuminate\Support\Facades\File;
or alternatively you can use the full path class in your code, so your code should be like this,
$post = Post::find($id);
$file= $post->image;
$destinationPath = public_path('/images');
$filename = $destinationPath.'/'.$file;
//fullpath class
\Illuminate\Support\Facades\File::delete($filename);
direct delete from your directory please try this will use in your controller
use File;
that also use in your controllers function
$update = tablename::where('id',$request->input('uid'))->where('status',1)->first();
if ($request->hasFile('cover'))
{
File::delete(public_path().$update->pu_cover_photo);
}
I have a project hosted on a shared server in Blue Host and in the store and update method of the AdminPostController I have a function to save the images in the public_path, but it generates the following error when trying to save it:
Intervention\Image\Exception\NotWritableException
Can't write image data to path (/home4/directoryname/public/directoryname/uploads/posts/1516313973.jpg)
This is my store method:
public function store(Request $request)
{
$posts = new Post();
$posts->title = $request->input('title');
$posts->slug = str_slug($request->get('title'));
$posts->content = $request->input('content');
$posts->category_id = $request->input('category_id');
$posts->user_id = $request->input('user_id');
$posts->blockquote = $request->input('blockquote');
$posts->blockquote_sign = $request->input('blockquote_sign');
$posts->save();
// Handle the client upload image id avatar
if($request->hasFile('avatar')){
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(500, 300)->save( public_path('uploads/posts/' . $filename ) );
$posts->avatar = $filename;
$posts->save();
}
return redirect()->route('contenido.index')
->with('success','PublicaciĆ³n creada correctamente!!!');
}
This is my update method:
public function update(Request $request, $id)
{
$updated = Post::findorFail($id);
$post = $request->all();
$updated->fill($post)->save();
if($request->hasFile('avatar')){
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(500, 300)->save( public_path('uploads/posts/' . $filename ) );
$updated->avatar = $filename;
$updated->save();
}
return redirect()->route('contenido.index')
->with('success','PublicaciĆ³n actualizada correctamente!!!');
}
This is my route resource for these methods:
Route::resource('contenido', 'Admin\AdminPostController');
I was reading different places and at the moment I have not found a solution, can someone guide me?.
This is how the files on the server are structured.
Laravel 5 by default uses the public folder as the home directory while my server uses another. Therefore, I set a alternative home directory.
I just had to perform the following actions:
1 - Edit index.php located in the public folder of your Laravel 5 project.
2 - Find the following line:
$app = require_once __DIR__.'/../bootstrap/app.php';
3 - Below the that line add the following lines:
$app->bind('path.public', function() {
return __DIR__;
});
4 - Now re-name the public directory of your project to public_html.
The above edit to your index.php file will direct your Laravel project to use the immediate parent directory of this file as the public directory for your project.
You can find more in this publication:
https://laracasts.com/discuss/channels/general-discussion/where-do-you-set-public-directory-laravel-5
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);
I want to upload a file in Laravel:
I put this code in route
Route::post('upload', 'myconttest#test')->name('upload');
And put this code in controller
function test(Request $request){
$file=$request->file('myfile');
$filename=$file->getClinetOriginalName();
// $projectname;
$path='files/';
$file->move($path,$filename);
}
I create a folder in public called files. The code runs without error but the file is not saved.
Its a working code please see this
public function store(Request $request)
{
$checkval = implode(',', $request->category);
$input = $request->all();
$input['category'] = $checkval;
if ($request->hasFile('userpic')) {
$userpic = $input['pic'];
$file_path = public_path("avatars/$userpic");
if(File::exists($file_path)) {
File::delete($file_path);
}
$fileName = time().$request->userpic->getClientOriginalName();
$request->userpic->move(public_path('avatars'), $fileName);
$input['userpic'] = $fileName;
Product::create($input);
return redirect()->route('productCRUD.index')->with('success','Product created successfully');
}
}
Make sure your form is enabled to upload the file. So check that the following attribute is set or not.
<form action"" 'files' => true>
Use the following namespace
use Illuminate\Support\Facades\Input;
Then try this:
$file = Input::file('myfile');
$filename = $file->getClinetOriginalName();
move_uploaded_file($file->getPathName(), 'your_target_path/'.$filename);
I think you can try this:
First you give folder permission to files folder in public folder
function test(Request $request){
$file=$request->file('myfile');
$filename=$file->getClinetOriginalName();
$file->move(public_path().'/files/', $filename);
}
Hope this work for you !!!
In controller test function you need to given public path of files folder like
$path = public_path('files/');
for moving file on given public folder path.