Save binary data as raw data (that is, it will not remove metadata or invalid contents)
In this case, the PHP backdoor will be stored on the server
https://base64.guru/developers/php/examples/decode-image
file_put_contents($file, $image_base64);
Hello all, I'm hoping someone can help me with a problem I'm having when trying to upload an image (created from Signature Pad via base64) to another server using FTP. I'm using Laravel 7.0.
The code below works perfectly for storing a signature on my installation of Laravel, I get a nice PNG file in a year/month folder with the signature exactly how it was entered.
So the local part of the code works perfectly.
However, when I use the last line of code to upload the image, it does create a folder (called 'staff-signatures') on the remote server, but doesn't upload the file - it immediately throws an error: 'Failed to load resource: the server responded with a status of 401 (Unauthorized)'.
I've put my FTP details in the filesystems.php file as follows:
'ftp' => [
'driver' => 'ftp',
'host' => env('FTP_HOST'),
'username' => env('FTP_USERNAME'),
'password' => env('FTP_PASSWORD'),
'root' => env('FTP_ROOT'),
'passive' => false,
'port' => 21,
],
All my FTP variables are in the .env file and they definitely work as I am able to make the folder on the other server if it doesn't exist.
I've never stored files off-server via FTP so it could be something really simple but I've been pulling my hair out trying to fix it but with no luck!
Hopefully the comments will help you through the code - does anyone have any ideas why this is happening?
Thanks in advance!
// Grab the signature
$signature = $request->sign;
$first_name = $request->first_name;
$last_name = $request->last_name;
// Set up our directory structure
$directory = 'staff-signatures';
$year = date('Y');
$month = date('m');
$localpath = $directory . '/' . $year . '/' . $month . '/';
// Check folder exists for storing
if (!File::exists($directory . '/' . $year)) {
File::makeDirectory($directory . '/' . $year, 0777, true, true);
}
if (!File::exists($directory . '/' . $year . '/' . $month)) {
File::makeDirectory($directory . '/' . $year . '/' . $month, 0777, true, true);
}
// Prepare signature
$image_parts = explode(";base64,", $request->sign);
$image_type_aux = explode("image/", $image_parts[0]);
$image_type = $image_type_aux[1];
$image_base64 = base64_decode($image_parts[1]);
$uniqueID = uniqid();
$filename = preg_replace(
"/[^a-z0-9\.]/",
"-",
strtolower($first_name . '-' . $last_name . '-' . $uniqueID)
);
// Put it all together
$file = $localpath . $filename . '.' . $image_type;
// Save the file to this laravel installation (works fine - I get my image)
file_put_contents($file, $image_base64);
//
// This doesn't work - I want to connect via FTP and store the above in the relevant folder
//
Storage::disk('ftp')->put('/staff-signatures/' . $filename . '.' . $image_type, $image_base64), 'r+');
Thanks for the replies folks - I looked through all that documentation but still had no luck. Had.
But I've sorted it.
For some reason my local installation (running on VirtualHostX) would create the folder but not the file. When I put it on my non-local development server it worked perfectly.
So, it works as I wanted after all!
Out of interest, anyone have any ideas why my local installation could create the folder but not the file?
Merry Christmas, folks!
Please or to participate in this conversation.