php Laravel 4 - 文件上传

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19060459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:44:28  来源:igfitidea点击:

Laravel 4 - File Upload

phpfile-uploadlaravellaravel-4

提问by George Rivera

I have a Form where among other info, a user would upload an image. I want to store the path to the image on the database and save the image to the public/img/folder on the Server.

我有一个表单,其中除其他信息外,用户将上传图像。我想将图像的路径存储在数据库上并将图像保存到public/img/服务器上的文件夹中。

The form was opened using: {{ Form::open(['route'=>'pizzas.store', 'files'=>true]) }}so that it would be able to POST files. Inspecting the HTML I have the folowing:

表单是使用打开的:{{ Form::open(['route'=>'pizzas.store', 'files'=>true]) }}这样它就可以 POST 文件。检查 HTML 我有以下内容:

<form method="POST" action="http://mypizza/pizzas" 
  accept-charset="UTF-8" enctype="multipart/form-data">

I can POST to my Controller and receive all the data from the Form as expected. The method handling the file is as follows:

我可以 POST 到我的控制器并按预期从表单接收所有数据。处理文件的方法如下:

public function store() {
    //TODO -  Validation

    $destinationPath = '';
    $filename        = '';

    if (Input::hasFile('image')) {
        $file            = Input::file('image');
        $destinationPath = '/img/';
        $filename        = str_random(6) . '_' . $file->getClientOriginalName();
        $uploadSuccess   = $file->move($destinationPath, $filename);
    }


    $pizza = Pizza::create(['name'       => Input::get('name'),
                           'price'       => Input::get('price'),
                           'ingredients' => Input::get('ingredients'),
                           'active'      => Input::get('active'),
                           'path'        => $destinationPath . $filename]);

    if ($pizza) {
        return Redirect::route('pizzas.show', $pizza->id);
    }

    //TODO - else
}

When I select a File and Submit the Form, everything seems to work, except that no file is saved on the /imgfolder. The database registers the file path and name correctly.

当我选择一个文件并提交表单时,一切似乎都有效,只是文件/img夹中没有保存文件。数据库正确注册文件路径和名称。

Running dd($uploadSuccess);right after the if { ...}block, I get the following:

dd($uploadSuccess);if { ...}块之后运行,我得到以下信息:

object(Symfony\Component\HttpFoundation\File\File)#220 (2) {
  ["pathName":"SplFileInfo":private]=> string(17) "/img\YZmLw7_2.jpg"
  ["fileName":"SplFileInfo":private]=> string(12) "YZmLw7_2.jpg" }

What am I doing wrong?

我究竟做错了什么?

回答by Reflic

Your $destination_pathis wrong. You have to include the path to the /public directory in your variable $destination like this:

你的$destination_path是错误的。您必须在变量 $destination 中包含 /public 目录的路径,如下所示:

$destinationPath = public_path().'/img/';

$destinationPath = public_path().'/img/';

回答by Travis Bennett

Since you appear to be using php 5.4 you also might consider using Stapler. It's quite stable now (will be out of beta very shortly) and would save you from a lot of the boilerplate that you're having to write right now.

由于您似乎在使用 php 5.4,您也可以考虑使用Stapler。它现在非常稳定(很快就会结束测试版)并且可以使您免于现在必须编写的大量样板。

回答by arpit desai

$destinationPath= public_path() . 'img/';

answer given by @Reflic may br right... but not for me....
this path worked for me.
it may because i removed "public/" from url... thank you.

@Reflic 给出的答案可能是对的……但不适合我……
这条路对我有用。
可能是因为我从 url 中删除了“public/”...谢谢。

回答by blakroku

i had the same problem. I got everything right about my code except for the

我有同样的问题。除了

public_path()

Wasn't prepending to the my folder

没有在我的文件夹之前

$destinationPath= public_path() . '/img/'; // Worked perfect

I also did this to change my file name

我也这样做是为了更改我的文件名

I got the file extension

我得到了文件扩展名

$extension = Input::file('YourFileName')->getClientOriginalExtension();
$filename = WhateverYouWantHere . '.' . $extension;

And that is it, file name changed. Laravel is indeed wonderful

就是这样,文件名改变了。Laravel 确实很棒

回答by Md Shahadat Hossain

you can write relative path also. like as

你也可以写相对路径。像作为

$destinationPath="resources/assets/images/";

$destinationPath="resources/assets/images/";

or

或者

$destinationPath= 'public/img/';

$destinationPath= 'public/img/';