0

I have code below and instead of saving files it saves temporary files in location

Code

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

two

what saved in my app location

one

What I need

Is to save real file translator_translations - 2019-04-15 05:52:50.csv and not tmp file.

any idea?

2
  • use put method like Storage::disk('local')->put($myfilename, $filedata); Apr 15, 2019 at 6:37
  • Pretty sure the , should be a . (full stop/period) in $location = storage_path('app/feeds/translations/', $new_filename);
    – Rwd
    Apr 15, 2019 at 6:39

2 Answers 2

1

You need to pass filename into the move() function instead of any other otherwise it will take temporary name.

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

Notes :

You should use proper filename. Filename should not contain any special character like spaces, comma and colon (:).

5
  • it says: 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
    Apr 15, 2019 at 6:30
  • @mafortis Can you please use proper file name convention as per laravel? Translation_Template - 15-04-2019 06_30_18 - 2019-04-15 06:29:37.csv this is not proper file name. It should be Translation_Template_15-04-2019_06_30_18_2019-04-15_06_29_37.csv. You need to remove spaces,-,: from filename please. Apr 15, 2019 at 6:33
  • I changed my date part to $uploaded_date = now()->format('Y-m-d_H_m_s'); and it's uploading now, quick question? is there anyway i get my file name as i was? because i need that name config for another function which runs after uploading file, if i can't get names like that then i have to change lots of codes again :/
    – mafortis
    Apr 15, 2019 at 6:45
  • You can add another field into the database like original_name in that you can store the original filename. Then you can use that name to compare and get the files according to the filename. Apr 15, 2019 at 6:50
  • @mafortis if you satisfy with the answer and it resolved your issue as per question then please give upvote and accept it as answer please. Thank you! :) Apr 15, 2019 at 6:55
0

You Wrong Approach Use move function supply two parameters old directory to new directory You remove this line $file->move($location); and use $file->storeAs( $location, $new_filename );

5
  • It says: Impossible to create the root directory "C:\laragon\www\....\storage\/app\C:/laragon/www/.../storage/app/feeds/translations".
    – mafortis
    Apr 15, 2019 at 6:20
  • $file->storeAs( 'app/feeds/translations/', $new_filename ); Use This Apr 15, 2019 at 6:28
  • now syay: 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
    – mafortis
    Apr 15, 2019 at 6:32
  • i Know your directory $file->storeAs( storage_path('app/feeds/translations/'), $new_filename ); Use this I hope Work Fine Apr 15, 2019 at 6:39
  • try with this storage_path('feeds/translations/', $new_filename); Apr 15, 2019 at 6:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.