如何在Laravel 4中更新上传的文件

时间:2014-11-13 20:42:03

标签: laravel-4

public function update($id)
    {
        $validation = MarketingMaterial::validator_edit(Input::all());

        if ($validation->fails()) {

            return Redirect::to('marketing_materials/'. $id . '/edit')->withErrors($validation);

        } else {


            $marketing_material                                  = MarketingMaterial::findOrFail($id);

            $old_title = $marketing_material->title;


            $marketing_material->title                           = Input::get('title');
            $marketing_material->description                     = Input::get('description');
            $marketing_material->marketing_materials_category_id = Input::get('marketing_materials_category_id');


            $thumb_path = Input::file('thumb_path');

            if (Input::hasFile('thumb_path'))
            {

                $file            = Input::file('thumb_path');
                $destinationPath = base_path().'/app/files/marketing_materials/'.$marketing_material->title.'/thumb_path/';
                $filename        = $file->getClientOriginalName();
                $extention       = $file->getClientOriginalExtension();
                $filesize        = $file->getSize();
                $uploadSuccess   = $file->move($destinationPath, $filename);


                $marketing_material->thumb_path = $filename;
            }


            $media_path = Input::file('media_path');

            if (Input::hasFile('media_path'))
            {

                $file            = Input::file('media_path');
                $destinationPath = base_path().'/app/files/marketing_materials/'.$marketing_material->title.'/media_path/';
                $filename        = $file->getClientOriginalName();
                $extention       = $file->getClientOriginalExtension();
                $filesize        = $file->getSize();
                $uploadSuccess   = $file->move($destinationPath, $filename);


                $marketing_material->media_path = $filename;
                $marketing_material->media_size = $filesize;
            }



            dd($old_title);

            $marketing_material->save();


            return Redirect::to('marketing_materials')
            ->with('success','The electronic marketing material was updated succesfully!');
        }
    }

目标:

能够编辑上传的文件(已上传的文件)。

控制器 - >更新功能

正如你所看到的,我依靠我的目标路径,在我的标题上,一旦用户更改标题,一切都会搞砸。

我不确定如何解决这个问题。

问题:

有人可以告诉我如何在Laravel 4中解决这个问题,还是指向正确的方向?

感谢。

截图

My Form

Error

1 个答案:

答案 0 :(得分:0)

    public function update($id)
{
    $validator = CatalogDownload::edit_validator(Input::all());

    if ($validator->fails()) {
        return Redirect::to('catalog_downloads/'. $id . '/edit')
        ->withErrors($validator);

    }else {


        $catalog_download        = CatalogDownload::findOrFail($id);
        $catalog_download->title = Input::get('title');
        $catalog_download->note  = Input::get('note');

        $catalog_download->save();





        foreach (ExportType::all() as $export_type)
        {




            if (Input::hasFile('type_' . $export_type->id))
            {



                $success = File::deleteDirectory(base_path().'/app/files/product_export/'. $catalog_download->id.'/'. $export_type->id.'/'); // Answer





                $product_export  = new ProductExport;

                $zip             = Input::file('type_' . $export_type->id);
                $filename        = $zip->getClientOriginalName();
                $filesize        = $zip->getSize();

                $destinationPath = base_path().'/app/files/product_export/'. $catalog_download->id.'/'. $export_type->id.'/';
                $uploadSuccess   = $zip->move($destinationPath, 'Bioss 2015 Catalog.zip' );




                if  (!isset($uploadSuccess )) 
                {
                    return Redirect::to('catalog_downloads/create')
                    ->with('error', 'Problem during upload')
                    ->withInput();          
                }


                $product_export->file_path           =  'Bioss 2015 Catalog.zip';

                $product_export ->exported_date      = DateHelper::getDateFormatMySQL(Input::get('exported_date'));
                $product_export->catalog_download_id = $catalog_download->id;
                $product_export->export_type_id      = $export_type->id;




                $product_export->size                = $filesize;




                $product_export->save();
            }


        }


        return Redirect::to('catalog_downloads/')
        ->with('success','The catalog_download was updated successfully!');

    }
}

上面的代码就是它的解决方案。

魔术发生在这一行:

$success = File::deleteDirectory(base_path().'/app/files/product_export/'. $catalog_download->id.'/'. $export_type->id.'/');

<强>解决方案

我删除了旧文件,然后重新创建新文件。