简体   繁体   English

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

[英]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 .做了一些研究工作,我找到了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).如何将 QR 图像保存到任何文件夹中(例如:上传)。 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注册控制器.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 .这里$_SERVER['SERVER_NAME']example.local/ (对于本地)和https://www.example.com/ (对于实时服务器)和UPLOAD_PATHcontents/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.上面的代码,将二维码图片文件的名称保存在DB中,但没有将二维码图片保存到uploads文件夹中。

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.当然,您需要根据您的服务器设置修改路径,但 __ DIR __ 包含当前文件目录的路径。

If you can specify the framework you are working on, I can help you modifying the path.如果你能指定你正在处理的框架,我可以帮你修改路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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