douju8113 2018-08-21 20:20
浏览 191

生成后如何将QR码图像保存到服务器文件夹? - PHP

I am trying to generate QR code for each users who signup for my website. I am going to send them QR code in email and they can use that QR code for sign-in. Doing some research work, I found phpqrcode. I tried the following code of lines, which is saving the name of the QR code image in the DB, but is not saving its image to the folder. How can I save the QR image into any folder(say for example: Uploads). SO once image is uploaded into the folder, I can easily send that image to the user.

So on submitting the register form, the following controller will be called and will save user as well as generate unique QR code for that user.

signupController.php

$user = \Model\User::loadFromPost();

if($user->save()){

    $tempDir = $_SERVER['SERVER_NAME'].UPLOAD_PATH . 'qrcodes/';
    $codeContents = $user->email;
    $fileName = $user->id.md5($codeContents).'.png';
    $pngAbsoluteFilePath = $tempDir.$fileName; 
    if (!file_exists($pngAbsoluteFilePath)) { 
        QRcode::png($codeContents, $pngAbsoluteFilePath); 
        $user->qrcode = $fileName;
        $user->save();
    } else { 
        echo 'File already generated! We can use this cached file to speed up site on common codes!'; 
        echo '<hr />'; 
    }
}

Here $_SERVER['SERVER_NAME'] is example.local/ (for Local) and https://www.example.com/ (for live server) and UPLOAD_PATH is contents/images/uploads. The above code, saves the name of QR code image file in DB but not saving the image of QR into the uploads folder.

Help is appreciated. Thanks.

  • 写回答

1条回答 默认 最新

  • dongxie9169 2018-08-21 20:30
    关注

    You cannot have domain as your absolute path. The absolut path has to be path on your server. So, replace the

    $tempDir = $_SERVER['SERVER_NAME'].UPLOAD_PATH . 'qrcodes/';
    

    with path in format such as

    $tempDir = __DIR__.DIR_SEPARATOR.UPLOAD_PATH.DIR_SEPARATOR.'qrcodes/';
    

    Of course, you need to modify the path according to your server setup but the __ DIR __ contains the path to the directory of the current file.

    If you can specify the framework you are working on, I can help you modifying the path.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化