Php 无法将laravel应用程序上的图像上载到hostgator主机
我一直在网上搜索,试图解决这个问题。多次更改了我的Controllers文件夹中的代码,但仍然没有解决方案。我将img文件夹和products文件夹的权限更改为777,但仍然没有成功 这是我的FTP cyberduck上的文件夹结构:Php 无法将laravel应用程序上的图像上载到hostgator主机,php,laravel,Php,Laravel,我一直在网上搜索,试图解决这个问题。多次更改了我的Controllers文件夹中的代码,但仍然没有解决方案。我将img文件夹和products文件夹的权限更改为777,但仍然没有成功 这是我的FTP cyberduck上的文件夹结构: -->app_base/ ( Has Everything from the base laravel folder EXCEPT the /public/ folder) -->[some other folders...] -->public
-->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');
}
}图像应进入公用文件夹,而不是代码中的应用程序目录。您正在尝试将图像移动到应用程序目录中,但使用公用路径定义目录地址。以下代码将图像上载到您的公用/上载文件夹中,然后可通过访问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);
}
不确定这里是否有解决方案,但经过几天的搜索,我终于找到了 我把文件保存到了错误的目录中。从->保存方法中取出公共文件 我变了
Image::make($image->getRealPath())->resize(468, 249)->save('public/img/products/'.$filename);
致:
可能是服务器设置。看看你的php.ini像什么?你能更具体一点吗?让你的代码简单一点,先看看他们上传到哪里。输入::文件'photo'->getRealPath;只是想看看它是否正在上传。如果你的代码在你的机器上运行,那么改变就是服务器设置。我改变了$image=Input::file'image';输入::文件'photo'->getRealPath;我现在收到了这个错误:对非对象调用成员函数getRealPath
Image::make($image->getRealPath())->resize(468, 249)->save('img/products/'.$filename);