Как загрузить файл с помощью пакета Laravel?

У меня есть функция прикрепления файлов в пакете Laravel. Я хочу, чтобы загруженное вложение сохранялось в каталоге проекта с использованием пакета и НЕ внутри пакета. В настоящее время файл загружается в каталог пакета 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');
}

person Alec Walczak    schedule 21.01.2016    source источник


Ответы (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";
}
person Alec Walczak    schedule 21.01.2016