Domanda

I'm trying to create a QR-code with the php library phpqrcode.

I want to save the file in a temporary file so I can use it afterwards. Something like this.

I'm using the Zend Framework and want to use the temporary directory. This is what I have so far:

require_once 'phpqrcode/qrlib.php';
require_once 'phpqrcode/qrconfig.php';

$tempDir = '/temp';

$fileName = 'test'.'.png';

$pngAbsoluteFilePath = $tempDir . $fileName;
$urlRelativeFilePath = '/temp' . $fileName;

// generating
if (!file_exists($pngAbsoluteFilePath)) {
    QRcode::png('http://mylink.com/s/'.$quiz_url, $pngAbsoluteFilePath, 'L', 4, 2);
    echo 'File generated!';
    echo '<hr />';
} else {
    echo 'File already generated! We can use this cached file to speed up site on common codes!';
    echo '<hr />';
}

echo 'Server PNG File: '.$pngAbsoluteFilePath;
echo '<hr />';

// displaying
echo '<img src="'.$urlRelativeFilePath.'" />';

My output shows:

Server PNG File: /temptest.png

And an image that can't be found. Can somebody help me on the way?

EDIT: When I tried to change the '/temp' to '/temp/' I get this warning:

Warning: imagepng(/temp/test.png): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/surveyanyplace/site/library/phpqrcode/qrimage.php on line 43

SECOND EDIT:
When I checked my hard drive I saw that he just saves images on my root map like 'temptest.png'... How can I make sure he save this on temp folder on my server?

È stato utile?

Soluzione

The image can't be found by your browser because it's looking for it at the URL "http://domain.tld/temptest.png" which relates to location "/Applications/MAMP/htdocs/surveyanyplace/site/public/temptest.png" on your disk and is not where the file was saved (it's "/temptest.png" as you noticed).

In order to directly serve files via your web server (Apache) you have to make sure the image file is under it's DocumentRoot (typically the "public" folder of your Zend Framework application)

One way is to create the following directory : "/Applications/MAMP/htdocs/surveyanyplace/site/public/qrcodecaches" and change $pngAbsoluteFilePath and $urlRelativeFilePath as follows:

$pngAbsoluteFilePath = APPLICATION_PATH . '/../public/qrcodecaches/' . $fileName;
$urlRelativeFilePath = '/qrcodecaches/' . $fileName;

($tempDir can be removed)

NB: You might want to take a look to Zend_View_Helper_BaseUrl to make $urlRelativeFilePath more portable when dealing with applications inside a subdirectory

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top