doushi9474 2014-05-28 17:22
浏览 84
已采纳

使用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 2014-05-28 17:28
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器