图像不从数据库 Codeigniter 4 中显示
image doesn't display from database Codeigniter 4
我是 codigniter 的新手。我正在尝试通过使用创建页面将它们发送到数据库来显示来自 mysql 数据库的图像,您可以在其中输入标题、一些信息和 select 图像,创建项目后显示的所有内容除外对于图像。一旦我打开 phpmyadmin,它就会显示图像有数据,但看起来它只是元数据,而不是实际图像本身。我已经坚持了几天了,所以我希望你们能帮助我!
在控制器中创建函数(已编辑):
public function create(){
$model = new ProjectModel();
$file = $this->request->getFile('image');
if ($this->request->getMethod() === 'post' && $this->validate([
'title' => 'required|min_length[3]|max_length[255]',
'info' => 'required',
'image' => 'uploaded[image]',
]))
{
$model->save([
'title' => $this->request->getPost('title'),
'slug' => url_title($this->request->getPost('title'), '-', TRUE),
'info' => $this->request->getPost('info'),
$tempfile = $file->getTempName(),
$imgdata = file_get_contents($tempfile),
]);
var_dump($imgdata);
#echo view('project/success');
}
else
{
echo view('site/create');
}
}
我的模型
namespace App\Models;
use CodeIgniter\Model;
class ProjectModel extends Model
{
protected $table = 'projects';
protected $allowedFields = ['title', 'slug', 'info', 'image'];
public function getProjects($slug = false)
{
if ($slug === false)
{
return $this->findAll();
}
return $this->asArray()
->where(['slug' => $slug])
->first();
}
}
这是在您按下创建按钮后创建 div 元素的文件
<?php if (! empty($projects) && is_array($projects)) : ?>
<?php foreach ($projects as $project_item): ?>
<div class="project-box" href="/projects/<?= esc($project_item['slug'], 'url') ?>">
<?php echo '<img class="project-image" src="data:image/jpeg;base64,'.base64_encode($project_item['image']).'" alt="image" ">'?>
<p class="project-name"><?=esc($project_item['title'])?></p>
<div class="bottom-border">
<p class="project-creator"><i class="far fa-user-circle"></i> <?=esc($project_item['creator'])?></p>
<div class="statistics">
<p class="project-likes"><i class="fas fa-heart"></i> <?=esc($project_item['likes'])?></p>
<p class="project-views"><i class="far fa-eye"></i> <?=esc($project_item['views'])?></p>
</div>
</div>
</div>
<?php endforeach; ?>
<?php else : ?>
<h3>No Projects</h3>
<?php endif ?>
这是您可以在其中创建项目的创建文件
<div class="parent">
<?= \Config\Services::validation()->listErrors() ?>
<form action="/site/create" method="post">
<h1>Create Project</h1>
<?= csrf_field() ?>
<div class="form-group">
<input class="ph-title" type="input" name="title" placeholder="Title" /><br/>
</div>
<div class="grow-wrap">
<textarea class="ph-info" name="info" placeholder="Type in project info"></textarea>
</div>
<!-- <div class="file-input">
<label for="file">
Select file
<p class="file-name"></p>
</label>
</div> -->
<input class="file-btn" type="file" name="image" value=""/><br/>
<input class="create-btn" type="submit" name="submit" value="Create Project"/>
</form>
</div>
我做错了什么?
这里有一些错误。首先,您的表单设置为图片上传,但需要将 enctype 添加到表单标签,以便您的后端 PHP 可以接收 $_FILES 对象
<form action="/site/create" method="post" enctype="multipart/form-data">
从输入中删除 value=""...
<input class="file-btn" type="file" name="image" />
现在您可以接收文件了 - 请参阅 https://codeigniter4.github.io/userguide/libraries/uploaded_files.html
您的验证应该更改
$this->validate([
//... your other validations...
'image' => 'uploaded[image]' // instead of required
]);
如果您想将图片存储在您的服务器上,然后将图片的名称记录在数据库中以供日后检索:
$imageDirectory="/path/to/where/you/store/images";
$imageName = $file->getRandomName();
$path = $this->request->getFile('image')->store($imageDirectory, $imageName);
// path is now the full path to the image on your server.
如果您想存储图像 BLOB 数据(如您的示例):
$tempfile = $file->getTempName();
$imgdata = file_get_contents($tempfile);
那么您的插入内容将如下所示...
$model->save([
'title' => $this->request->getPost('title'),
'slug' => url_title($this->request->getPost('title'), '-', TRUE),
'info' => $this->request->getPost('info'),
'image' => $imgdata,
]);
我是 codigniter 的新手。我正在尝试通过使用创建页面将它们发送到数据库来显示来自 mysql 数据库的图像,您可以在其中输入标题、一些信息和 select 图像,创建项目后显示的所有内容除外对于图像。一旦我打开 phpmyadmin,它就会显示图像有数据,但看起来它只是元数据,而不是实际图像本身。我已经坚持了几天了,所以我希望你们能帮助我!
在控制器中创建函数(已编辑):
public function create(){
$model = new ProjectModel();
$file = $this->request->getFile('image');
if ($this->request->getMethod() === 'post' && $this->validate([
'title' => 'required|min_length[3]|max_length[255]',
'info' => 'required',
'image' => 'uploaded[image]',
]))
{
$model->save([
'title' => $this->request->getPost('title'),
'slug' => url_title($this->request->getPost('title'), '-', TRUE),
'info' => $this->request->getPost('info'),
$tempfile = $file->getTempName(),
$imgdata = file_get_contents($tempfile),
]);
var_dump($imgdata);
#echo view('project/success');
}
else
{
echo view('site/create');
}
}
我的模型
namespace App\Models;
use CodeIgniter\Model;
class ProjectModel extends Model
{
protected $table = 'projects';
protected $allowedFields = ['title', 'slug', 'info', 'image'];
public function getProjects($slug = false)
{
if ($slug === false)
{
return $this->findAll();
}
return $this->asArray()
->where(['slug' => $slug])
->first();
}
}
这是在您按下创建按钮后创建 div 元素的文件
<?php if (! empty($projects) && is_array($projects)) : ?>
<?php foreach ($projects as $project_item): ?>
<div class="project-box" href="/projects/<?= esc($project_item['slug'], 'url') ?>">
<?php echo '<img class="project-image" src="data:image/jpeg;base64,'.base64_encode($project_item['image']).'" alt="image" ">'?>
<p class="project-name"><?=esc($project_item['title'])?></p>
<div class="bottom-border">
<p class="project-creator"><i class="far fa-user-circle"></i> <?=esc($project_item['creator'])?></p>
<div class="statistics">
<p class="project-likes"><i class="fas fa-heart"></i> <?=esc($project_item['likes'])?></p>
<p class="project-views"><i class="far fa-eye"></i> <?=esc($project_item['views'])?></p>
</div>
</div>
</div>
<?php endforeach; ?>
<?php else : ?>
<h3>No Projects</h3>
<?php endif ?>
这是您可以在其中创建项目的创建文件
<div class="parent">
<?= \Config\Services::validation()->listErrors() ?>
<form action="/site/create" method="post">
<h1>Create Project</h1>
<?= csrf_field() ?>
<div class="form-group">
<input class="ph-title" type="input" name="title" placeholder="Title" /><br/>
</div>
<div class="grow-wrap">
<textarea class="ph-info" name="info" placeholder="Type in project info"></textarea>
</div>
<!-- <div class="file-input">
<label for="file">
Select file
<p class="file-name"></p>
</label>
</div> -->
<input class="file-btn" type="file" name="image" value=""/><br/>
<input class="create-btn" type="submit" name="submit" value="Create Project"/>
</form>
</div>
我做错了什么?
这里有一些错误。首先,您的表单设置为图片上传,但需要将 enctype 添加到表单标签,以便您的后端 PHP 可以接收 $_FILES 对象
<form action="/site/create" method="post" enctype="multipart/form-data">
从输入中删除 value=""...
<input class="file-btn" type="file" name="image" />
现在您可以接收文件了 - 请参阅 https://codeigniter4.github.io/userguide/libraries/uploaded_files.html
您的验证应该更改
$this->validate([
//... your other validations...
'image' => 'uploaded[image]' // instead of required
]);
如果您想将图片存储在您的服务器上,然后将图片的名称记录在数据库中以供日后检索:
$imageDirectory="/path/to/where/you/store/images";
$imageName = $file->getRandomName();
$path = $this->request->getFile('image')->store($imageDirectory, $imageName);
// path is now the full path to the image on your server.
如果您想存储图像 BLOB 数据(如您的示例):
$tempfile = $file->getTempName();
$imgdata = file_get_contents($tempfile);
那么您的插入内容将如下所示...
$model->save([
'title' => $this->request->getPost('title'),
'slug' => url_title($this->request->getPost('title'), '-', TRUE),
'info' => $this->request->getPost('info'),
'image' => $imgdata,
]);