【问题标题】:Laravel stores temporary files insteadLaravel 存储临时文件
【发布时间】:2019-04-15 14:39:22
【问题描述】:

我有下面的代码,而不是保存文件,而是将临时文件保存在位置

代码

public function upload(Request $request)
{
    //validate the file
    if (!$request->hasFile('file_to_import')) {
        return new JsonResponse([
            'status'  => 'fail',
            'message' => trans('json.needSelectFile'),
        ]);
    }

    $file = $request->file('file_to_import');

    if (strtolower($file->getClientOriginalExtension()) != 'csv') {
        return new JsonResponse([
            'status'  => 'fail',
            'message' => trans('json.needValidCSV'),
        ]);
    }

    $disk = Storage::disk('local');

    if (!$disk->exists('feeds/translations')) {
        $disk->makeDirectory('feeds/translations');
    }

    $uploaded_date = now();
    $name_without_extension = str_replace('.' . $file->getClientOriginalExtension(), '', $file->getClientOriginalName());

    $new_filename = $name_without_extension . ' - ' . $uploaded_date . '.' . $file->getClientOriginalExtension();
    $location = storage_path('app/feeds/translations/', $new_filename);
    $file->move($location);

    return new JsonResponse([
        'status'   => 'success',
        'filename' => $new_filename,
    ]);
}

file i uploaded

what saved in my app location

我需要什么

是保存真实文件translator_translations - 2019-04-15 05:52:50.csv而不是tmp文件。

有什么想法吗?

【问题讨论】:

  • 使用 put 方法,例如 Storage::disk('local')->put($myfilename, $filedata);
  • 很确定, 应该是$location = storage_path('app/feeds/translations/', $new_filename); 中的.(句号/句号)

标签: php laravel


【解决方案1】:

您需要将文件名传递给move() 函数而不是任何其他函数,否则它将采用临时名称。

$file->move($destinationPath,$filename);

注意事项:

您应该使用正确的文件名。文件名不应包含任何特殊字符,如空格、逗号和冒号 (:)。

【讨论】:

  • 上面写着:Could not move the file "C:\Users\Theo\AppData\Local\Temp\phpBF29.tmp" to "C:\laragon\www\.....\storage\app/feeds/translations\Translation_Template - 15-04-2019 06_30_18 - 2019-04-15 06:29:37.csv" (move_uploaded_file(): Unable to move 'C:\Users\Theo\AppData\Local\Temp\phpBF29.tmp' to 'C:\laragon\www\pursury\storage\app/feeds/translations\Translation_Template - 15-04-2019 06_30_18 - 2019-04-15 06:29:37.csv')
  • @mafortis 你能按照 laravel 使用正确的文件名约定吗? Translation_Template - 15-04-2019 06_30_18 - 2019-04-15 06:29:37.csv 这不是正确的文件名。应该是Translation_Template_15-04-2019_06_30_18_2019-04-15_06_29_37.csv。您需要从文件名中删除spaces,-,:
  • 我将日期部分更改为$uploaded_date = now()->format('Y-m-d_H_m_s');,现在正在上传,快速提问?反正我有我的文件名吗?因为我需要该名称配置用于上传文件后运行的另一个函数,如果我无法获得这样的名称,那么我必须再次更改大量代码:/
  • 您可以将另一个字段添加到数据库中,例如original_name,您可以存储原始文件名。然后就可以使用该名称根据文件名比较和获取文件了。
  • @mafortis 如果您对答案感到满意并且它根据问题解决了您的问题,那么请放弃投票并接受它作为答案。谢谢! :)
【解决方案2】:

你错误的方法使用移动功能提供两个参数旧目录到新目录你删除这行$file->move($location);并使用$file->storeAs( $location, $new_filename );

【讨论】:

  • 上面写着:Impossible to create the root directory "C:\laragon\www\....\storage\/app\C:/laragon/www/.../storage/app/feeds/translations".
  • $file->storeAs( 'app/feeds/translations/', $new_filename );使用这个
  • 现在说:fopen(C:\laragon\www\....\storage\/app\app/feeds/translations/translator_translations - 2019-04-15 06:32:09.csv): failed to open stream: No such file or directory
  • 我知道你的目录 $file->storeAs( storage_path('app/feeds/translations/'), $new_filename );使用这个我希望工作正常
  • 试试这个 storage_path('feeds/translations/', $new_filename);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-16
  • 2011-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多