如何使用Laravel包上传文件?

时间:2016-01-21 17:43:53

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

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

服务提供商(我认为发布uploads文件夹可能有效 - 不。)

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

1 个答案:

答案 0 :(得分:0)

我们走了。我使用(详细记录的)存储外观来处理本地文件系统(项目)。

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

% Create the warning dialog figure and get its handle
h=warndlg('Try 1.  Click OK to continue')
% Find the warning dialog figure pushbutton handle
wh=findobj(h,'style','pushbutton')
% Set the pushbutton background color to "dark gray"
set(wh,'BackgroundColor',[.5 .5 .5])
% Set the pushbutton foreground color to "white"
set(wh,'foregroundColor',[1 1 1])
% Call uiwait for the warning dialog figur
uiwait(h)