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
Related
"I'm tring to upload file using custome library in laravel file goes in folder successfully but file isn't upload in database
this is my custom library:-
namespace App\Classes;
use Illuminate\Http\Request;
class Hello
{
static function jai(Request $request)
{
if($request->hasfile('name'))
{
$image=$request->file('name');
$new_image=time().'.'.$image->getClientOriginalName();
$image->move(public_path('image/'),$new_image);
}
}
}
?>
and this is my controller store function :-
Hello::jai($request);
$x=$request->all();
$x['name']=
$j=Hello::jai($request)->new_image;
Cruds::create($x);
The file "[000004].jpg" was not uploaded due to an unknown error.
Hope this will help.
public function upload(Request $request){
if($file = $request->file('file')){
$name = $file->getClientOriginalName();
if($file->move('files',$name)){
$file= new Files();//DB instance modal
$file->url= $name;
$file->save();
return "success";
}
}
You forgot the file extension. See if this help you.
$image = $request->file('your_input_form_file_name');
$image = time() . '_' . $image->getClientOriginalName() . '.' . $file->getClientOriginalExtension();
$image->move(public_path('images/'), $name);
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.
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 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.