lundi 25 février 2019

Laravel React Uploading Files within a Form

So I am trying to implement an upload page within my app and in that page, you can have both an image and a song uploaded as well as other things (genre, description, etc). I managed to get the image to work using image intervention but i am trying to get my mp3/wav file (using a base64 decode function) to be read as a file so I can use the move method to store it in a local folder (valid upload) and I don't understand why laravel is reading it as a string. Is there something i'm missing?

the decoding function

function base64_to_song($base64_string, $output_file) {
    // open the output file for writing
    $ifp = fopen( $output_file, 'wb' ); 

// split the string on commas
// $data[ 0 ] == "data:image/png;base64"
// $data[ 1 ] == <actual base64 string>
$data = explode( ',', $base64_string );

// we could add validation here with ensuring count( $data ) > 1
fwrite( $ifp, base64_decode($data[1]));

// clean up the file resource
fclose( $ifp ); 

return $output_file; 
}

In my controller:

$songextension = $request->get('songextension');
$file = $request->json()->get('song_file');
$extension = $songextension; 
$filename = rand(11111,99999).'.'.$extension;
$newfile = base64_to_song($file, $filename);
$destinationPath="uploads/music";
$newfile->move($destinationPath, $filename);

It just keeps reading $newfile as a string rather than the output



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire