Line 15, why do you return the filename there?
Ehesp said:
Line 15, why do you return the filename there?
I just want to check value of this. Without return it doesn't work too.
Add 'files' => true To your Form array.
And also fix your path (seems like there is a typo):
user/admin/products/addd to user/admin/products/add
Reached said:
Add 'files' => true To your Form array.
And also fix your path (seems like there is a typo):
user/admin/products/addd to user/admin/products/add
How can I move files to path /public/uploads/products
Here is a snippet of some of my own code that might help you.
$image = Input::file('image');
$filename = date('Y-m-d-H:i:s')."-".$image->getClientOriginalName();
$path = public_path('images/products/'.$filename);
Image::make($image->getRealPath())->resize(200, 200)->save($path);
$product->image = 'images/products/'.$filename;
Please note though that I am using the great Intervention image package for laravel.
EDIT: added link
How can I set file type, I mean that user can only upload images with extension .jpg, .jpeg, .gif, .png?
You would do that in your validation model (assuming you are using some kind of package).
Example:
'image'=>'required|image|mimes:jpeg,jpg,png,bmp,gif,svg',
I did it in this way but I got The image must be an image. The image must be a file of type: jpeg, jpg, png, bmp, gif, svg.
I'm not sure it's related, but did you add: 'files' => true As I suggested earlier?
Please make sure that you are in fact uploading a file of the specified type (i've failed on this myself).
I really do I'm choosing 6.jpg it has proper extension
hello, I am working ona project and I am trying to upload a photo, too. I have a another question. When I try to change a profile picture I can but I can not cgange a name from my database. I dont understand why :s Could you help me guys? here is my codes.
<?php
$data = Input::only('full_name', 'twitter_username', 'instagram_username', 'facebook_username', 'bio', 'image');
$file = Input::file('image');
$destinationPath = public_path().'/Avatars/';
$filename = Sentry::getUser()->username. '.jpg';
$uploadSuccess = $file->move($destinationPath, $filename);
$user->profile->fill($data)->save();
?>
@barantr90: how does your model looks like?
This is how i Upload files and images (with the intervention package https://github.com/Intervention/image) and re-size image
controller.php
class FileController extends BaseController
{
public function postUploadFile()
{
$file = Input::file('file'); // your file upload input field in the form should be named 'file'
//var_dump($file);
$rules = array(
'file' => 'required|mimes:png,gif,jpeg,pdf,doc,rtf,xls |max:20000'
);
$validator = Validator::make( array('file'=> $file) , $rules);
if($validator->passes())
{
//get File Name
$newFilename = $file->getClientOriginalName();
//clean File Name
$newFilename = $this->cleanFileName( $newFilename );
//destination path
$destinationPath= public_path('uploads') .'/files/';//'uploads/jav_'.str_random(8);
//check if file exist
while (File::exists( $destinationPath . $newFilename))
{
//if file exit, add unique name
$newFilename = uniqid() . "_" . $newFilename ;//;$file->getClientOriginalName();
}
//upload file
$uploadSuccess = $file->move($destinationPath, $newFilename);
if( $uploadSuccess )
{
//file as uploaded
return 'success';//Response::json('success', 200); // or do a redirect with some message that file was uploaded
}
else
{
//file was not upload
return 'error'; //Response::json('error', 400);
}
}
else
{
return 'Error. Invalid file format or size > 2MB'; //Response::json('error', 400);
}//end Validation
}//end Method
public function postUploadImage()
{
$file = Input::file('file'); // your file upload input field in the form should be named 'file'
//var_dump($file);
$rules = array(
'file' => 'required|mimes:png,gif,jpeg|max:20000'
);
$validator = \Validator::make( Input::all() , $rules);
if($validator->passes())
{
//get File Name
$newFilename = $file->getClientOriginalName();
$newFilename = $this->cleanFileName( $newFilename );
//destination path
$destinationPath= public_path('uploads') .'/images/';//'uploads/jav_'.str_random(8);
//check if file exist
while (File::exists( $destinationPath . $newFilename))
{
//if file exit, add unique name
$newFilename = uniqid() . "_" . $newFilename;
}
$uploadSuccess = Image::make( $file->getRealPath() )
->resize(250,null, function ($constraint) { $constraint->aspectRatio(); })
->save($destinationPath.$newFilename);
// see http://image.intervention.io/api/resize
if( $uploadSuccess )
{
//$item = Item::find( $item_id );
//$item->image = $filename;
//$item->save();
return 'success';//Response::json('success', 200); // or do a redirect with some message that file was uploaded
}
else
{
return 'error'; //Response::json('error', 400);
}
}
else
{
return 'error. Invalid file format or size >2Mb'; //Response::json('error', 400);
}
}
/*
* Clean the file name
*/
private function cleanFileName($fileName)
{
//remove blanks
$fileName = preg_replace('/\s+/', '', $fileName);
//remove charactes
$fileName = preg_replace("/[^A-Za-z0-9_-\s.]/", "", $fileName);
return $fileName;
}
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community