php - 警告::C 中没有这样的文件或目录
问题描述
我正在为网站开发 mvc 框架,我只想在编辑帖子时从我的图像文件夹中删除旧图像,但出现以下错误:
警告:取消链接(C:\xampp\htdocs\Model View Controller2/public/images/2425.jpg):C:\xampp\htdocs\Model View Controller2\app\Controller\Admin\ServicesController.php 中没有这样的文件或目录在第 72 行
这是我的控制器(当我编辑相同的图像时,旧的图像被删除;它是我想要的不同图像)
<?php
namespace App\Controller\Admin;
use Core\HTML\BootstrapForm;
use \App;
class ServicesController extends AppController{
public function __construct(){
parent::__construct();
$this->loadModel('Service');
}
public function index(){
$services = $this->Service->all();
$this->render('admin.services.index', compact('services'));
}
public function edit(){
$errors = false;
$this->loadModel('Service');
if (!empty($_POST)) {
if($_FILES['img']['size'] > 1500000){
$errors = "<strong>Ce fichier est trop lourd !</strong>";
}
if(strrchr($_FILES['img']['name'], '.') !== '.jpg'){
$errors = "<strong>L'extension de ce fichier n'est pas un .jpg !</strong>";
}
if($errors != true){
$dir = '../public/images/';
print_r($dir.$_FILES['img']['name']);
unlink($dir.$_FILES['img']['name']);
move_uploaded_file($_FILES['img']['tmp_name'], $dir.$_FILES['img']['name']);
$result = $this->Service->update($_GET['id'], array(
'titre' => $_POST['titre'],
'contenu' => $_POST['contenu'],
'img' => $dir.$_FILES['img']['name']));
}
}
$service = $this->Service->find($_GET['id']);
$services = $this->Service->extract('id', 'titre', 'contenu', 'img');
$form = new BootstrapForm($service);
$this->render('admin.services.edit', compact('services', 'form', 'errors'));
}
public function delete(){
if (!empty($_POST)) {
$result = $this->Service->delete($_POST['id']);
return $this->index();
}
}
}`
这是我的视图表单 edtit.php
<center><h1>Editer la page d'accueil Services</h1></center>
<?php if($errors): ?>
<center class="alert alert-danger">
<?= $errors; ?>
</center>
<?php endif; ?>
<form class="edit" action="" method="post" enctype="multipart/form-data">
<?= $form->input('titre', 'Titre'); ?>
<?= $form->input('contenu', 'Message', array('type' => 'textarea', 'rows' => 8)); ?>
<?= $form->input('img', 'image'); ?>
<img src="<?= $servie->img; ?>">
<?= $form->file('img', 'Choisissez une image'); ?>
<button class="btn btn-primary">Sauvegarder</button>
</form>
我的 ServiceTable.php
<?php
namespace App\Table;
use Core\Table\Table;
class ServiceTable extends Table{
protected $table = 'services';
/**
* Récupère les derniers article
* @return array
*/
public function last(){
return $this->query("
SELECT * FROM services");
}
}
解决方案
如果您想检查该图像是否已存在同名,然后在上传之前将其删除,您可以执行以下操作:
if(file_exists($dir.$_FILES['img']['name'])) {
unlink($dir.$_FILES['img']['name']);
}
以防止抛出任何警告。
推荐阅读
- swift - 每次我尝试访问它时,Struct 的 UUID 类型的 id 都会发生变化
- linux - 如何清空日志文件的内容
- css - 打破 Grommet 样式组件样式的全局样式
- java - 在Java端解密列数据
- javascript - 如何将 Next.js 链接到现有的 HTML 静态网站?
- python - Python Flask - 从 open_session 调用时中止(400)不使用预期的模板
- mariadb - 我在存储过程 mariadb 中出现语法错误的原因是什么?
- c# - 如何将 JSON 反序列化为泛型对象,并根据 JSON 中的数据结构获取合适的类型?
- azure-log-analytics - 我正在使用度量收集器设备从我的每个设备收集一些天蓝色 IotHub 数据。如何获取这些insightMetrics 数据,设备方面
- c# - 在 MVC 中将秒转换为分钟不起作用