2014-05-28 17:22

使用Laravel上传图片 - 上传为文件夹文件


. Hello y'all, I'm trying to enable a user to pick a file, upload it, and then save the path to it in a database.

So far I have gotten the picture to "upload" in that it passes a validator and the path is sent to the database but other than that it is not working.

I can see in my FTP FileZilla that there is something in 'public/uploads/images' named 'cookie_monster_sir.png' (I'm using it as a test picture, don't judge me) BUT for some reason it's saved as a FILE FOLDER! lol. I have no idea how that happened. And then within the cookie_monster_sir.png folder there is another file that is titled 'php' and followed by numbers and letters. I'm guessing somewhere in the upload the file got messed up. I've tried uploading several times but I still just have the same file folder and more files that start with 'php' and are then followed by numbers and letters.

Any explanation or help on how to fix this and allow for functioning upload of a picture would be greatly appreciated! Thanks for your time!

Here is my view:

{{ Form::open(array('route'=>'order-create-post', 'name' => 'orderForm', 'id' => 'orderForm', 'files' => true)) }}

{{ Form::file('drawing_of_job') }}

<div class="row">
    <input type = "submit" name = "submit" value="Create Order" id="submit" class="button radius expand"/>      
{{ Form::close() }}


public function postCreate(){

        $validator = Validator::make(Input::all(),
       'drawing_of_job'     => 'required|image',

            return Redirect::route('order-create')
                ->with('global', 'There are fields with errors.');

        }   else   {

            /*Create Order*/
                $drawing_of_job         = Input::file('drawing_of_job');
                $file_name          = $drawing_of_job->getClientOriginalName();
                $path_to_drawing        = 'uploads/images/' . $file_name;

$order = Order::create(array(
                             'path_to_drawing'      => $path_to_drawing,

                return Redirect::route('home')
                    ->with('global', 'Your order has been created.');
            return Redirect::route('home')
                    ->with('global', 'The order could not be created.');        
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dsfb20227 dsfb20227 7年前

    That's because it's what you're doing :)

    $file_name = $drawing_of_job->getClientOriginalName();
    $path_to_drawing = 'uploads/images/' . $file_name;

    ^__ here you're creating a (for Laravel) path with the file name, say 'uploads/images/cookie_monster_sir.png'; (let's forget for a moment the extension in the filename)


    ^__ here you're moving the file without providing a name, so it's saved as its temp name in the folder you specified. Infact, you should be getting something like:


    Try providing the name:

    $path_to_drawing = 'uploads/images/';
    $drawing_of_job->move($path_to_drawing, $file_name);

    This should be working fine.

    点赞 评论 复制链接分享