简体   繁体   中英

How to save QR code image to server folder once generated ? - 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM