I'm doing a Admin Panel in Laravel 5.6 and I have a problem where productController --resource update.
My store as this:
if($request->hasfile('filename'))
{
$file = $request->file('filename');
$name=$file->getClientOriginalName();
$image_resize = Image::make($file->getRealPath());
$image_resize->resize(590, 590);
$image_resize->save(public_path('/images/urunler/' .$name));
}
$urun = new Urun();
$urun->k_id=$request->get('k_id');
$urun->ad=$request->get('ad');
$urun->form=$request->get('form');
$urun->icerik=$request->get('icerik');
$urun->ticari_sekli=$request->get('ticari_sekli');
$urun->filename=$name;
$urun->save();
return redirect('/urunler')->with('success','Ürün Başarı İle Eklendi.');
and My Update as this:
$urun= Urun::findOrFail($id);
$urun->update($request->all());
if ($request->hasFile('filename'))
{
$file = $request->file('filename');
$name=$file->getClientOriginalName();
$img = Image::make($file->getRealPath());
$img->resize(590, 590);
$img = Image::make($file->getRealPath())->resize(590, 590)->insert(public_path('/images/urunler/' .$name));
$img->save(public_path('/images/urunler/' .$name));
$urun->image = $name;
}
$urun->save();
return redirect('/urunler')->with('success','Ürün Başarı İle Güncellendi.');
Store part is working but Update part save a filename database but image doesn't move in images/urunler.
My php version is 7.2.4 and I'm using Laravel 5.6.
Delete these codes.
$img = Image::make($file->getRealPath())->resize(590, 590)->insert(public_path('/images/urunler/' .$name));
$img->save(public_path('/images/urunler/' .$name));
After that. Add these codes.
$location = public_path('/images/urunler/'.$name);
Image::make($file->getRealPath())->resize(590, 590)->save($location);
Give it a try.
Solution: Change related codes like that.
$location = public_path('/images/urunler/'.$name);
Image::make($file->getRealPath())->resize(590, 590)->save($location);
$urun->filename= $name;
Then add
$this->validate(request(),[
'k_id' => 'required',
'ad' => 'required',
'form' => 'required',
'icerik' => 'required',
'ticari_sekli' => 'required',
'filename'=>'required' ]);
PS: filename must be just required. Not required|image. It isn't an image. Its just name.
After that request filename. Like that: $urun->filename= $name;
don't $urun->image= $name;. Because there is no $urun->image.
This works for me:
if ($request->file('filename')) {
$file = $request->file('filename');
$destinationPath = $_SERVER['DOCUMENT_ROOT'] . '/images/urunler/';
$file->move($destinationPath, 'FILENAME.' . $file->getClientOriginalExtension());
}
Just edit FILENAME to the name of the file you want or replace FILENAME with $file->getClientOriginalName();
The file wil be saved in the public/images/urunler/ folder
EDIT:
Last thing i can think of would be this: in the form open tag place enctype="multipart/form-data". What this says is that your form exists of normal data AND possible files. There might still be files are not allowed so also place files="true"in the form open.
Related
I use below script to uploade a file to the sevre. it works fine on the local host (wamp server), but when I try to use it on the server I figure out that the uploaded file size is 0 byte.
Any one know taht where is the problem?
public function uploadFile(Request $request)
{
$request->validate([
'name' => 'required',
'auther_id'=>'required',
'doc_type'=>'required',
'file'=>'required|mimes:pdf'
]);
$fileModel = new File;
$fileName = time().'_'.$request->file->getClientOriginalName();
$filePath = $request->file('file')->storeAs('uploads', $fileName, 'public');
$fileModel->name = time().'_'.$request->file->getClientOriginalName();
$fileModel->file_path = '/storage/' . $filePath;
$fileModel->save();
}
I think you can use like this. first of all you need to store your file inside storage. then you can get public url from storage.
Try this way
$fileName = time().'_'.$request->file->getClientOriginalName();
Storage::put('/public/uploads/'.$fileName,$request->file('file'));
$url = Storage::url('public/uploads/'.$fileName);
$fileModel = new File;
$fileModel->name = time().'_'.$request->file->getClientOriginalName();
$fileModel->file_path = $url;
$fileModel->save();
Reason of this error was permissions of some folders. I just reset the host completely and now everything work like a charm.
Well, what happens is that I want to upload an image to Cloudinary through Laravel,
I Follow the steps as it says on the documentation:
https://github.com/cloudinary-labs/cloudinary-laravel
and here : https://cloudinary.com/blog/laravel_file_upload_to_a_local_server_or_to_the_cloud
I am using Laravel 8, here is my code:
public function store(Request $request){
$request->validate([
'name' => 'required|string|unique:festivals|max:255', //unique:table
'description' => 'required|string|max:255',
'image' => 'required|image|dimensions:min_width=200,min_height=200',
], self::$messages);
//Here I create an instance and upload file to server
$festival = new Festival($request->all());
$path = cloudinary()->upload($request->file('image')->getRealPath())->getSecurePath();
dd($path);
//Then at field image of festivals que save the path and that goes to the database
$festival->image = 'festivals/' . basename($path);
$festival->save();
return response() -> json($festival, 201); //code 201 created
}
When I try to create a new record through Postman, this happens:
Error after register new festival
However, the image was uploaded to Cloudinary:
image uploaded
Then I tried to check if the record was created, but it was not created.
What can I do, does anyone know?
Thanks
Ok, I Solved :)
//Here I create an instance and upload file to server
$festival = new Festival($request->all());
$result = $request->image->storeOnCloudinary();
//Here I get the url
$path = $result->getPath();
//Then at field image of festivals que save the path and that goes to the database
//$festival->image = $path; output: "https://res.cloudinary.com/test/image/upload/v1226931211/zuiyb17vfs8dre6gvuov.jpg"
//$festival->image = dirname($path); output: "https://res.cloudinary.com/test/image/upload/v1526711711"
//$festival->image = basename($path); output: "zuiy31lvfs4dre5gvuov.jpg"
$base = basename(dirname($path).basename($path)); //output: "v1636941221zuiyb14vfsmdre6gvuov.jpg"
$folder = substr($base, 0, -24); //output: "v1626915261"
//So:
$img_path = $folder.'/'.basename($path);
$festival->image = 'festivals/'.$img_path; //imageFolder/imageName.jpg;
But if someone has a best way, please tell me
when uploading a file from html page and need to get the file extension. But the uses function showing an error. What are procedure to solve that issue?
public function downloadAttendance(Request $request)
{
$this->validate($request, [
'attendance_date' => 'required',
'attendance_file' => 'required',
]);
$date = $request->attendance_date ? database_formatted_date($request->attendance_date) : null;
$file = $request->attendance_file;
$file_ext = $file->getClientOriginalExtension();
dd($file_ext);
$file_path = $file->getRealPath();
}
It should be
$file = $request->file('attendance_file');
$file_ext = $file->getClientOriginalExtension();
You are getting string when using
$request->file
You can get file by using this method,
$file = $request->file('file_key');
refer this link for more information.
https://laravel.com/docs/6.x/filesystem#file-uploads
I make input data with an upload image, my code
$name = $request->input('name');
$images = $request->file('image');
$name_img = $images->getClientOriginalName();
$ext = $images->getClientOriginalExtension();
// Move Uploaded File
$destinationPath = 'img/';
$images->move($destinationPath,$name_img);
$path = $destinationPath.'/'.$name_img;
then put them in array, then insert in database
$data = array(
'name' => $name,
'image' => $path, );
DB::table('daftar')->insert($data);
return json_encode(array('status' =>true));
That code works in Laravel 5.1, but it doesnt work in Laravel 5.2.
Anything wrong with that code for Laravel 5.2?
Thank You. :)
I am using this approach and it work:
//Use should have use App\Daftar in above your class;
//Your method inputs should be like UploadRequest $request , Daftar $daftar
$file=$request->file('image');
$name=time().'-'.$file->getClientOriginalName();
$path = public_path().'/img';
$file->move($path, $name);
$daftar->create(array('name' => $name, 'image' => $path));
Ensure that in the table was not a field that can not be empty
you cal learn more about public_path() here:
https://laravel.com/docs/5.5/helpers#method-public-path
Also my Upload folder is on public directory
if you want to ensure that data saved in the database is the same as you send check with following approach:
//Use should have use App\Daftar in above your class;
//Your method inputs should be like UploadRequest $request , Daftar $daftar
$file=$request->file('image');
$name=time().'-'.$file->getClientOriginalName();
$path = public_path().'/img';
$file->move($path, $name);
print 'name is'.$name.' and image is '.$path;
$daftar->create(array('name' => $name, 'image' => $path));
And after that check the data that saved in the database both data should be same.
I have a site where there is member registration. All data will be saved but I get only this error:
Could not move the file "/tmp/phpa4pH3I" to "/home/sporter/public_html/someonect.org/uploads\membersImages/33921.jpg" ()
My code is like this
public function store(){
// dd(Input::all());
$validator = Validator::make(Input::all(), Members::$rules);
if($validator->passes()):
$first_name = Input::get('mem_firstName');
$last_name = Input::get('mem_lastName');
$email = Input::get('mem_email');
$image = Input::get('mem_image');
$phone = Input::get('mem_phone');
$occupation = Input::get('mem_occupation');
$citizen = Input::get('mem_citizen');
$address = Input::get('mem_address');
$destinationPathImage = str_replace('PROJECT\\', '', base_path().'\uploads\membersImages\\') ;
//Generating a random name
$randomName = rand(11111,99999);
//Renaming the image
$ImageName = $randomName.'.'.'jpg'; // renameing image
$imagePath = $destinationPathImage.$ImageName;
Members::create([
'first_name'=>$first_name,
'last_name'=>$last_name,
'email'=>$email,
'image'=>$imagePath,
'phone'=>$phone,
'occupation' => $occupation,
'citizen' => $citizen,
'address'=>$address
]);
...
I don't understrand clearly your str_replace part, but if you setup your url in config properly, you don't need this.
I'm using this kind of upload for an image:
if($request->hasFile('mem_image')) {
$file = $request->file('mem_image');
$ext = $request->file('mem_image')->getClientOriginalExtension();
$filename = rand(11111,99999). '.' . $ext;
$file->move('./uploads/', $filename);
$member->mem_image = '/uploads/' . $filename;
$member->save();
}
In case, your uploads directory is in your public folder.
If your script have permission for writing in target folder. The problem can be only in path. Check what contain in variable $destinationPathImage. It must be correct and exists path. In your error message I see problem with slashes.
also think that it's be cool to check exists of destination folder and file exists before save to database record with path of file.
You're not using the proper slashes.
Replace \uploads\membersImages\\ with /uploads/membersImages/.
If it still not working, please double check the permissions in that folder using the is_writable php function. However, have in mind that the is_writable function is not working properly on windows platforms.