首页 > 解决方案 > 警告::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");
    }

}

标签: php

解决方案


如果您想检查该图像是否已存在同名,然后在上传之前将其删除,您可以执行以下操作:

if(file_exists($dir.$_FILES['img']['name'])) {
    unlink($dir.$_FILES['img']['name']);
}

以防止抛出任何警告。


推荐阅读