Image upload on laravel doesn't work online : Laravel - php

The project works fine on my localhost but has issues on a live shared server.
I have tried adding this code to my index.php
// set the public path to this directory
$app->bind('path.public', function() {
return __DIR__;
});
I have tried adding this code in a new sym.php file in my public folder
<?php
$targetFolder = $_SERVER['DOCUMENT_ROOT'].'/storage/app/public';
$linkFolder = $_SERVER['DOCUMENT_ROOT'].'/public/storage';
symlink($targetFolder,$linkFolder);
echo 'Symlink process successfully completed';
?>
I have tried adding this on my web.php and then running site/linkstorage
Route::get('/linkstorage', function () {
Artisan::call('storage:link');
});
None of these solutions works
here is a snippet of my Controllers code:
public function storeBrand(Request $request){
$this->validate($request, ['brand_name'=> 'required',
'brand_url'=> 'required',
'brand_image'=>'image|nullable|max:1999']);
if($request->hasFile('brand_image')){
//1 : get filename with ext
$fileNameWithExt = $request->file('brand_image')->getClientOriginalName();
//2 : get just file name
$fileName = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
//3 : get just extension
$extension = $request->file('brand_image')->getClientOriginalExtension();
//4 : file name to store
$fileNameToStore = $fileName.'_'.time().'.'.$extension;
//upload image
$path =$request->file('brand_image')->storeAs('public/BrandImages', $fileNameToStore);
}
else{
$fileNameToStore ='noimage.jpg';
}
$brand=new Brand();
$brand->brand_name =$request->input('brand_name');
$brand->brand_url =$request->input('brand_url');
$brand->brand_image =$fileNameToStore;
$brand->save();
return redirect('/create_brand')->with('status', 'The '.$brand->brand_name.' Brand has been saved successfully. Create another one.');
Note
When an image is uploaded the path can be traced, but the image is not found, returns an empty image.
Thank you for your time and assistance.

I have got the solution:
I Wrote down this code in my web.php route file:
Route::get('/linkstorage', function () { $targetFolder = base_path().'/storage/app/public'; $linkFolder = $_SERVER['DOCUMENT_ROOT'].'/storage'; symlink($targetFolder, $linkFolder); });
After that I navigated to my url/linkstorage
It worked!!

Looking at the code in your controller, it seems correct. Perhaps the error is within your form in the Blade file associated with this method. Based on experience I tend to forget to write this and this could probably sort out your error.
Write this on the form tag.
<form action="{{ insert the route here }}" method="POST" enctype="multipart/form-data">
// insert form input fields here...
</form>

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

How to upload a file (Laravel)

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.

Laravel 5.4 upload original file name and extention

how can i upload original filename (file.jpg) to database when submitting file via form. Controller:
public function addCv(Request $request){
$cv = Cv::create($request->all());
$file = $request->file_name;
$filename = $file->getClientOriginalName();
Storage::putFileAs('public/uploads', $file, $filename);
return redirect()->back();
}
at the moment, this function uploads a path like this C:\xampp\tmp\php18DD.tmp.
Instead of that i want just filename and extension (file.extension).
Storage is working fine - storing with original name.
You could try
$file = $request->image->getClientOriginalName(); //Get Image Name
$extension = $request->image->getClientOriginalExtension(); //Get Image Extension
$fileName = $file.'.'.$extension; //Concatenate both to get FileName (eg: file.jpg)
I would suggest adding enctype="multipart/form-data" in the form tag in the view, from where you are uploading the file:
<form enctype="multipart/form-data">
You can use like below,
$this->getRequest()->files['name_of_file_field_in_post']->getClientOriginalName();
Reference: Get Uploaded File's Original Name

Call to a member function on a non-object getting errors

I tried so many time but this code is not working. I don't know why. It is a image upload form. This code worked for another form but here it's getting an error: Call to a member function isValid() on a non-object
$file = array('dest_img' => Input::file('dest_img'));
// checking file is valid.
if (Input::file('dest_img')->isValid()) {
$destinationPath = 'uploads'; // upload path
$extension = Input::file('dest_img')->getClientOriginalExtension(); // getting image extension
$fileName = $s.'.'.$extension;
$imgPath= $destinationPath.'/'.$fileName;
//return $imgPath;
// renameing image
Input::file('dest_img')->move($destinationPath, $fileName); // uploading file to given path
// sending back with message
//Session::flash('success', 'Upload successfully');
//return Redirect::to('tblaze_admin/bannerAdd');
$data=array(
'dest_title' =>$input['dest_title'],
'dest_desc' =>$input['dest_desc'],
'dest_img' =>$imgPath,
);
//$result=Cms::where('cms_id',$cms_id)->update($data);
$result=Destination::where('dest_id',$dest_id)->update($data);
if($result >0)
{
\Session::flash('flash_message','Destination Updated Successfull!!');
}
else
{
\Session::flash('flash_error_message','Destination Updation Failed!!');
}
}
I'm stuck at this code; please give a solution
Have you added enctype="multipart/form-data" to your <form> tag? Or if you're using the Form builder, 'files' => true?
Input::file('dest_img') is not an object. You might have not loaded the classes that define Input. Check that laravel is bootstrapped correctly.

Laravel 5 storage

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.

Categories