I'm trying to make a form to upload a file, an excel file,
this is my html
<h2>Agregar Torneo</h2>
{{Form::open('admin/addtorneo', 'POST',array('files' => 'true', 'enctype' => "multipart/form-data"))}}
{{Form::label('cvs', 'Archivo:')}}
{{Form::file('cvs')}}
{{Form::submit('Subir')}}
{{Form::close()}}
and the php
$file = Input::file('cvs');
$destinationPath = 'uploads/'.Str::random(5);
$filename = Input::file('cvs.name');
$uploadSuccess = Input::file('cvs')->move($destinationPath, $filename);
$new = new Torneo;
$new->nombre = $filename;
$new->dir = $destinationPath;
$new->save();
return "Torneo agregado <br> <a href='../admin'>Volver</a>";
but I keep getting
Call to a member function move() on a non-object
I tried using $file->getClientOriginalName() instead of Input::file('cvs.name') but I get Call to a member function getClientOriginalName() on a non-object, It seems to me that the form isn't right and it ain't reciving the file correctly
Just call Input::file('cvs') one time, the second time it becomes null object.
example :
$file = Input::file('cvs');
$destinationPath = 'uploads/'.Str::random(5);
$file->move($destinationPath);
It works.
Related
I am trying to upload an image into my database. When i do upload, i get this error below
Call to undefined method Intervention\Image\ImageManager::upload()
Searching on the internet for solutions, i found this method
adding this line 'Intervention\Image\ImageServiceProvider' in my $providers in config/app.php
adding this line 'Image' => 'Intervention\Image\Facades\Image' in my $aliases in config/app.php
In my controller as well, i have use Image. But then i am still getting this error above. What could i be missing please?
Controller
public function uploadImagePost(UploadUserImageRequest $request)
{
$user = Auth::user();
$image = $request->file('profile_image');
if (false === empty($user->image_path)) {
$user->image_path->destroy();
}
$relativePath = 'uploads/users/' . $user->id;
$path = $relativePath;
$dbPath = $relativePath . DIRECTORY_SEPARATOR . $image->getClientOriginalName();
$this->directory(public_path($relativePath));
Img::upload($image, $path);
$user->update(['image_path' => $dbPath]);
return redirect()->route('my-account.home')
->with('notificationText', 'User Profile Image Uploaded successfully!!');
}
Library you have used doesn't have upload() method. Use save() method for saving the file.
// read image from temporary file
$img = Image::make($_FILES['image']['tmp_name']);
// save image
$img->save('foo/bar.jpg');
Refer this link for more details
New to laravel here.
I'm trying to save an image in the models and in the folder in the project. Seems like it only saves in the folder but returning BadMethodCallException in Macroable.php line 74: Method save does not exist. whenever i save it to database. Any help is very much appreciated!
public function itemPicture(Request $request)
{
if($request->hasFile('itemPic'))
{
$bfItemPic = $request->file('itemPic');
$filename = /*time() . '.' . */ $bfItemPic->getClientOriginalName();
Image::make($bfItemPic)->resize(250,250)->save( public_path('/itempictures/' .$filename));
//bufashItems::create($request->all());
$bfproducts = bufashItems::all();
$bfproducts->item_picture = $filename;
$bfproducts->save();
}
return redirect('/Items');
}
You get this error because you're trying to use save() method on a collection. You should get an object to make it work, for example:
$bfproducts = bufashItems::where('id', 5)->first();
$bfproducts->item_picture = $filename;
$bfproducts->save();
You are trying to save a collection there and save method calls do not exist on collections, that's why it's throwing the error.
If you are trying to create a new record, try the following:
$bfproducts = new bufashItems();
$bfproducts->item_picture = $filename;
$bfproducts->save();
Make sure you have set the fillable field correctly on bhfashItems class.
bufashItems::all(), this is the eloquent by which you can retrieve data.
You have to do:
$bfproducts = new bufashItems();
$bfproducts->item_picture = $filename;
$bfproducts->save();
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.
I'm using yii framework but I think this is related to PHP
In my controller, I have the following code
$model = new Events;
$model->type_id = $type_id;
$checkFileUpload = checkFileUpload($model);
the function checkFileUpload is a custom function which contains
function checkFileUpload($model)
{
$rnd = rand(0, 9999);
$uploadedFile = CUploadedFile::getInstance($model, 'image');
if($uploadedFile->error == 0)
{
$fileName = "{$rnd}-{$uploadedFile}"; // random number file name
$model->image = $fileName;
...
I got the error get property of non-object in $uploadedFile->error.
I've tried to use reference to the model instead, but it is deprecated and does not work for me.
If I use the code of the called function (checkFileUpload) within the controller code, it works fine. I suspect that object is not passed in a correct way.
Any help?
This is because your call to CUploadedFile::getInstance returns null and not the instance you desired.
Null is returned if no file is uploaded for the specified model attribute.
— Yii Documentation
It seems like your file was not correctly uploaded. I am not a Yii Framework user, but the documentation states:
The file should be uploaded using CHtml::activeFileField.
— Yii Documentation
So you should verify that the file was actually correctly uploaded with the proper method from the Yii Framework.
PS: Objects are always passed by reference.
$model = new Events;
$type_id=$model->type_id;
$checkFileUpload = checkFileUpload($model);
function checkFileUpload($model)
{
$rnd = rand(0, 9999);
$uploadedFile = CUploadedFile::getInstance($model, 'image');
if(!isset($uploadedFile->getHasError()))
{
$fileName = "{$rnd}-{$uploadedFile}"; // random number file name
$model->image = $fileName;
The problem occurred because at the time when you are using $uploadedFile->error,the value of $uploadedFile is null.
The following line is not giving you the desired value
$uploadedFile = CUploadedFile::getInstance($model, 'image');
Which means no file has been uploaded.
Try CVarDumper::dump($_FILES,10,true);
This will tell you whether the problem is with the UPLOADING OF THE FILE or GETTING THE DETAILS OF THE UPLOADED FILE
you cant access the private property $_error $uploadedFile->_error if you are trying to. you must call $uploadedFile->getError() in your code. Also $uploadedFile will return null if no file uploaded so you must take care of that as well.
$rnd = rand(0, 9999);
$uploadedFile = CUploadedFile::getInstance($model, 'image');
if(!empty($uploadedFile) && !$uploadedFile->getHasError())
{
$fileName = "{$rnd}-{$uploadedFile}"; // random number file name
$model->image = $fileName;
will work for you.
I have setup a repository to create a new resident.
<?php namespace Crescent\Repos;
interface ResidentRepository {
public function create($input);
}
Then in my controller I have, which uses the intervention image class to resize an image and it uploads correctly to the directory, but how can I save the name of the file to the DB using this repo?
public function store()
{
if (Input::hasFile('photo')){
$res = new Residents;
$file = Input::file('photo');
$name = $file->getClientOriginalName();
$input = Input::all();
$image = Image::make(Input::file('photo')->getRealPath())->resize(200, 200);
$image->save(public_path() . '/uploads/residents/' . $input['photo']->getClientOriginalName());
$res->photo = $name; // This doesn't work
}
$this->resident->create(Input::all());
}
Everything else works all the data, but the image isn't storing the name just showing some temp dir/name like /tmp/phpIX7KcY
I see that you have done $res = new Residents; and $res->photo = $name; but you haven't done $res->save(); which would have saved the name to the table corresponding to Residents. Also since you haven't added anything else to $res, only the photo would be saved.
Replace the code in your controller with the following:
public function store()
{
$input = Input::all();
if (Input::hasFile('photo')){
$file = Input::file('photo');
$name = $file->getClientOriginalName();
$image = Image::make(Input::file('photo')->getRealPath())->resize(200, 200);
$image->save(public_path() . '/uploads/residents/' . $input['photo']->getClientOriginalName());
$input['photo'] = $name;
}
$this->resident->create($input);
}
If in your code $this->resident->create(Input::all()); saves all data properly except the photo, it is because by passing Input::all() you're saving everything exactly as it was received from the client and the filename received from the resizing operation isn't present in Input::all(). By assigning Input::all() to the variable $input and doing $input['photo'] = $name;, the location of the file on the server is stored instead of the location on the client. So, by doing $this->resident->create($input);, the location on the server is stored along with other data received from the client.