无法将laravel应用上的图像上传到hostgator托管

时间:2014-12-05 23:55:41

标签: php laravel

我一直在网上搜索,试图解决这个问题。多次更改了我的controllersfolder中的代码,仍然没有解决方案。我将我的img文件夹和产品文件夹的权限更改为777但仍然没有成功。

这是我的FTP cyberduck上文件夹的结构:

-->app_base/ ( Has Everything from the base laravel folder EXCEPT the /public/ folder)
-->[some other folders...]
-->public_html/ 
    -->daveswebapp.us/ (name of my website. Has all the content of my base public/folder)
       -->img
         -->products
             [empty folder]

这是我每次尝试在管理面板中上传新产品图片时收到的错误:

Intervention \ Image \ Exception \ NotWritableException 
Can't write image data to path         (/home2/ecuanaso/app_base/bootstrap/img/products/1417822656.jpg)

产品控制器代码:

<?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() as $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');
}

}

2 个答案:

答案 0 :(得分:0)

图片应该进入公共文件夹,而不是代码中的app目录,你试图将图像移动到app directoy中,但是使用public_path定义目录地址,下面的代码将图像上传到你的public / uploads文件夹中,然后通过访问yourdomain.com/img.jpg

//create two empty variables outside of conditional statement because we gonna access them later on 
    $filename = "";
    $extension = "";
//check if you get a file from input, assuming that the input box is named photo
    if (Input::hasFile('photo'))
    {
//create an array with allowed extensions
        $allowedext = array("png","jpg","jpeg","gif");
/get the file uploaded by user
        $photo = Input::file('photo');
//set the destination path assuming that you have chmod 777 the upoads folder under public directory
        $destinationPath = public_path().'/uploads';
//generate a random filename 
        $filename = str_random(12);
//get the extension of file uploaded by user
        $extension = $photo->getClientOriginalExtension();
//validate if the uploaded file extension is allowed by us in the $allowedext array
        if(in_array($extension, $allowedext ))
        {
//everything turns to be true move the file to the destination folder
            $upload_success = Input::file('photo')->move($destinationPath, $filename.'.'.$extension);
        }

答案 1 :(得分:0)

不确定一个解决方案是否会在这里,但我在经过几天的搜索后想出来了。

我将文件保存到错误的目录中。从 - &gt;保存方法

中取出了公众

我改变了

Image::make($image->getRealPath())->resize(468, 249)->save('public/img/products/'.$filename);

为:

Image::make($image->getRealPath())->resize(468, 249)->save('img/products/'.$filename);