I have a function to upload files. on Localhost it's not having an error. but after I deploy on shared hosting, its have a problem. If localhost, I'm not moving some folder, but on "tutorial" shared hosting, I need to make 2 folders. Laravel and public. this Laravel folder is all file on project Laravel without public
It's my schema on my shared hosting
(/home/sippausr)
etc
Laravel ->
app
bootstrap
config
database
public_html
files ( this file saved here)
resources
routes
storage
tests
vendor
logs
mail
public_ftp
public_html ->
css
files (not on here, i need to save here)
home
images
js
kalibrasi
public
sop
theme
And I have a function to upload a file and saved this file to directory files on public_html 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');
}
But, this file not saved at public_html/files,
This file saved in the Laravel folder, on public_html( you can see at schema). Can someone help me?
Use the below code to save your file to your files directory in your public folder.
use Illuminate\Support\Facades\Storage;
class YourClassName implements Contract {
public function store6(Request $request) {
$this->validate($request, [
]);
if($request->hasfile('image')) {
$file = $request->file('image');
$name=$file->getClientOriginalName();
Storage::putFileAs('public/files', $file, $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();
return redirect('user/show6')->with('success', 'Data Telah Terinput');
}
}
Related
I'm generating PDF documents on the fly using php-pdftk and saving it to the public directory. Works great locally, but on a production Digital Ocean server, the file won't save to the public directory. In my code below at the ->saveAs..., the 'docs/reg-forms/' are directories within public.
...
$pdf = new Pdf('/docs/ax_reg_form.pdf');
$result = $pdf->fillForm([
'email' => $this->usercar->user->email,
'date' => $this->usercar->created_at,
...
])
->needAppearances()
->saveAs('docs/reg-forms/'.$this->usercar->user->id.'_'.strtolower($this->usercar->user->first_name).'_'.strtolower($this->usercar->user->last_name).'_'.'car_class.pdf');
session()->flash('url', '/docs/reg-forms/'.$this->usercar->user->id.'_'.strtolower($this->usercar->user->first_name).'_'.strtolower($this->usercar->user->last_name).'_'.'car_class.pdf');
return redirect()->route('usercar.show', $this->usercar->id);
try to wrap with public_path helper
public_path('docs/reg-forms/');
and check if the path exists
if (!file_exists(public_path('docs/reg-forms/'))) {
mkdir(public_path('docs/reg-forms/'), 0777, true);
}
posting this again because I didn't find to solution yet.
Laravel can't found the file storage/app/public/upload
when I usehttp://127.0.0.1:8000/storage/upload/The_fileName.x I get 404 not found
I've tried http://127.0.0.1:8000/storage/app/public/upload/The_fileName.x too.
what should I do ?
In DocumentController :
public function store(Request $request)
{
$request->validate([
'doc' => "required",...
]);
$document = new document();
$file = $request->file('doc');
$filename=time().'.'.$file->getClientOriginalExtension() ;
// I've tried these too, one by one and still get the same error .
//$file_path = public_path('public/upload');
//$file->move($file_path, $filename);
//Storage::disk('local')->put($file, $filename);
//request('doc')->store('upload', 'public');
$file->storeAs('public/upload', $filename);
$document->doc = $request->input('doc', $filename);
$document->candidate_id = $candidate_id;
$document->save();
Thank you in advance
According to Laravel document File Storage, you need to create a symbolic link at public/storage which points to the storage/app/public then you can access the file with http://127.0.0.1:8000/upload/The_fileName.x.
I'm working on a Laravel project, and I make a feature that the user can upload image for its profile, and I use the public path to save the avatars cause 000webhost doesn't support (storage:link) and every thing works better in local
but when I upload the project on 000webhost, it refuses to upload the image and returns error (The image failed to upload.)
How can i solve it
config/filesystem.php
'public' => [
'driver' => 'local',
'root' => public_path('storage2'),
'url' => env('APP_URL').'/storage2',
'visibility' => 'public',
],
controller
public function Change(Request $request)
{
$Validate = $this->Validation($request);
if (!$Validate->fails()) {
$User = $this->getUser();
$OldImage = $User->image;
$Extension = $request->file("image")->getClientOriginalExtension();
$NewImage = str_random(30) . "." . $Extension;
Storage::putFileAs($this->privatePath, $request->file("image"), $NewImage);
$User->update(["image" => $NewImage]);
Storage::delete($this->privatePath ."/". $OldImage);
session()->flash("message", "You have changed your image");
return back();
} else {
session()->flash("error", $Validate->errors()->first());
return back();
}
}
But the problem is not in the code, cause it works in local
I think the problem with some permissions or in file .htaccess or anything like that
We are using laravel mediable for joining image to model in our web, Code is given below. We have default images in a folder and use Laravel package Intervention for uploading.
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
$id = $user->id;
$uname = $user->name[0];
$img = Image::make('uploads/users/default/'.$uname.'.png');
$img->save('uploads/users/images/'.$id.'.png');
$media = MediaUploader::import('uploads', 'users/images', $id, 'png');
$user->attachMedia($media, 'user');
$img = Image::make('uploads/users/default/'.'badge1'.'.png');
$img->save('uploads/users/images/'.$id.'badge'.'.png');
$media = MediaUploader::import('uploads', 'users/images', $id.'badge', 'png');
$user->attachMedia($media, 'badge');
}
But on cpanel path error occurs, Intervention uploads the file on exact location but MediaUploader of Laravel Mediable unable to find file from path to attach it to the user. Can someone help? i hope i have defined the issue correct.
Facing some problem when uploading image using Intervention service provider on laravel 4.2
error- Can't write image data to path.
-searched on google and stackoverflow and tried to solve but the problem is still unsolved. may that directory has not in writable mod. how can I make the diretory 'public/img/products/' writable mod in windows 7 using git bash and Cygwin Terminal too
my product controller create method -
public function postCreate() {
$validator = Validator::make(Input::all(), Product::$rules);
if ($validator->passes()) {
$product = new Product;
$product->category_id = Input::get('category_id');
$product->title = Input::get('title');
$product->description = Input::get('description');
$product->price = Input::get('price');
$image = Input::file('image');
$filename = date('Y-m-d-H:i:s')."-".$image->getClientOriginalName();
$path = public_path('img/products/' . $filename);
Image::make($image->getRealPath())->resize(468, 249)->save($path);
$product->image = 'img/products/';
$product->save();
return Redirect::to('admin/products/index')
->with('message', 'Product Created');
}
return Redirect::to('admin/products/index')
->with('message', 'Something went wrong')
->withErrors($validator)
->withInput();
}
Though this is an old thread.
Windows file name must not have ":"(colon) as a character. Instead use "-"(dash) or "_"(underscore). Even a space(" ") will be fine.
Example:
$filename = date('Y-m-d-H_i_s')."-".$image->getClientOriginalName();
or
$filename = date('Y-m-d-H i s')."-".$image->getClientOriginalName();
You should change the access permissions of directory.
sudo chmod -R 777 public/img/products