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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题