Laravel 5.5项目:如何将后端生成的二维码图片存储到托管服务器?
Hey there! Let's get that QR code saved to your hosting server instead of just holding it as a Base64 string. I'll walk you through modifying your existing code to work with Laravel's built-in storage system—clean, maintainable, and perfect for your use case.
First, Adjust Your CreateQr Method
Your current code generates a Base64 string, but we can tweak it to save the actual PNG file directly to your server's storage (and still keep the Base64 if you need it for frontend display). Here's the updated version:
public function CreateQr($data) { // Encrypt your data as before $enc = (new CryptoController())->Encrypt($data); // Get the logged-in user ID (swap to Laravel's Auth helper for better practice) $userID = auth()->id() ?? $_SESSION['currentUserID']; // Create a unique filename to avoid overwrites (user ID + timestamp) $filename = 'user_' . $userID . '_' . time() . '.png'; $storagePath = 'qrcodes/' . $filename; // Generate and save the QR code directly to storage QrCode::format('png')->size(250)->generate($enc, storage_path('app/public/' . $storagePath)); // Optional: Keep the Base64 string if you need it for frontend rendering $base64Src = base64_encode(file_get_contents(storage_path('app/public/' . $storagePath))); // Get the public URL to access the QR code (works after setting up the storage link) $publicUrl = asset('storage/' . $storagePath); // Return whatever data your app needs return [ 'base64' => $base64Src, 'storage_path' => storage_path('app/public/' . $storagePath), 'public_url' => $publicUrl ]; }
Critical Setup Steps
Create the Public Storage Link: Laravel stores public-access files in
storage/app/public/, but they need a symbolic link to yourpublic/directory to be web-accessible. Run this Artisan command once on your server:php artisan storage:linkThis creates a link from
public/storagetostorage/app/public/, so your QR codes will be reachable at URLs likeyourdomain.com/storage/qrcodes/user_123_1699999999.png.Check Directory Permissions: Make sure the
storage/app/public/qrcodesdirectory exists and has write permissions. You can auto-create it with this code (importIlluminate\Support\Facades\Filefirst):if (!File::exists(storage_path('app/public/qrcodes'))) { File::makeDirectory(storage_path('app/public/qrcodes'), 0755, true); }Use Laravel's Auth (Optional): I swapped
$_SESSION['currentUserID']withauth()->id()because Laravel's built-in auth is more secure and integrates better with the framework. Stick with your session call if you're not using Laravel's Auth system.
Alternative: Use Laravel's Storage Facade
For future flexibility (like switching to cloud storage later), use Laravel's Storage facade instead of direct path writes:
// Generate QR code content as a string $qrCodeContent = QrCode::format('png')->size(250)->generate($enc); // Save to the public disk Storage::disk('public')->put($storagePath, $qrCodeContent);
This follows Laravel's best practices and keeps your storage logic consistent.
That's it! Your QR codes will now be saved to your hosting server, and you can access them via public URLs or use the Base64 string wherever you need it.
内容的提问来源于stack exchange,提问作者red xx

