[英]Codeigniter Linux Failed to open stream: Permission denied on creating image file/qr code in 777 mode temp directory
Please help me.请帮我。 I am trying to create a temporary png image file/qr code in
/var/www/html/temp
folder using phpqrcode library by embedding an image from /var/www/assets/images
.我正在尝试通过嵌入来自
/var/www/assets/images
的图像,使用 phpqrcode 库在/var/www/html/temp
文件夹中创建一个临时 png 图像文件/二维码。 But it keeps showing error like this:但是它一直显示这样的错误:
A PHP Error was encountered</h4>
<p>Severity: Warning</p>
<p>Message: imagepng(/var/www/html/temp/10012023_053321_pm_qrcode.png): failed to open stream: Permission denied</p>
<p>Filename: phpqrcode/qrimage.php</p>
<p>Line Number: 43</p>
<p>Backtrace:</p>
<p style="margin-left:10px">
File: /var/www/html/application/libraries/phpqrcode/qrimage.php<br />
Line: 43<br />
Function: imagepng </p>
<p style="margin-left:10px">
File: /var/www/html/application/libraries/phpqrcode/qrencode.php<br />
Line: 494<br />
Function: png </p>
<p style="margin-left:10px">
File: /var/www/html/application/libraries/phpqrcode/qrencode.php<br />
Line: 286<br />
Function: encodePNG </p>
<p style="margin-left:10px">
File: /var/www/html/application/helpers/portalsdm_helper.php<br />
Line: 145<br />
Function: png </p>
<p style="margin-left:10px">
File: /var/www/html/application/controllers/Sestama.php<br />
Line: 465<br />
Function: create_qr </p>
<p style="margin-left:10px">
File: /var/www/html/index.php<br />
Line: 315<br />
Function: require_once </p>
</div>
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>A PHP Error was encountered</h4>
<p>Severity: Warning</p>
<p>Message: imagecreatefrompng(/var/www/html/temp/10012023_053321_pm_qrcode.png): failed to open stream: No such file or directory</p>
<p>Filename: helpers/portalsdm_helper.php</p>
<p>Line Number: 149</p>
<p>Backtrace:</p>
<p style="margin-left:10px">
File: /var/www/html/application/helpers/portalsdm_helper.php<br />
Line: 149<br />
Function: imagecreatefrompng </p>
<p style="margin-left:10px">
File: /var/www/html/application/controllers/Sestama.php<br />
Line: 465<br />
Function: create_qr </p>
<p style="margin-left:10px">
File: /var/www/html/index.php<br />
Line: 315<br />
Function: require_once </p>
</div>
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>A PHP Error was encountered</h4>
<p>Severity: Warning</p>
<p>Message: imagesx() expects parameter 1 to be resource, bool given</p>
<p>Filename: helpers/portalsdm_helper.php</p>
<p>Line Number: 158</p>
<p>Backtrace:</p>
<p style="margin-left:10px">
File: /var/www/html/application/helpers/portalsdm_helper.php<br />
Line: 158<br />
Function: imagesx </p>
<p style="margin-left:10px">
File: /var/www/html/application/controllers/Sestama.php<br />
Line: 465<br />
Function: create_qr </p>
<p style="margin-left:10px">
File: /var/www/html/index.php<br />
Line: 315<br />
Function: require_once </p>
</div>
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>A PHP Error was encountered</h4>
<p>Severity: Warning</p>
<p>Message: imagesy() expects parameter 1 to be resource, bool given</p>
<p>Filename: helpers/portalsdm_helper.php</p>
<p>Line Number: 159</p>
<p>Backtrace:</p>
<p style="margin-left:10px">
File: /var/www/html/application/helpers/portalsdm_helper.php<br />
Line: 159<br />
Function: imagesy </p>
<p style="margin-left:10px">
File: /var/www/html/application/controllers/Sestama.php<br />
Line: 465<br />
Function: create_qr </p>
<p style="margin-left:10px">
File: /var/www/html/index.php<br />
Line: 315<br />
Function: require_once </p>
</div>
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>A PHP Error was encountered</h4>
<p>Severity: Warning</p>
<p>Message: Division by zero</p>
<p>Filename: helpers/portalsdm_helper.php</p>
<p>Line Number: 166</p>
<p>Backtrace:</p>
<p style="margin-left:10px">
File: /var/www/html/application/helpers/portalsdm_helper.php<br />
Line: 166<br />
Function: _error_handler </p>
<p style="margin-left:10px">
File: /var/www/html/application/controllers/Sestama.php<br />
Line: 465<br />
Function: create_qr </p>
<p style="margin-left:10px">
File: /var/www/html/index.php<br />
Line: 315<br />
Function: require_once </p>
</div>
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>A PHP Error was encountered</h4>
<p>Severity: Warning</p>
<p>Message: imagecopyresampled() expects parameter 1 to be resource, bool given</p>
<p>Filename: helpers/portalsdm_helper.php</p>
<p>Line Number: 169</p>
<p>Backtrace:</p>
<p style="margin-left:10px">
File: /var/www/html/application/helpers/portalsdm_helper.php<br />
Line: 169<br />
Function: imagecopyresampled </p>
<p style="margin-left:10px">
File: /var/www/html/application/controllers/Sestama.php<br />
Line: 465<br />
Function: create_qr </p>
<p style="margin-left:10px">
File: /var/www/html/index.php<br />
Line: 315<br />
Function: require_once </p>
</div>
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>A PHP Error was encountered</h4>
<p>Severity: Warning</p>
<p>Message: imagepng() expects parameter 1 to be resource, bool given</p>
<p>Filename: helpers/portalsdm_helper.php</p>
<p>Line Number: 172</p>
<p>Backtrace:</p>
<p style="margin-left:10px">
File: /var/www/html/application/helpers/portalsdm_helper.php<br />
Line: 172<br />
Function: imagepng </p>
<p style="margin-left:10px">
File: /var/www/html/application/controllers/Sestama.php<br />
Line: 465<br />
Function: create_qr </p>
<p style="margin-left:10px">
File: /var/www/html/index.php<br />
Line: 315<br />
Function: require_once </p>
I already set the /var/www/html/temp and /var/www/assets/images directory mode to 777 and set the owner and its group to apache. If it helps, i am using Codeigniter 3, Linux Centos 9 and Apache. Also this is the actual script that cause the error above:我已经将 /var/www/html/temp 和 /var/www/assets/images 目录模式设置为 777,并将所有者及其组设置为 apache。如果有帮助,我正在使用 Codeigniter 3、Linux Centos 9 和 Apache .这也是导致上述错误的实际脚本:
require_once APPPATH . "libraries/phpqrcode/qrlib.php";
$tempdir = FCPATH . "temp/"; //Nama folder tempat menyimpan file qrcode
if (!file_exists($tempdir)) //Buat folder bername temp
mkdir($tempdir);
//ambil logo
$logopath = base_url() . "assets/images/Logo-BMKG-new-242x300.png";
//isi qrcode jika di scan
$codeContents = $content;
$filename = date('dmY_his_a', time()) . '_' . $suffix . '.png';
//simpan file qrcode
QRcode::png($codeContents, $tempdir . strval($filename), QR_ECLEVEL_H, 10, 4);
// ambil file qrcode
$QR = imagecreatefrompng($tempdir . $filename);
// memulai menggambar logo dalam file qrcode
$logo = imagecreatefromstring(file_get_contents($logopath));
imagecolortransparent($logo, imagecolorallocatealpha($logo, 0, 0, 0, 127));
imagealphablending($logo, false);
imagesavealpha($logo, true);
$QR_width = imagesx($QR);
$QR_height = imagesy($QR);
$logo_width = imagesx($logo);
$logo_height = imagesy($logo);
// Scale logo to fit in the QR Code
$logo_qr_width = $QR_width / 8;
$scale = $logo_width / $logo_qr_width;
$logo_qr_height = $logo_height / $scale;
imagecopyresampled($QR, $logo, $QR_width / 2.3, $QR_height / 2.3, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
// Simpan kode QR lagi, dengan logo di atasnya
imagepng($QR, $tempdir . $filename);
return [base_url() . '/' . $tempdir . $filename, $tempdir . $filename];
The $content
is FCPATH. "/uploads/mutasi/beritaacara/". $data['title_pdf']
$content
是FCPATH. "/uploads/mutasi/beritaacara/". $data['title_pdf']
FCPATH. "/uploads/mutasi/beritaacara/". $data['title_pdf']
FCPATH. "/uploads/mutasi/beritaacara/". $data['title_pdf']
. FCPATH. "/uploads/mutasi/beritaacara/". $data['title_pdf']
. Please help me what did i do wrong?请帮帮我我做错了什么? How do i make it work?
我如何让它工作? It said that its a permission issue but i already tried to set almost any related directory mode to 777 and its owner & group to apache but still wont work.
它说这是一个权限问题,但我已经尝试将几乎所有相关目录模式设置为 777,并将其所有者和组设置为 apache,但仍然无法正常工作。
It turns out my SELinux is enabled, it's on enforcing mode and my temp folder context is unconfined_u:object_r:httpd_sys_content_t:s0
.事实证明我的 SELinux 已启用,它处于强制模式并且我的临时文件夹上下文是
unconfined_u:object_r:httpd_sys_content_t:s0
。 The problem is solved when i set the temp folder context to httpd_sys_rw_content_t
using chcon -R -t httpd_sys_rw_content_t temp
.当我使用
chcon -R -t httpd_sys_rw_content_t temp
将临时文件夹上下文设置为httpd_sys_rw_content_t
时,问题就解决了。 Thank you guys.感谢你们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.