简体   繁体   English

Codeigniter file_put_contents:未能打开 stream:权限被拒绝

[英]Codeigniter file_put_contents: failed to open stream: Permission denied

While Uploading the image in the server I got the error in codeigniter.在服务器中上传图像时,我在 codeigniter 中遇到错误。 But it works fine in local system.但它在本地系统中运行良好。

A PHP Error was encountered:遇到 PHP 错误:

Severity: Warning

Message: file_put_contents(upload/logo/1617000545.png): failed to open stream: Permission denied

Filename: models/Logo_m.php

Line Number: 41行号:41

    list($type, $data) = explode(';', $data);
        list(, $data) = explode(',', $data);
       
        $data = base64_decode($data);
        $imageName = time().'.png';

        file_put_contents('upload/logo/'.$imageName, $data);````

Do you have check about your directory or permission?您是否检查过您的目录或权限?

Some problem maybe cause:一些问题可能导致:

  1. Directory Does not exists, you must create it first.目录不存在,您必须先创建它。
  2. Permissions / Script does not have enough access to write files, you must verify the application could write directory and directory is writable. Permissions/Script 没有足够的权限来写文件,你必须验证应用程序可以写目录并且目录是可写的。

Your code also doesn't validate the image content / metadata;您的代码也不会验证图像内容/元数据;

// $data = trim(explode(',', explode(';', $data)[1])[1]);
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
// determine absolute path
$imageDirectory = __DIR__ .'/upload/logo';
// check if string / content is an image
if (! ($imgInfo = getimagesizefromstring($data))) {
    // do exit here because data is not a binary image string
    throw new Exception('Data is not an image');
}
// check if image directory exist
if (!file_exists($imageDirectory)) {
    mkdir($imageDirectory, 755, true);
}
if (!is_dir($imageDirectory) || !is_writable($imageDirectory)) {
    throw new Exception('Could not save image. Directory is not writable.');
}

$extension = $imgInfo['mime'];
// optional to filter jpeg image
if ($extension  === 'jpeg') {
    $extension = 'jpg';
}
// use only time is not recommended due time is not unique, please change with random value
$baseName = time();
$imageName = $baseName . '.' . $extension;
$written   = file_put_contents($imageDirectory.'/'.$imageName, $data);

暂无
暂无

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

相关问题 file_put_contents无法打开流:权限被拒绝 - file_put_contents failed to open stream: Permission denied file_put_contents - 无法打开流:权限被拒绝 - file_put_contents - failed to open stream: Permission denied file_put_contents无法打开流:权限被拒绝 - file_put_contents failed to open stream: Permission denied 警告:file_put_contents():无法打开流:权限被拒绝 - Warning: file_put_contents(): failed to open stream: Permission denied file_put_contents()::无法打开流:权限被拒绝,调整CODEIGNITER的大小 - file_put_contents():: failed to open stream: Permission denied, resize CODEIGNITER file_put_contents(文件路径/文件)[function.file-put-contents]:无法打开流:权限被拒绝 - file_put_contents(filepath/file) [function.file-put-contents]: failed to open stream: Permission denied Google 云端硬盘:file_put_contents(token.json):无法打开流:权限被拒绝 - Google Drive: file_put_contents(token.json): failed to open stream: Permission denied Symfony 控制台命令 v6 和 Dompdf --- file_put_contents - 无法打开 stream:权限被拒绝 (windows) - Symfony Console Commands v6 and Dompdf --- file_put_contents - Failed to open stream: Permission denied (windows) Laravel Forge:file_put_contents():无法打开流:权限被拒绝 - Laravel Forge: file_put_contents(): failed to open stream: Permission denied Laravel-file_put_contents()-打开stream失败,权限被拒绝 - Laravel- file_put_contents()-failed to open stream, permission denied
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM