【问题标题】:Call to undefined function finfo_open() line 43调用未定义函数 finfo_open() 第 43 行
【发布时间】:2014-11-02 05:09:12
【问题描述】:

所以这里的代码如果你能帮助我有系统允许管理员写文章和上传图片当我只写文章并发布它没关系但是当我添加图片到文章并发布时我有错误调用未定义函数 finfo_open() 第 43 行

<?php 

    // RECUPERATION DES DONNEES DU FORMULAIRE
    // photo
        $newsPhotoAvant         = (isset($_POST['newsPhotoAvant']))?        formatage_from_post($_POST['newsPhotoAvant']) : '';
        $newsPhotoDelete        = (isset($_POST['newsPhotoDelete']))?       formatage_from_post($_POST['newsPhotoDelete']) : '';
        $newsPhotoLargeur       = (isset($_POST['newsPhotoLargeur']))?      formatage_from_post($_POST['newsPhotoLargeur']) : '';

    // -----------------
    // Gestion des photos supprimees
    if ($newsPhotoAvant!='' && $newsPhotoDelete=='ON')
    {
        // Suppression de l'ancienne Photo
        if(file_exists('../../'.REP_NEWS_PHOTO.$newsPhotoAvant)) {
            unlink('../../'.REP_NEWS_PHOTO.$newsPhotoAvant);
        }
        // -----------------
        // Suppression dans la base de donnees par UPDATE
        $update_query           = "UPDATE ".$NEWS_TABLE." ".
                                " SET news_photo    = '' ".
                                " WHERE news_id     = :newsId;";
      try {
        $pdo_update             = $pdo->prepare($update_query);
        $pdo_update->bindValue(':newsId',       $newsId,        PDO::PARAM_INT);
        $pdo_update->execute();
      } catch (PDOException $e) { echo 'Erreur SQL : '. $e->getMessage().'<br/>'; die(); }
        // -----------------
    }

    // ----------------------------------
    // VERIFICATION / TRAITEMENT de la photo si uploadee
    // ----------------------------------
    $msgErreurPhoto             = '';   // message d erreur
    $traiterPhotoOK             = true; // (par defaut)

    if(isset($_FILES['newsPhoto']) && $_FILES['newsPhoto']['size']>0)
    {
        // -------------------------------------
        // extension du fichier uploadé (en minuscule)
        $file_Extension         = strtolower(pathinfo($_FILES['newsPhoto']['name'],PATHINFO_EXTENSION));

        // Type MIME réel du fichier (important : évite les fichiers NON valides, dont l'extension a été renommée)
    //  $finfo                  = new finfo(FILEINFO_MIME_TYPE, NULL); // Retourne le type mime
    //  $file_MimeType          = $finfo->file($_FILES['newsPhoto']['tmp_name']);

        // (alternative, si la CLASS finfo n'est pas supportée)
        $finfo                  = finfo_open(FILEINFO_MIME_TYPE); // Retourne le type mime à la extension mimetype
        $file_MimeType          = finfo_file($finfo, $_FILES['newsPhoto']['tmp_name']);
        finfo_close($finfo);

        // -------------------------------------
        // GESTION DES ERREURS
        // -------------------------------------
        // on vérifie les RESTRICTIONS sur les fichiers
        if (UPLOAD_ERR_OK<>0 && UPLOAD_ERR_FORM_SIZE==2) {
            $msgErreurPhoto     .= 'Taille de fichier trop important ('.FILE_SIZEMAX_PHOTO.' octets)<br />';
            $traiterPhotoOK     = false;
        }
        // -----------------
        // on vérifie la TAILLE MAXI
        elseif ($_FILES['newsPhoto']['size'] > FILE_SIZEMAX_PHOTO) {
            $msgErreurPhoto     .= 'Taille de fichier supérieure à la taille maxi autorisée ('.FILE_SIZEMAX_PHOTO.' octets)<br />';
            $traiterPhotoOK     = false;
        }
        // -----------------
        // on vérifie l'EXTENSION
        elseif(!in_array($file_Extension, explode(',', constant('FILE_EXTENSION_PHOTO')))) {
            $msgErreurPhoto     .= 'L\'extension ne correspond pas (Extensions acceptées  : <b>'.constant('FILE_EXTENSION_PHOTO').'</b>)<br />';
            if(in_array($file_MimeType, explode(',', constant('FILE_MIMETYPE_PHOTO')))) {
              $msgErreurPhoto   .= '<b>Attention</b> : Ce fichier est peut-être corrompu !<br />';
              $msgErreurPhoto   .= 'L\'extension ne correspond pas au type MIME !<br />';
            }
            $traiterPhotoOK     = false;
        }
        // -----------------
        // on vérifie le TYPE MIME
        elseif(!in_array($file_MimeType, explode(',', constant('FILE_MIMETYPE_PHOTO')))) {
            $msgErreurPhoto     .= 'Le type MIME ne correspond pas (Extensions acceptées  : <b>'.constant('FILE_EXTENSION_PHOTO').'</b>)<br />';
            if(in_array($file_Extension, explode(',', constant('FILE_EXTENSION_PHOTO')))) {
              $msgErreurPhoto   .= '<b>Attention</b> : Ce fichier est peut-être corrompu !<br />';
              $msgErreurPhoto   .= 'L\'extension ne correspond pas au type MIME !<br />';
            }
            $traiterPhotoOK     = false;
        }
        // -----------------
        if ($traiterPhotoOK===false) {
            $msgErreurPhoto     = '<b>Erreur (Photo)</b> :<br />'.$msgErreurPhoto.'Impossible d\'enregistrer le fichier.';
        }
        // -------------------------------------
        // si pas d'erreur : TRAITEMENT
        // -------------------------------------
        if ($traiterPhotoOK===true)
        {
            // --------------------
            // enregistement de la PHOTO sous forme id_nom-image(.jpg, ...)
            // NB : id etant unique (auto-increment), cela rend le nom de la photo unique
            $file_Upload        = $newsId.'_'.$_FILES['newsPhoto']['name'];
            $file_Upload        = formatage_nom_fichier($file_Upload); // remplacement des caracteres speciaux + tout en minuscules
            $file_Upload        = str_replace('.jpeg','.jpg',$file_Upload); // on remplace aussi .jpeg par .jpg
            // --------------------
            // enregistrement de la photo dans le dossier
            $temp = $_FILES['newsPhoto']['tmp_name'];
            move_uploaded_file($temp, '../../'.REP_NEWS_PHOTO.$file_Upload);
            // --------------------
            // REDIMENSIONNEMENT et SAUVEGARDE de la PHOTO (si necessaire)
            // ecraser (remplacer) la photo (meme rep, meme nom)
            $redimPHOTOOK       = fctredimimage($newsPhotoLargeur,0,'','','../../'.REP_NEWS_PHOTO,$file_Upload);
            // --------------------
            // SUPPRESSION des ANCIENNES PHOTOS (si necessaire) dans le dossier
            if ($newsPhotoAvant!='' && $newsPhotoAvant!=$file_Upload)
            {
                if(file_exists('../../'.REP_NEWS_PHOTO.$newsPhotoAvant)) {
                    unlink('../../'.REP_NEWS_PHOTO.$newsPhotoAvant);
                }
            }
            // -----------------
            // enregistrement du NOM dans la base de donnees par UPDATE
            $update_query       = "UPDATE ".$NEWS_TABLE." SET ".
                                " news_photo            = :file_Upload, ".
                                " news_photo_largeur    = :newsPhotoLargeur ".
                                " WHERE news_id         = :newsId;";
          try {
            $pdo_update         = $pdo->prepare($update_query);
            $pdo_update->bindValue(':file_Upload',      $file_Upload,       PDO::PARAM_STR);
            $pdo_update->bindValue(':newsPhotoLargeur', $newsPhotoLargeur,  PDO::PARAM_STR);
            $pdo_update->bindValue(':newsId',           $newsId,            PDO::PARAM_INT);
            $pdo_update->execute();
          } catch (PDOException $e) { echo 'Erreur SQL : '. $e->getMessage().'<br/>'; die(); }
            // -----------------
        }

    } // fin TRAITEMENT PHOTO
    // ---------------------------------------------------

?>

【问题讨论】:

  • 您使用的是 PHP 5.3+ 吗?如果没有,那么您必须将 finfo 安装为 pecl 模块
  • phpinfo 说我在 5.2.17 版本
  • 然后升级你的 PHP,或者安装 pecl 模块。 finfo 直到 5.3 才成为标准的 php 组件
  • 谢谢你的朋友,我从我的虚拟主机服务从 5.2 切换到 5.3,现在可以了,再次感谢你

标签: php


【解决方案1】:

您似乎尚未启用该扩展程序。在您的php.ini 中启用php_fileinfo.dll。如果它不起作用并且您在 Windows 上运行 PHP,您可以尝试安装 Gnuwin

附:我强烈建议您更新您的 PHP。 PHP

【讨论】:

  • 谢谢你的朋友,我从我的虚拟主机服务从 5.2 切换到 5.3,现在可以了,再次感谢你
猜你喜欢
  • 2017-08-16
  • 2016-09-29
  • 2016-12-01
  • 2014-02-13
  • 1970-01-01
  • 2018-10-01
  • 2014-11-22
  • 2016-09-18
  • 2017-03-06
相关资源
最近更新 更多