doushi9474
doushi9474
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"/>      
</div>
{{ Form::close() }}

Controller:

public function postCreate(){

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

        if($validator->fails()){
            return Redirect::route('order-create')
                ->withErrors($validator)
                ->withInput()
                ->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;
                $drawing_of_job->move($path_to_drawing);

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

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

1条回答

  • 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)

    $drawing_of_job->move($path_to_drawing);
    

    ^__ 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:

    'uploads/images/cookie_monster_sir.png/yancalicgday';
    

    Try providing the name:

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

    This should be working fine.

    点赞 评论 复制链接分享

相关推荐