IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

Dysfonctionnement de ma class Fichier pour gestion des images


Sujet :

Langage PHP

  1. #1
    Membre régulier
    Homme Profil pro
    Développeur JAVA & PHP
    Inscrit en
    Mai 2013
    Messages
    86
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Développeur JAVA & PHP
    Secteur : Distribution

    Informations forums :
    Inscription : Mai 2013
    Messages : 86
    Points : 71
    Points
    71
    Par défaut Dysfonctionnement de ma class Fichier pour gestion des images
    Bonjour à tous,

    Je doit créer un class Fichier qui permet de gérer différent fichier.

    Je rencontre un probleme avec ma fonction deplacerFichier() et afficherFichier().

    La première fonction permet d'enregistrer mon fichier dans un autre dossier
    La deuxième fonction doit me permettre d'afficher mon fichier dans le navigateur.

    Je galère un peu sur ces deux fonctions.
    Si l'un d'entre vous peut me mettre sur la voie cela me serait extrêmement utile.

    Merci



    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
     
     
    class Fichier
    {
        private $id;
        private  $nomFichier;
        private $newNomFichier;
        private $domaine;
        private  $tailleFichier;
        private  $fichierTmp;
        private  $fichierType;
        private  $fichierExtension;
        private  $dossierCourrant;
        private  $cheminTelechargement;
        private  $fichierExtensionAutorise = array('jpeg','jpg','png','pdf','doc');
        public static $TAILLE_MAX_AUTORISEE = 500000;
        public static $DOC_DEVIS = "DEVIS";
        public static $DOC_BON_COMMANDE = "BC";
        public static $DOC_FICHE_TRAVAIL = "FT";
        public static $DOC_BON_LIVRAISON = "BL";
        public static $DOC_FACTURE = "FACTURE";
        public static $DOC_AVOIR = "AVOIR";
        public static $DOC_PROFORMA = "PF";
        public static $DOC_FIN_CHANTIER = "FC";
     
     
     
     
        public function setNewNomFichier($newNomFichier)
        {
            $this->newNomFichier = $newNomFichier;
        }
     
        /**
         *
         * @param string $nom nom du fichier
         * @param string $fichier fichier $_FILE
         * @param string $path route du dossier
         */
        public function __construct($domaine,$fichier = null,$path,$identifiant=null,$nomfichier=null,$extension = null)
        {
            $this->domaine = $domaine;
            $this->nomFichier = $nomfichier;
            $this->id = $identifiant;
            $this->cheminTelechargement = getcwd()."/$path/".basename($this->nomFichier);
     
     
    //         $this->tailleFichier = filesize($this->nomFichier);
     
     
            if($extension == null)
            {
                $this->fichierExtension = strtolower(end(explode('.',$this->nomFichier)));
            }
            else
            {
                $this->fichierExtension = $extension;
            }
     
     
            /**
             * Creation du nom de fichier
             */
            if($this->id != null)
            {
                $nbrFichier =  count($this->rechercheFichier($path,$this->nomFichier));
     
                if($nbrFichier < 1)
                {
                    $this->newNomFichier = $this->creerNomFichierFacture($this->id,$this->fichierExtension); 
                }
     
            }
            else
            {
                $this->nomFichier = $fichier['name'];
                $this->tailleFichier = $fichier['size'];
                $this->fichierTmp  = $fichier['tmp_name'];
                $this->fichierType = $fichier['type'];
            }
     
        }
     
        /**
         *
         * @param string $identifiant (numéro de facture ou docflow)
         * @param string $extension (ex .pdf)
         * @return string
         */
        public function creerNomFichierFacture($identifiant,$extension)
        {
            $date = date("Ymd");
     
           return  $date."_".$identifiant."_".date("His").".".$extension;
     
        }
     
     
            public function tailleIsValide()
            {
                if($this->tailleFichier <= self::$TAILLE_MAX_AUTORISEE)
                {
     
                    return false;
                }
                return true;
            }
     
     
            public function  extensionIsValide()
            {
                if(!in_array($this->fichierExtension,$this->fichierExtensionAutorise)) {
                    return false;
                }
                return true;
            }
     
        /**
         * Enregistrement du fichier
         * return path du fichier
         */
        public function deplacerFichier()
        {
            if( $this->extensionIsValide())
            {
                var_dump($this->nomFichier);
                var_dump( "exec/".$this->newNomFichier);
                return copy($this->nomFichier, "exec/".$this->newNomFichier);
            }
        }
     
     
        public function fichierExist()
        {
            if(file_exists($this->cheminTelechargement))
            {
                return true;
            }
            return false;
        }
     
        /**
         *
         * @param string $path
         * @param string $nomFichier
         * @return array tableau contenant nom des Fichier
         */
        public function rechercheFichier($path,$nomFichier)
        {
            if($nomFichier !== null)
            {
                $nom = explode("_", $nomFichier);
                if(count($nom) < 3)
                {
                    return glob($path."/".$nomFichier);
                }
                else
                {
                    return glob($path."/".$nom[0]."_".$nom[1]."_*");
                }
            }
     
            return null;
        }
     
     
        public function renommerFichier()
        {
     
            rename($this->cheminTelechargement.$this->nomFichier);
        }
     
        public function afficherFichier()
        {
     
            header("Content-type:{$this->fichierType}");
     
            header("Content-Length: " .$this->tailleFichier);
     
            header("Content-Description:inline;filename=".$this->cheminTelechargement.$this->nomFichier);
     
            header('Content-Transfer-Encoding:binary');
     
            header('Accept-ranges:bytes');
     
            @readfile($this->cheminTelechargement.$this->nomFichier);       
        }
     
     
        public function getNewNomFichier()
        {
            return $this->newNomFichier;
        }
     
    }
     
    $f2 = new Fichier("facture",null,"exec","Identifiant_Adrien","c:\Utilisateurs\jBross\Documents/Identifiant_Adrien.pdf");
    $f2->deplacerFichier();
    $f2->afficherFichier();

  2. #2
    Expert éminent sénior
    Avatar de mathieu
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    10 234
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 10 234
    Points : 15 531
    Points
    15 531
    Par défaut
    tout d'abord, est ce que le déplacement du fichier fonctionne bien ?

  3. #3
    Membre régulier
    Homme Profil pro
    Développeur JAVA & PHP
    Inscrit en
    Mai 2013
    Messages
    86
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Développeur JAVA & PHP
    Secteur : Distribution

    Informations forums :
    Inscription : Mai 2013
    Messages : 86
    Points : 71
    Points
    71
    Par défaut
    Citation Envoyé par mathieu Voir le message
    tout d'abord, est ce que le déplacement du fichier fonctionne bien ?
    Le déplacement ne fonctionne pas, je ne parviens pas à effectuer la copie de mon fichier.

  4. #4
    Expert éminent Avatar de CosmoKnacki
    Homme Profil pro
    Justicier interdimensionnel
    Inscrit en
    Mars 2009
    Messages
    2 858
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Justicier interdimensionnel

    Informations forums :
    Inscription : Mars 2009
    Messages : 2 858
    Points : 6 556
    Points
    6 556
    Par défaut
    Est-ce qu'il est normal que ta fonction rename n'ait qu'un seul paramètre?
    Brachygobius xanthozonus
    Ctenobrycon Gymnocorymbus

  5. #5
    Membre régulier
    Homme Profil pro
    Développeur JAVA & PHP
    Inscrit en
    Mai 2013
    Messages
    86
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Développeur JAVA & PHP
    Secteur : Distribution

    Informations forums :
    Inscription : Mai 2013
    Messages : 86
    Points : 71
    Points
    71
    Par défaut
    Non je l'ai modifié ce matin
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     public function renommerFichier()
        {
            rename($this->nomFichier,$this->newNomFichier);
        }

  6. #6
    Membre régulier
    Homme Profil pro
    Développeur JAVA & PHP
    Inscrit en
    Mai 2013
    Messages
    86
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Développeur JAVA & PHP
    Secteur : Distribution

    Informations forums :
    Inscription : Mai 2013
    Messages : 86
    Points : 71
    Points
    71
    Par défaut
    J'ai constater que mon fichier n'était pas considéré comme tel.

    Lorsque j'utilise la fonction is_file()
    elle me renvoi false.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    var_dump(is_file($this->nomFichier));
    J'ai une grosse partie du problème qui sera résolu si je parviens à corriger cela.
    ils considère mon paramètre comme une string et non un file

  7. #7
    Expert éminent sénior
    Avatar de mathieu
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    10 234
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 10 234
    Points : 15 531
    Points
    15 531
    Par défaut
    dans l'appel de la fonction "copy", indiques plutôt un chemin complet pour le fichier destination.

    ensuite, la méthode s'appelle "deplace" mais tu copies le fichier au lieu de le déplacer. pour déplacer un fichier, utilise "rename" :
    https://www.php.net/manual/fr/function.rename.php

  8. #8
    Membre régulier
    Homme Profil pro
    Développeur JAVA & PHP
    Inscrit en
    Mai 2013
    Messages
    86
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Développeur JAVA & PHP
    Secteur : Distribution

    Informations forums :
    Inscription : Mai 2013
    Messages : 86
    Points : 71
    Points
    71
    Par défaut
    Citation Envoyé par mathieu Voir le message
    dans l'appel de la fonction "copy", indiques plutôt un chemin complet pour le fichier destination.

    ensuite, la méthode s'appelle "deplace" mais tu copies le fichier au lieu de le déplacer. pour déplacer un fichier, utilise "rename" :
    https://www.php.net/manual/fr/function.rename.php
    J'ai revu mes fonctions
    effectivement deplace a été renommé en copier et ma fonction renomme a été changé en deplacerRenommerFichier

    je bute juste sur ma fonction afficherFichier() qui est sencé afficher dans le navigateur mon fichier

    La class aujourd'hui ressemble à ca

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
     
     
    class Fichier
    {
        private  $id;
        private  $nomFichier;
        private  $newNomFichier;
        private  $domaine;
        private  $tailleFichier;
        private  $fichierTmp;
        private  $fichierType;
        private  $fichierExtension;
        private  $dossierCourrant;
        private  $cheminTelechargement;
        private  $destinationFichier;
        private  $fichier;
        private  $fichierExtensionAutorise = array('jpeg','jpg','png','pdf','doc');
     
        public   static $CONTENTTYPE = array(
                                            "pdf"=>"application/pdf",
                                            "jpeg"=>"image/jpeg",
                                            "jpg"=>"image/jpeg",
                                            "png"=>"image/png",
                                            "doc"=>"application/msword"
     
        );
     
        public   static $TAILLE_MAX_AUTORISEE = 500000;
     
       /**
        * 
        * @param String $domaine nom du domaine sur lequelle l'utilisateur se trouve
        * @param String $fichier nom du fichier si transmit par $_FILE(formulaire)
        * @param String $path chemin du dossier ou doit être copié deplacé ou renommé le fichier
        * @param String $identifiant numéro DEVIS FACTUR DOCFLOW ...
        * @param String $nomfichier nom du fichier à exploiter 
        * @param String $extension extention dui fichier (pdf,doc,...)
        */    
        public function __construct($domaine,$fichier = null,$path,$identifiant=null,$nomfichier=null,$extension = null)
        {
            $this->domaine = $domaine;
            $this->nomFichier = $nomfichier;
            $this->id = $identifiant;
     
     
            //$this->cheminTelechargement = getcwd()."/$path/".basename($this->nomFichier);
            //var_dump($this->cheminTelechargement);
     
     
            $this->destinationFichier = $path;
     
            if($nomfichier !== null)
            {
                $this->fichier = file($nomfichier,FILE_USE_INCLUDE_PATH);
                $this->tailleFichier = filesize($this->nomFichier);
            }
     
            if($extension == null)
            {
                $this->fichierExtension = strtolower(end(explode('.',$this->nomFichier)));
            }
            else
            {
                $this->fichierExtension = $extension;
            }
     
     
            /**
             * Creation du nom de fichier
             */
            if($this->id != null)
            {
                /*
                 * On vérifie l'existance du fichier 
                 */
                $nbrFichier =  count($this->rechercheFichier($path,$this->nomFichier));
     
                if($nbrFichier < 1)//si fichier non trouvé 
                {
                    $this->newNomFichier = $this->creerNomFichierFacture($this->id,$this->fichierExtension); 
                }
                else 
                {
                    var_dump("afficher ".$this->nomFichier);
                    //$this->afficherFichier();
                }
            }
            else
            {
                $this->nomFichier = $fichier['name'];
                $this->tailleFichier = $fichier['size'];
                $this->fichierTmp  = $fichier['tmp_name'];
                $this->fichierType = $fichier['type'];
            }
     
        }
     
        /**
         *Création du nomp de fichier au format YYMMDD_IDENTIFIANT_HHIISS.*
         *
         * @param string $identifiant (numéro de facture ou docflow)
         * @param string $extension (ex .pdf)
         * @return string
         */
        public function creerNomFichierFacture($identifiant,$extension)
        {
            $date = date("Ymd");
           return  $date."_".$identifiant."_".date("His").".".$extension;
     
        }
     
            public function tailleIsValide()
            {
                if($this->tailleFichier <= self::$TAILLE_MAX_AUTORISEE)
                {   
                    return false;
                }
                return true;
            }
     
     
            public function  extensionIsValide()
            {
                if(!in_array($this->fichierExtension,$this->fichierExtensionAutorise)) {
                    return false;
                }
                return true;
            }
     
        /**
         * Copie du fichier
         * return path du fichier
         */
        public function copierFichier()
        {
            if( $this->extensionIsValide())
            {
                return copy($this->nomFichier, $this->destinationFichier."/".$this->newNomFichier);
            }
        }
     
     
        public function fichierExist()
        {
            if(file_exists($this->cheminTelechargement))
            {
                return true;
            }
            return false;
        }
     
        /**
         *
         * @param string $path
         * @param string $nomFichier
         * @return array tableau contenant nom de Fichiers
         */
        public function rechercheFichier($path,$nomFichier)
        {
            /*
             * Si le nom de fichier contien son path exec/fifhier.pdf c'est qu'il à déjà été créé
             * 
             */
            $isPath =  explode("/", $nomFichier);
     
     
            if(count($isPath) > 0)
            {
                return glob($nomFichier);
            }
            else 
            {
                if($nomFichier !== null)
                {
                    $nom = explode("_", $nomFichier);
     
                    if(count($nom) < 3)
                    {
                        return glob($path."/".$nomFichier);
                    }
                    else
                    {
                        return glob($path."/".$nom[0]."_".$nom[1]."_*");
                    }
                }   
            }
     
     
            return null;
        }
     
     
        public function deplacerRenommerFichier()
        {
            $route = null;
            $nom = explode("/", $this->nomFichier);
     
            $nbr = count($nom);
     
            for($i=0;$i<$nbr;$i++)
            {
                if($i < ($nbr-1))
                {
                    $route .= $nom[$i];
                }
                else 
                {
                    $route .= "/OLD_".$nom[$i];
                }
            }
            rename($this->nomFichier, $route);
        }
     
        public function afficherFichier()
        {
     
            header("Content-type:{".self::$CONTENTTYPE[$this->fichierExtension]."}");
     
            header("Content-Length: " .$this->tailleFichier);
     
            header("Content-Description:inline;filename=".$this->nomFichier);
     
            header('Content-Transfer-Encoding:binary');
     
            header('Accept-ranges:bytes');
     
            @readfile($this->nomFichier);
     
     
        }
     
    }

  9. #9
    Expert éminent sénior
    Avatar de mathieu
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    10 234
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 10 234
    Points : 15 531
    Points
    15 531
    Par défaut
    si vous n'avez aucun affichage, c'est certainement parce que vous n'avez pas activé l'affichage des erreurs :
    https://y-komotir.developpez.com/tut...-deboguer-php/

  10. #10
    Membre régulier
    Homme Profil pro
    Développeur JAVA & PHP
    Inscrit en
    Mai 2013
    Messages
    86
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Développeur JAVA & PHP
    Secteur : Distribution

    Informations forums :
    Inscription : Mai 2013
    Messages : 86
    Points : 71
    Points
    71
    Par défaut
    Ma fonction n'affiche pas mon pdf via le navigateur et télécharge un fichier inexploitable
    Par contre les erreurs s'affichent, je n'ai pas de souci de ce coté la


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
      public function afficherFichier()
        {
     
            header("Content-type:{".self::$CONTENTTYPE[$this->fichierExtension]."}");
     
            header("Content-Length: " .$this->tailleFichier);
     
            header("Content-Description:inline;filename=".$this->nomFichier);
     
            header('Content-Transfer-Encoding:binary');
     
            header('Accept-ranges:bytes');
     
            @readfile($this->nomFichier);
     
     
        }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Editeur d'image : Classe de gestion des images
    Par Clorish dans le forum Delphi
    Réponses: 35
    Dernier message: 12/07/2007, 20h06
  2. Classe de gestion des images
    Par tlemcenvisit dans le forum MFC
    Réponses: 2
    Dernier message: 03/05/2005, 19h07
  3. Class de gestion des images avec rotation
    Par Johnny Boy dans le forum MFC
    Réponses: 1
    Dernier message: 03/05/2005, 11h54
  4. [TP]Gestion des images bmp avec BMP.TPU
    Par Gabi dans le forum Turbo Pascal
    Réponses: 9
    Dernier message: 14/05/2004, 23h20

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo