php - Unable to upload images on laravel app to hostgator hosting -
i've been searching around net try fix problem. changed code in controllersfolder multiple times , still no solution. changed permissions of img folder , products folder 777 , still no success.
this structure of folders on ftp cyberduck:
-->app_base/ ( has base laravel folder except /public/ folder) -->[some other folders...] -->public_html/ -->daveswebapp.us/ (name of website. has content of base public/folder) -->img -->products [empty folder]
this error receive each time try upload new product images in admin panel:
intervention \ image \ exception \ notwritableexception can't write image data path (/home2/ecuanaso/app_base/bootstrap/img/products/1417822656.jpg)
products controller code:
<?php class productscontroller extends basecontroller { public function __construct() { parent::__construct(); $this->beforefilter('csrf', array('on'=>'post')); $this->beforefilter('admin'); } public function getindex() { $categories = array(); foreach(category::all() $category) { $categories[$category->id] = $category->name; } return view::make('products.index') ->with('products', product::all()) ->with('categories', $categories); } public function postcreate() { $validator = validator::make(input::all(), product::$rules); if ($validator->passes()) { $product = new product; $product->category_id = input::get('category_id'); $product->title = input::get('title'); $product->description = input::get('description'); $product->price = input::get('price'); $image = input::file('image'); $filename = time() . '.' . $image->getclientoriginalextension(); $path = public_path('img/products/' . $filename); image::make($image->getrealpath())->resize(468, 249)->save($path); $product->image = 'img/products/'.$filename; $product->save(); return redirect::to('admin/products/index') ->with('message', 'product created'); } return redirect::to('admin/products/index') ->with('message', 'something went wrong') ->witherrors($validator) ->withinput(); } public function postdestroy() { $product = product::find(input::get('id')); if ($product) { file::delete('public/'.$product->image); $product->delete(); return redirect::to('admin/products/index') ->with('message', 'product deleted'); } return redirect::to('admin/products/index') ->with('message', 'something went wrong, please try again'); } public function posttoggleavailability() { $product = product::find(input::get('id')); if ($product) { $product->availability = input::get('availability'); $product->save(); return redirect::to('admin/products/index')->with('message', 'product updated'); } return redirect::to('admin/products/index')->with('message', 'invalid product'); }
}
images should go public folder , not in app directory in code trying move image app directoy defining directory address public_path following code uploads image public/uploads folder accessible via visiting yourdomain.com/img.jpg
//create 2 empty variables outside of conditional statement because gonna access them later on $filename = ""; $extension = ""; //check if file input, assuming input box named photo if (input::hasfile('photo')) { //create array allowed extensions $allowedext = array("png","jpg","jpeg","gif"); /get file uploaded user $photo = input::file('photo'); //set destination path assuming have chmod 777 upoads folder under public directory $destinationpath = public_path().'/uploads'; //generate random filename $filename = str_random(12); //get extension of file uploaded user $extension = $photo->getclientoriginalextension(); //validate if uploaded file extension allowed in $allowedext array if(in_array($extension, $allowedext )) { //everything turns true move file destination folder $upload_success = input::file('photo')->move($destinationpath, $filename.'.'.$extension); }
Comments
Post a Comment