I have this code:
public function getUpload()
{
return View::make('foto/upload');
}
public function postUpload()
{
$file = Input::file('file');
$filename = $file->getClientOrginalName();
$path = 'uploads';
return $file->move($path, $filename);
}
Here views:
{{Form::open(array('url' => 'foto/upload', 'class' => 'dropzone'))}}
{{Form::close()}}
After upload, display 'X', but how I can display this error? Or what I should fix?
You got a typo. It is "getClientOrIginalName"
Related
public function update_room_detail(Request $request)
{
$request->validate([
'room_type' => 'required',
]);
if($images = $request->file('room_image'))
{
foreach($images as $item):
$var = date_create();
$time = date_format($var, 'YmdHis');
$imageName = $time.'-'.$item->getClientOriginalName();
$item->move(public_path().'/assets/images/room', $imageName);
$arr[] = $imageName;
endforeach;
$image = implode("|", $arr);
}
else
{
unset($image);
}
RoomDetail::where('id',$request->room_id)->update([
'room_type' => $request->room_type,
'room_image' => $image,
]);
Alert::success('Success', 'Rooms details updated!');
return redirect()->route('admin.manage-room');
}
In the above code I am trying to update image in database table. When I click on submit button then it show Undefined variable: image and when I use $image='' in else part instead of unset($image) then blank image name save. So, How can I solve this issue please help me? Please help me.
Thank You
As per the PHP documentation:
unset() destroys the specified variables.
What this means is that it doesn't empty the value of the specified variables, they are destroyed completely.
$foo = "bar";
// outputs bar
echo $foo;
unset($foo);
// results in Warning: Undefined variable $foo
echo $foo;
You've already discovered how to handle this:
when I use $image='' in else part instead of unset($image) then blank image name save
Fix:
Note : uses Storage library feel free to use any other.
public function update_room_detail(Request $request)
{
$request->validate([
'room_type' => 'required',
]);
$imageNames = array();
if ($request->hasFile('room_image')) {
$images = $request->file('room_image');
foreach ($images as $item) {
$var = date_create();
$time = date_format($var, 'YmdHis');
$imageName = $time . '-' . $item->getClientOriginalName() .".".$item->extension();
$item->storeAs('/public/room-images-path', $imageName);
array_push($imageNames, $imageName);
}
}
RoomDetail::where('id', $request->room_id)->update([
'room_type' => $request->room_type,
'room_image' => $imageNames,
]);
Alert::success('Success', 'Rooms details updated!');
return redirect()->route('admin.manage-room');
}
I am developing API in Laravel. I will receive the image in base64 format. How can I convert the base64 to image in Laravel?
public function profile_image_upload(User $user, Request $request)
{
$request->validate([
'picture' => 'required|image|mimes:jpeg,png,jpg|max:2048',
], []);
if ($user->picture !== null)
Storage::delete($user->picture);
$res_upload = uploadService::store_image($request->file('picture'), config('upload.user_profile_picture_storage_path'));
if ($res_upload)
return $user->update([
'picture' => $res_upload,
]);
return false;
you can create this helper function
in Helper.php ref link https://laravel-news.com/creating-helpers
function base64ImageUpload($path, $file)
{
$image = $file; // your base64 encoded
if (preg_match('/base64/', $file)) {
$imageInfo = explode(";base64,", $image);
$imgExt = str_replace('data:image/', '', $imageInfo[0]);
$image = substr($image, strpos($image, ",") + 1);
$name = \Str::random(40) . '.' . $imgExt;
$filePath = $path . '/' . $name;
\Storage::put($filePath, base64_decode($image));
return $filePath;
} else {
return null;
}
}
then whenever u need to upload u can just call this function like
$imageLocation = base64ImageUpload(config('upload.user_profile_picture_storage_path'),$request->picture);
I have a code where I want to save images using Vue and Laravel saves the route in the database
The Controller:
public function update(Request $request, $id){
$home = Home::findOrFail($id);
$home->background = $request->background;
$home->title = $request->title;
$home->subtitle = $request->subtitle;
$home->icon_go = $request->icon_go;
$fileName = $request->image;
$path = $_SERVER['DOCUMENT_ROOT'].'assets/images/'.$fileName;
$home->image = $path;
$home->update();
file_put_contents($path, $fileName);
return response()->json([
'status'=> 200,
'title' => 'Home Update',
'data' => $home,
]);
}
The input:
<v-col cols="12" sm="12" md="12">
<input type="file"
#change="getImage"
label="Imagen"
required
:class="{ 'is-invalid' : form.errors.has('image') }">
<has-error :form="form" field="image"></has-error>
</v-col>
Only I just put the input, the form is working fine
The function update:
update(){
//Update a resource
this.$Progress.start()
this.form.busy = true;
this.form.image = this.form.image.name
this.form.put('/api/v1/home/' + this.form.id)
.then(response => {
this.getHome()
if (this.form.successful) {
this.$Progress.finish()
this.updateNotify()
}else{
this.$Progress.fail()
this.$snotify.error('¡Ha ocurrido un error!', 'Error')
}
})
.catch(e => {
this.$Progress.fail()
console.log(e)
})
},
The problem may be in the controller but I cannot detect it.
I'd appreciate your help.
The only thing that does not work is that the image is not showing the content
The photo is saved in the folder public / assets / images This is how the image is saved in the folder
Try using the below code. Since $request->image won't give file object. Instead, we need to use file() helpr.
public function update(Request $request, $id){
$home = Home::findOrFail($id);
$home->background = $request->background;
$home->title = $request->title;
$home->subtitle = $request->subtitle;
$home->icon_go = $request->icon_go;
$file = $request->file('image'); //gets the image file
$path = $_SERVER['DOCUMENT_ROOT'].'assets/images/';
$home->image = $path.$file->getClientOriginalName();
$home->update();
$file->move($path, $file->getClientOriginalName()); //stores in location
return response()->json([
'status'=> 200,
'title' => 'Home Update',
'data' => $home,
]);
}
The problem is that my loop only running and uploading the same first pic each time rather than uploading each other in a row one after one!
Here is my code of the form
{!! Form::file('photos[]', ['roles' => 'form', 'class' => 'form-control-file','multiple' => true]) !!}
Here is my code of the controller
$files=$request->file('photos');
foreach ($files as $file) {
$insert = new Images;
$insert->youth_fashion_images_category = $request->selectproduct;
$destinationPath = 'uploads/products';
$imageName = 'uploads/products/'.time().'.'.$file->getClientOriginalExtension();
$insert->Save();
$uid = $insert->id;
$file->move($destinationPath,$imageName);
$image = array(
'youth_fashion_images_img' => $imageName
);
Images::where('youth_fashion_images_id',$uid)->update($image);
}
return redirect('adminpanel/viewimages');
Try this code :
if($request->hasfile('photos'))
{
foreach($request->file('photos') as $image)
{
$destinationPath = 'uploads/products';
$imageName = 'uploads/products/'.time().'.'.$image->getClientOriginalExtension();
$image->move($destinationPath,$imageName);
$insert = new Images;
$insert->youth_fashion_images_category = $request->selectproduct;
$insert->youth_fashion_images_img = $imageName;
$insert->Save();
}
}
You should try this code.
if($request->hasfile('photos')) {
foreach($request->file('photos') as $image)
{
$destinationPath = 'uploads/products';
$name = 'uploads/products/'.time().'.'.$image->getClientOriginalName();
$image->move($destinationPath, $name);
$data[] = $name;
}
}
$insert= new Images;
$insert->youth_fashion_images_img = json_encode($data);
$insert->save();
json_encode to insert the multiple image names in one row.
So add $name in array $data[] = $name;.
I hope this will helps.
I created a form here with Laravel and DropzoneJS and I tried uploading a Gimp file (.xcf) and when it is uploaded it is saved in S3 as the following
<random-name>.
without the "xcf" extension just random name ending with a dot.
Also, I created a text file and renamed it to test.xcf when I tried uploading that file it was uploaded with the .txt extension.
Here is my UploadController.php which handles the upload:
<?php
namespace App\Http\Controllers;
use App\Upload;
use Illuminate\Http\Request;
class UploadController extends Controller
{
public function upload(Request $request)
{
$originalName = $request->file('file')->getClientOriginalName();
$fileSize = $request->file('file')->getClientSize();
$path = $request->file('file')->store('documents');
$explode = explode('documents/', $path);
$name = $explode[1];
$uniqueId = $this->generateUniqueId();
$upload = new Upload();
$upload->unique_id = $uniqueId;
$upload->name = $name;
$upload->path = $path;
$upload->original_name = $originalName;
$upload->size = $fileSize;
if ($upload->save())
{
return response()->json([
'original_name' => $originalName,
'size' => $fileSize,
'url' => env('AWS_URL') . $path,
'id' => $uniqueId,
'status' => 'OK'
]);
}
return response()->json(['status' => 'BAD', 'message' => 'There was a problem saving your file.']);
}
public function generateUniqueId()
{
$result = '1';
$result .= rand(100000000, 999999999);
while(Upload::where('unique_id', '=', $result)->first())
{
$result = '1';
$result .= rand(100000000, 999999999);
}
return $result;
}
}
I've got no idea why it's doing that.
I suggest, you generate your own hash for filename, like I do in this code:
$file = $request->file('csv');
$path = $file->storeAs(
'csv',
md5($file->getClientOriginalName()) . $file->getClientOriginalExtension(),
's3'
);
You can also add uniqid() to md5 input
If you're using laravel 5+ then you should get the extension also using this.
$extension = $file->getClientOriginalExtension();
This will work fine.