I make variable that contain the path of the directory where the uploading files will be stored. I wonder this works very well on windows but not on linux ubuntu. Perhaps the way I mention directory is wrong.
Following controller on Laravel;
public function Store(Request $request){
$data = array();
$data['product_name'] = $request->product_name;
$data['product_code'] = $request->product_code;
$data['details'] = $request->details;
$image = $request->file('logo');
if($image){
$image_name = date('dmy_H_s_i');
$ext=strtolower($image->getClientOriginalExtension());
$image_full_name=$image_name.'.'.$ext;
$upload_path='home/laravel/udemy/kash';
$image_url=$upload_path.$image_full_name;
$success = $image->move($upload_path,$image_full_name);
$data['logo']=$image_url;
$product=DB::table('products')->insert($data);
return redirect()->route('product.index')
->with('success','Product Created Successfully');
}
I found solution finally. $upload_path='$home/laravel/udemy/kash/';
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.
when developing locally, i was able to upload images in storage directory, but now as the diectory sructure changed a bit.
Now Intervention is showing an error.
Error is
message: "Image source not readable", exception: "Intervention\Image\Exception\NotReadableException",…}
exception: "Intervention\Image\Exception\NotReadableException"
This is my controller
public function submitAddProduct(Request $request){
$data = $request->validate([
'image' => ['required','image'],
'image_second' => ['required','image']
]);
//dd(request('image')->store('uploads','public'));
$imagePath = request('image')->store('uploads','public');
$image = Image::make(public_path("storage/{$imagePath}"));
$image->save();
$imagePath2 = request('image_second')->store('uploads','public');
$image2 = Image::make(public_path("storage/{$imagePath2}"));
$image2->save();
$dataVal = $request->all();
$dataVal['image'] = $imagePath;
$dataVal['image_second'] = $imagePath2;
// d3d($dataVal);
$d = Products::create($dataVal);
$id = $d->id;
if($id){
$arr = array('msg' => $id);
}
return Response()->json($arr);
}
it should get stored in the uploads directory.
My cpanel directory structure is as follows
The public_html contains
And the system folder inside the public_html contains
The symlinks are working fine, as i can see images that were previously present in uploads.
symlink.php is
<?php
symlink('/home//public_html/system/storage/app/public','/home//public_html/storage');
Only the first Image gets uploaded to storage/uploads and then interventions shows error.
Please Help
In my Laravel project I created a page to upload the files and I use the $file of laravel it works fine for some system only but for some system it shows an error as shown in image below.
Function I am using to upload files in model
public function add_document_sub_cert($req)
{
$subcontractor_id = $req['subcontractor_id'];
$reference_id = $req['reference_id'];
$files = $req->file("uploaded_doc0");
$i = 0;
foreach($files as $file){
$i++;
$ext = $file->guessClientExtension();
$name = $file->getClientOriginalName();
$file_name_1 = str_replace(".".$ext,"",$name);
$path = $file->storeAs('subcontractor/','avc'.$i.'.jpg');
if($path){
$document = new Document();
$document->doc_name = 'avc.jpg';
$document->module = 'subcontractor';
$document->reference_id = $reference_id;
$document->save();
}
}
}
Your error says that you didn't specify a filename. I see that your variable $file_name_1 is never used. Haven't you forgotten to use it somewhere?
Without knowing how your class Document works, it's impossible to tell you exactly where is the bug.
I'm trying to save an image to a folder in my laravel application.
I'm getting the error:
fopen(F:\blog\public/usr-data/photos): failed to open stream: Permission denied
Here's my laravel controller which is writing to this folder.
$error = false;
$absolutedir = public_path();
$dir = '/usr-data/photos';
$serverdir = $absolutedir.$dir;
$filename = array();
foreach($_FILES as $name => $value) {
$json = json_decode($_POST[$name.'_values']);
$tmp = explode(',',$json->data);
$imgdata = base64_decode($tmp[1]);
$fileAry = explode('.',$json->name);
$extension = strtolower(end( $fileAry ));
$fname = $card->id.'.'.$extension;
$handle = fopen($serverdir,'w');
fwrite($handle, $imgdata);
fclose($handle);
$filename[] = $fname;
}
I've tried using
Icacls "F:\blog\public/usr-data/photos" /grant Everyone:(OI)(CI)F
But no joy - still the same issue.
You need to set up writing permissions on a storage and public folders and then all folders inside these ones. Use right click on a folder, go to Properties and change folder permissions.
http://m.wikihow.com/Change-File-Permissions-on-Windows-7
EDIT: This will work for Linux only. I'm keeping this answer in case anyone runs into the same problem on a Linux machine.
Try this:
php artisan cache:clear
sudo chmod -R 777 app/storage
composer dump-autoload
Instead of using file write, you can also use the Laravel filesystem storage class. Please try with the below given example.
$image represents the encoded image.
$Id represents the id of the user.
public function imageUpload($image, $Id)
{
//Decode base64 string to image
$profile_image = 'image/jpeg;base64,' . $image;
$new_data = explode(";", $image);
$data = explode(",", $new_data[1]);
$file = base64_decode($data[1]);
$imageFileName = $Id . '.jpeg';
$image_path = '/storage/' . $imageFileName;
Storage::put($imageFileName, $file);
return $image_path;
}
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.