2016-01-21 109 views
1

我在Laravel包中有一個文件附件功能。我希望上傳的附件使用軟件包中的軟件包和NOT保存在項目目錄中。目前,該文件上傳到包uploads目錄而不是項目uploads目錄。有關如何將此文件保存在正確位置的任何建議?如何使用Laravel包上傳文件?

控制器:

$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'); 
} 

回答

0

HERE WE GO。我使用了(充分記錄的)存儲外觀來處理本地文件系統(項目)。

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"; 
}