Laravel 4 Can';t将图像数据写入laravel中的路径

Laravel 4 Can';t将图像数据写入laravel中的路径,laravel-4,Laravel 4,我和这家伙犯了同样的错误: 基本上,我的错误是将图像上传到特定路径,我的代码如下: public function postCreate() { $validator = Validator::make(Input::all() , Product::$rules); if ($validator->passes()) { $product = new Product; $product->category_id = Input:

我和这家伙犯了同样的错误:

基本上,我的错误是将图像上传到特定路径,我的代码如下:

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 = date('Y-m-d-H:i:s')."-".$image->getClientOriginalName();
        Image::make($image->getRealPath())->resize(468, 249)->save('public/img/products'.$filename);
        $product->image = 'public/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();

} 
我只是想学习一下关于laravel电子商务web应用程序的教程

我想问题是我的目录中没有写权限,如何在目录中添加写权限。例如,公共文件夹,我在谷歌上搜索了一些地方,但我不明白我要编辑的是什么

例如htaccesss文件,或者我可以在cmd上进行写更改吗?另外,如何检查目录是否受写保护

另外,我正在使用windows。我附上了错误的截图


谢谢。

您可能需要更改日期格式,因为windows不允许文件名中使用冒号:

$filename=date('Y-m-d-H:i:s')。“-”$image->getClientOriginalName()

您还可能希望在路径中添加一个尾随斜杠,这样它就不会将文件名连接到文件夹路径:


Image::make($Image->getRealPath())->调整大小(468249)->保存('public/img/products'.$filename)

嗯,我做了john建议的更正,然后做了以下更正:

我替换了以下代码:

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

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

问题解决了,我不知道为什么公共路径有效,但没关系

除此之外,您还需要确保文件夹路径存在并且设置了正确的权限

$relPath = 'img/product/';
if (!file_exists(public_path($relPath))) {
    mkdir(public_path($relPath), 777, true);
}
其中,
$relPath
是相对于
public
目录的路径


但是,此要求是特定于windows的。在linux中,如果文件夹目录不存在,将创建它。

通常,当您还没有将图像存储在公用目录中的目录时,会发生此错误。有时可能是权限问题

您的
public
目录中是否存在
img
目录

要解决此问题,请执行以下步骤:

  • 使用以下代码段:
  • $relPath='img/'//公共目录中的路径
    如果(!file_exists(public_path($relPath)){//验证目录是否存在
    mkdir(public_path($relPath),666,true);//如果不存在,则创建它
    }
    
    或手动创建公开的
    img
    目录

    2.然后您可以保存您的图像:

    Image::make($Image)->resize(468249)->save(public_path('img/products'.$filename))//保存您的图像
    $product->image='img/products'.$filename//笔记
    $product->save();
    

    **注意:我们不需要在路径中指定
    公共
    目录,因为我们使用的是
    相对路径
    img
    目录将在
    public
    目录中创建。

    我还建议大家检查
    $path
    是否存在。就像Jose Seie使用本机PHP检查一样,我建议您考虑一下内置帮助程序。 这可以通过文件Facade helper实现:

    File::exists($imagePath) or File::makeDirectory($imagePath, 777, true);
    

    建议您使用Laravel内置函数、类和助手来提高应用程序的性能

    谢谢你指出这一点,但我仍然得到了错误