Laravel - Error Server Error when i upload File in Server - laravel

I upload correctly my file in my Localhost, but when I upload my app in the server, they give me Error: Server Error after uploading the file.
This is my controller :
$exploded = explode(',', $request->image);
$decoded = base64_decode($exploded[1]);
if(str_contains($exploded[0], 'jpeg'))
$extension = "jpg";
else
$extension = "png";
$fileName = str_random().'.'.$extension;
$path = public_path().'/'.$fileName;
file_put_contents($path, $decoded);
$person->photo = $fileName;
In my localhost, files are saved in the public folder. I would like to save my files in my serve in public_html or in public_html/image.

$file = $r->file('photo'); //get photo from output
$foreign_name = mt_rand(100000,999999);//name is betwwen 100000 and 999999
$destination = '/images'; //destination path
$file_name = str_replace(' ', '_', $foreign_name);
$file_name .= '.'.$file->getClientOriginalExtension(); //add to name jpg or png or...
$file->move($destination, file_name);

Related

Upload image and extract filename instead of file path

I am using following code to upload thumbnail in "storage/app/public/websites"
$path = $request->file('thumbnail')->store('public/websites');
It is working fine and upload images to "websites" directory but problem is it returns the actual path e.g. websites/r63mAKN1kil3BIwvwwRevOv93MgWQFme39BwH8ZV.jpeg
i only want to save image name e.g. r63mAKN1kil3BIwvwwRevOv93MgWQFme39BwH8ZV.jpeg in database table.
By default Laravel generates Unique ID for image name. Is there way to return only filename instead of path ?
This is the hash name, so, first I think you need to separate your steps.
$file = $request->file('thumbnail');
$path = $file->store('public/websites');
when you need to add file name you can use $file->hashName();
You can use the basename function
http://php.net/manual/en/function.basename.php
$filename = basename($path);
This is what you need
$extension = $request->image->getClientOriginalExtension();
$image_name = str_replace(' ', '', trim($request->model) . time() . "." . $extension);
and to move the image to your desired folder use:
$image_path = $request->image->move(public_path('images'), $image_name);
$info = pathinfo( $url );
$contents = ( new \GuzzleHttp\Client() )->get( $url, [ 'verify' => true ] )->getBody()->getContents();
$file = str_finish(sys_get_temp_dir(), '/') . $info[ 'basename' ];
\File::put( $file, $contents );
$ext = ( new UploadedFile( $file, $info[ 'basename' ] ) )->guessExtension();

Laravel - Image source not readable after publish website

I was created simple CMS and everything works at localhost. After published website I can't upload image. I have an error "Image source not readable". I changed chmod upload's folder to 777.
My code:
$minphoto_path = 'uploads/img';
$minphoto = $request->minphoto;
$minphoto_new_name = time().$minphoto->getClientOriginalName();
$minphoto->move($minphoto_path, $minphoto_new_name);
$img = Image::make(public_path($minphoto_path . '/' .$minphoto_new_name))->resize(240, 338)->save(public_path($minphoto_path . '/' .$minphoto_new_name));
Try upload that way:
$file = Input::file('file');
$fileName = time() . '-' . $file->getClientOriginalName();
$file->move('uploads', $fileName);
$img = Image::make($file->getRealPath())->resize(320, 240)
->save('public/uploads/'. $file->getClientOriginalName());
Or shorter:
$file = Input::file('file');
$img = Image::make($file)->resize(320, 240)
->save('public/uploads/'. $file->getClientOriginalName());

Trying to save the file from amazon s3 on web server in Laravel

I am trying to save the file from amazon s3 in laravel,but can not able to save it on the server.Please check and let me know is it correct ?
$fileUrl = Input::get('actual_file_url') ;
$fileNameWithExtension = substr($fileUrl, strrpos($fileUrl, '/') + 1);
$disk = Storage::disk('s3');
$file = $disk->get($fileNameWithExtension);
Storage::disk('local')->put($fileNameWithExtension,$file);
$storagePath = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix();
$extension = $file->getClientOriginalExtension();
$fileName = $file->getFilename().'.'.$extension;

Laravel 5.2 upload image to storage

I want to ask, how can I make that somebody selects an image, then he clicks upload and the image will save into my project (example: project/public/images). Thank you :)
You can just do:
$file = $request->file('image');
$name = $file->getClientOriginalName();
$path = public_path("images/$name");
Storage::put($path, File::get($file->getRealPath()));
However. I would recommend to use laravel medialibrary package:
https://docs.spatie.be/laravel-medialibrary/v4/introduction
This way you can just do:
$post->addMediaFromRequest('image')->toCollection('images');
Or else you can do like this
$destinationPath = '';
$filename = '';
if (Input::hasFile('file')) {
$file = Input::file('file');
$destinationPath = public_path().'/images/';
$filename = time() . '_' . $file->getClientOriginalName();
$filename = str_replace(' ','_',$filename);
$uploadSuccess = $file->move($destinationPath, $filename);
}

How to save a jpeg image from a base64 data string with move_uploaded_file or file_put_contents?

currently i developing a mobile app that able to snap photo and save in server. I able to pass the image base64 data string to my php using AJAX. I also can decode the base64 data string to image, but cannot save in my server. Every time i run my app, the result show that i already save my image into server but when i check in my server i didn't find it. I follow the example on https://xuri.me/2014/11/18/save-a-png-image-from-a-base64-data-string-with-php.html.
Below is my php code.
$img = $_POST['img'];
$img = str_replace('data:image/jpg;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$img_name = uniqid();
$file_name = $img_name . '.jpeg';
$file_path = realpath("../media/rewardreceipt/");
$file_path = $file_path . "/" . $file_name ;
if(file_put_contents($file_path , $data)) {
$arr = array('result' =>$file_name);
echo json_encode($arr);
} else{
$arr = array('result' =>'false');
echo json_encode($arr);
}

Resources