riaanv1987's avatar

Please could someone help me with this file upload problem

HI I was just on stack exchange and nearly got devoured by some dude who clearly was not in the mood to help...either way...

This is the code block that I am having problems with....also don't rail on me on how badly written it is. This controller depends on actions of others so all this does the work it is suppose to do.

public function betaling(Request $request) { // Skep 'n nuwe betaling $betaling = new Betaling(); // Blank Array $filenames = []; // Check die files if($request->file('aandeel_pop')) $filenames[] = $request->file('aandeel_pop'); if($request->file('nfc_pop')) $filenames[] = $request->file('nfc_pop'); // Vind die user $user = Auth::user();

    foreach($filenames as $file) {
        if(!empty($file)){
            $filename = time() . $file->getClientOriginalName();
            $file->move(storage_path('/app/public/pop/uploads/'), $filename);
            $filenames[] = $filename;
        }
    }
    // Set die waardes vir die betaling
    $betaling->trans_nommer = $request->trans_nommer;
    $betaling->user_id = $user->lid_nommer;
    $betaling->aandeel_pop = $filenames[2];
    $betaling->nfc_pop = $filenames[3];
    $betaling->uploaded = Carbon::now();
    $betaling->save();

    // Update die transaksie
    $transaksie = Transaksie::where('trans_nommer', $request->trans_nommer)->firstOrFail();
    $transaksie->status_id = 6;
    $transaksie->is_paid = 1;
    $transaksie->aandeel_pop = $filenames[2];
    $transaksie->nfc_pop = $filenames[3];
    $transaksie->save();

    $transaksie->user->notify(new BetalingGelaai($transaksie));

    // Merk die sertifikaat as verkoop
    if($request->sert_nommer > 1003333 ){
        $sertifikaat = Alternate::where('sert_nommer', $request->sert_nommer)->firstOrFail();
        if($sertifikaat){
            $sertifikaat->is_paid = 4;
            $sertifikaat->save();
        }
    }elseif($request->sert_nommer < 1003604) {
        $sertifikaat = Share::where('sert_nommer', $request->sert_nommer)->firstOrFail();
        if($sertifikaat){
            $sertifikaat->is_paid = 4;
            $sertifikaat->save();
        }
    }elseif($request->sert_nommer < 50000) {
        $sertifikaat = Aandeel::where('sert_nommer', $request->sert_nommer)->firstOrFail();
        if($sertifikaat){
            $sertifikaat->is_paid = 4;
            $sertifikaat->save();
        }
    }
    return redirect()->back()->with('success','Betaling was suksesvol geplaas!');
}

The problem that i am having is that the file upload work for some users and other users get a 500 when posting the files. I have two upload fields on the form and when i press upload it gives 500. On the other hand when i start uploading from the bottom to the top it works. Can someone please shed some light on this for me

0 likes
7 replies
lostdreamer_nl's avatar

Looking at your code, I would figure that it will only work when you upload BOTH files, and will fail every time you upload 1 single file, is that correct ?

It's because of this part:

    $filenames = []; 
    // Check die files 
    if($request->file('aandeel_pop')) 
        $filenames[] = $request->file('aandeel_pop'); 
    
    if($request->file('nfc_pop')) 
        $filenames[] = $request->file('nfc_pop'); // Vind die user $user = Auth::user();

    foreach($filenames as $file) {
        if(!empty($file)){
            $filename = time() . $file->getClientOriginalName();
            $file->move(storage_path('/app/public/pop/uploads/'), $filename);
            $filenames[] = $filename;
        }
    }

You are reusing that $filenames array for both the files, and their filenames after uploading.

And a few lines down:

    $transaksie->aandeel_pop = $filenames[2];
    $transaksie->nfc_pop = $filenames[3];

You now expect both files to occupy index 2 and 3 (but if you only uploaded 1 file, the $filenames array would only have 2 values, key 0 = the uploaded file, and key 1 = the filename after upload.)

Refactor this part into:

    $files = []; 
    $filenames = []; 
    // Check die files 
    if($request->file('aandeel_pop')) 
        $files ['aandeel_pop'] = $request->file('aandeel_pop'); 

    if($request->file('nfc_pop')) 
        $files ['nfc_pop'] = $request->file('nfc_pop'); // Vind die user $user = Auth::user();


    foreach($files as $type => $file) {
        if(!empty($file)){
            $filename = time() . $file->getClientOriginalName();
            $file->move(storage_path('/app/public/pop/uploads/'), $filename);
            $filenames[$type] = $filename;
        }
    }

and the second part:

    if(!empty($filenames)) {
        $betaling->trans_nommer = $request->trans_nommer;
        $betaling->user_id = $user->lid_nommer;
        if(isset($filenames['aandeel_pop'])
            $betaling->aandeel_pop = $filenames['aandeel_pop'];
        if(isset($filenames['nfc_pop'])
            $betaling->nfc_pop = $filenames['nfc_pop'];
        $betaling->uploaded = Carbon::now();
        $betaling->save();
    }

Now it will only grab the files that have actually been uploaded. (Make srure that $betaling could be saved without those files, or that you have your upload form (and validation) make sure the files are being uploaded.

ps. Is this African ? Looks so much like my native dutch, Makes me think 'Die Antwoord' has been coding :)

riaanv1987's avatar

@LOSTDREAMER_NL - Yes I ilve in south africa hehehe thanks for the reply, Let me see if this resolves my issue and I will report back. Dankie vir jou tyd :-)

riaanv1987's avatar

@LOSTDREAMER_NL - Also i tested the code still the same problem! It happens even if both files are selected and when i press upload it goes and goes and then 500:

Here is the stacktrace after implementing your changes @lostdreamer_nl

[2019-03-06 16:14:57] local.ERROR: syntax error, unexpected '$betaling' (T_VARIABLE) {"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): syntax error, unexpected '$betaling' (T_VARIABLE) at /home/ndo0ayouz18t/eureka/app/Http/Controllers/BetalingsController.php:49) [stacktrace]

Tangente's avatar

@riaanv1987 you getting that error after adding his changes, syntax error, because he forgot a parentheses here

if(isset($filenames['aandeel_pop'])

Change that to

if(isset($filenames['aandeel_pop']))

lostdreamer_nl's avatar

Tangente is correct:

// there is a bug here
        if(isset($filenames['aandeel_pop'])
            $betaling->aandeel_pop = $filenames['aandeel_pop'];
        if(isset($filenames['nfc_pop'])

// it should be

        if(isset($filenames['aandeel_pop']))
            $betaling->aandeel_pop = $filenames['aandeel_pop'];
        if(isset($filenames['nfc_pop']))

Cronix's avatar

Side note not having to do with the stated issue - I don't see how it would ever enter this block

}elseif($request->sert_nommer < 50000) {

because the previous block also covers that condition

}elseif($request->sert_nommer < 1003604) {
lostdreamer_nl's avatar

Indeed, and also a bit off over here:

sert_nommer > 1003333 

sert_nommer < 1003604

sert_nommer < 50000

What should happen with sert_nommer = 1003500 ? It's going into the first block, but could also go into the second block.

The third block wont ever be hit....

Please or to participate in this conversation.