【问题标题】:How Do I Upload a File using a Laravel Package?如何使用 Laravel 包上传文件?
【发布时间】:2016-01-22 02:39:18
【问题描述】:

我在 Laravel 包中有一个文件附件功能。我希望上传的附件使用包保存在项目目录中,而NOT在包中。目前,文件上传到包uploads目录而不是项目uploads目录。有关如何将此文件保存在正确位置的任何建议?

控制器:

$attachment->spot_buy_item_id = $id;
$attachment->name = $_FILES['uploadedfile']['name'];
$attachment->created_ts = Carbon::now();

$ds          = DIRECTORY_SEPARATOR;  //1

$storeFolder = '../../../resources/uploads';

if (!empty($_FILES)) {

    $tempFile = $_FILES['uploadedfile']['tmp_name'];          //3

    $extension = pathinfo($_FILES['uploadedfile']['name'], PATHINFO_EXTENSION);

    $attachment_hash = md5($tempFile);

    $new = $attachment_hash.'.'.$extension;

    // complete creating attachment object with newly created attachment_hash
    $attachment->hash = $new;
    $attachment->save();

    $targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;  //4

    $targetFile =  $targetPath. $new;  //5

    move_uploaded_file($tempFile,$targetFile); //6

    chmod($targetFile, 0777);

}
else
{
    return "error";
}

服务提供者(我认为发布上传文件夹可能会起作用 - 不。)

public function boot()
{
    require __DIR__ . '/Http/routes.php';

    $this->loadViewsFrom(__DIR__ . '/resources/views', 'ariel');

    $this->publishes([
        __DIR__ . '/resources/uploads' => public_path('vendor/uploads'),
    ], 'public');

    $this->publishes([
        __DIR__ . '/../database/migrations' => database_path('migrations')], 'migrations');
}

【问题讨论】:

    标签: php laravel file-upload package laravel-5.1


    【解决方案1】:

    我们开始吧。我使用(有据可查的)存储门面来处理本地文件系统(项目)。

    https://laravel.com/docs/5.1/filesystem

    $attachment->spot_buy_item_id = $id;
    $attachment->name = $_FILES['uploadedfile']['name'];
    $attachment->created_ts = Carbon::now();
    
    $ds          = DIRECTORY_SEPARATOR;  //1
    
    $storeFolder = 'uploads';
    
    if (!empty($_FILES)) {
    
        $tempFile = $_FILES['uploadedfile']['tmp_name'];          //3
    
        $extension = pathinfo($_FILES['uploadedfile']['name'], PATHINFO_EXTENSION);
    
        $attachment_hash = md5($tempFile);
    
        $new = $attachment_hash.'.'.$extension;
    
        // complete creating attachment object with newly created attachment_hash
        $attachment->hash = $new;
        $attachment->save();
    
        $tmpPath = $storeFolder . $ds;
    
        $targetFile =  $tmpPath . $new;  //5
    
        Storage::disk('local')->put($targetFile, file_get_contents($request->file('uploadedfile')));
    
    }
    else
    {
        return "error";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-09
      • 2013-12-15
      • 2019-10-10
      • 1970-01-01
      • 2018-09-19
      • 2017-05-05
      • 2018-10-25
      • 2019-03-18
      相关资源
      最近更新 更多