You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 5.5项目:如何将后端生成的二维码图片存储到托管服务器?

Saving Generated QR Codes to Your Hosting Server in 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 your public/ directory to be web-accessible. Run this Artisan command once on your server:

    php artisan storage:link
    

    This creates a link from public/storage to storage/app/public/, so your QR codes will be reachable at URLs like yourdomain.com/storage/qrcodes/user_123_1699999999.png.

  • Check Directory Permissions: Make sure the storage/app/public/qrcodes directory exists and has write permissions. You can auto-create it with this code (import Illuminate\Support\Facades\File first):

    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'] with auth()->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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:33:48