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

PHP & Base de données Discussion :

Intégré mon code d'upload d'image dans la page.


Sujet :

PHP & Base de données

  1. #1
    Membre averti
    Inscrit en
    Mars 2010
    Messages
    26
    Détails du profil
    Informations personnelles :
    Âge : 36

    Informations forums :
    Inscription : Mars 2010
    Messages : 26
    Par défaut Intégré mon code d'upload d'image dans la page.
    Bonjour,

    J'ai une page pour mettre modifié les données d'un utilisateur, j'ai le code pour uploader le tout mais je n'arrive pas à les mettre ensemble sur la même page.


    Code Upload fichier : 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
    <?php
    $poids_max = 512000; // Poids max de l'image en octets (1Ko = 1024 octets)
    $repertoire = 'uploads/'; // Repertoire d'upload
    if (isset($_FILES['fichier']))
    {
     
    // On vérifit le type du fichier
    if ($_FILES['fichier']['type'] != 'image/png' && $_FILES['fichier']['type'] != 'image/jpeg' && $_FILES['fichier']['type'] != 'image/jpg' && $_FILES['fichier']['type'] != 'image/gif' && $_FILES['fichier']['type'] != 'image/bmp' && $_FILES['fichier']['type'] != 'image/jpg' && $_FILES['fichier']['type'] != 'image/png' && $_FILES['fichier']['type'] != 'image/ico')
    {
    $erreur = 'Le fichier doit être au format *.jpeg, *.bmp, *.jpg, *.png, *.ico *.gif ou *.png .';
    }
     
    // On vérifit le poids de l'image
    elseif ($_FILES['fichier']['size'] > $poids_max)
    {
    $erreur = 'L\'image doit être inférieur à ' . $poids_max/1024 . 'Ko.';
    }
     
    // On vérifit si le répertoire d'upload existe
    elseif (!file_exists($repertoire))
    {
    $erreur = 'Erreur, le dossier d\'upload n\'existe pas.';
    }
     
    // Si il y a une erreur on l'affiche sinon on peut uploader
    if(isset($erreur))
    {
    echo '' . $erreur . '<br><a href="javascript:history.back(1)">Retour</a>';
    }
    else
    {
     
    // On définit l'extention du fichier puis on le nomme par le timestamp actuel
    if ($_FILES['fichier']['type'] == 'image/jpeg') { $extention = '.jpeg'; }
    if ($_FILES['fichier']['type'] == 'image/jpeg') { $extention = '.jpg'; }
    if ($_FILES['fichier']['type'] == 'image/png') { $extention = '.png'; }
    if ($_FILES['fichier']['type'] == 'image/gif') { $extention = '.gif'; }
    if ($_FILES['fichier']['type'] == 'image/gif') { $extention = '.bmp'; }
    if ($_FILES['fichier']['type'] == 'image/gif') { $extention = '.jpg'; }
    if ($_FILES['fichier']['type'] == 'image/gif') { $extention = '.png'; }
    if ($_FILES['fichier']['type'] == 'image/gif') { $extention = '.ico'; }
    $nom_fichier = time().$extention;
     
    // On upload le fichier sur le serveur.
    if (move_uploaded_file($_FILES['fichier']['tmp_name'], $repertoire.$nom_fichier))
    {
    $url = 'www.monsite.com/'.$repertoire.''.$nom_fichier.'';
    echo 'Votre image à été uploadée sur le serveur avec succes!<br>Voici le lien: <input type="text" value="' . $url . '" size="60">';
    }
    else
    {
    echo 'L\'image n\'a pas pu être uploadée sur le serveur.';
    }
     
    }
     
    }
    else
    {
    ?>
    <form method="post" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $poids_max; ?>">
    <input type="file" name="fichier">
    <input type="submit" value="Envoyer">
    </form>
    <?php
    }
    ?>

    Après le code pour modifier mon utilisateur.
    Qui renvois toutes les variables vers une page qui va faire les modifications dans la base de donnée.

    Code modifier utilisateur : 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
    <form action="modifvalidation.php" method="post">
    <input type="hidden" name="Id" value="<?php echo $id; ?>" size=30 />
      <table width="100%" border="0" cellspacing="0" cellpadding="2">
     
      <tr> 
        <td width="50%" align="right">Initial </td>
     
        <td width="50%">
        	<table border="0" bgcolor="" cellpadding=0 cellspacing=2>
            <tr>
              <td><input type="text" name="Initial" value="<?php echo $initial; ?>" size=30 /> </td>
            </tr>
          </table>
        </td>
      </tr>
      <tr> 
        <td width="50%" align="right">Nom </td>
     
        <td width="50%">
        	<table border="0" bgcolor="" cellpadding=0 cellspacing=2>
            <tr>
              <td><input type="text" name="Nom" value="<?php echo $nom; ?>" size=30 /> </td>
            </tr>
          </table>
        </td>
      </tr>
      <tr> 
        <td width="50%" align="right">Pr&eacute;nom </td>
     
        <td>
    	<table border="0" bgcolor="" cellpadding=0 cellspacing=2>
            <tr>
              <td><input type="text" name="Prenom" value="<?php echo $prenom; ?>" size=30 /></td>
            </tr>
          </table>
        </td>
      </tr>
      <tr> 
        <td width="50%" align="right">Numero</td>
     
        <td>
    	<table border="0" bgcolor="" cellpadding=0 cellspacing=2>
            <tr>
              <td><input type="text" name="Numero" value="<?php echo $numero; ?>" size=30 /></td>
            </tr>
          </table>
        </td>
      </tr>
     <td align="center" colspan="2"><input type="submit" value="Modifier" />
    <a href="deletevalidation.php?Id=<?php echo "$id" ?>;"> <input type="button" value="Supprimer"> </a>
     </td>
      </table>
      </form>


    En faite j'aimerai passé ".l'url." que je crée dans le code d'upload sur la page modifvalidation.php en meme temps que les autres variables.
    Mais comme y'a 2 fois la balise "FORM" je ne sais pas comment intégré ça.

    Si vous pouvez m'aider svp

    merci

  2. #2
    Membre éclairé
    Inscrit en
    Juin 2006
    Messages
    584
    Détails du profil
    Informations forums :
    Inscription : Juin 2006
    Messages : 584
    Par défaut
    Il te suffit de mettre tout dans le même formulaire avec :

    <form method="post" enctype="multipart/form-data">

    C'est tout.

  3. #3
    Membre averti
    Inscrit en
    Mars 2010
    Messages
    26
    Détails du profil
    Informations personnelles :
    Âge : 36

    Informations forums :
    Inscription : Mars 2010
    Messages : 26
    Par défaut
    Bah justement je n'y arrive pas.

    j'ai fais comme ça

    et ça me dis que "url" n'est pas défini

    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
     
    <?php
    $poids_max = 512000; // Poids max de l'image en octets (1Ko = 1024 octets)
    $repertoire = 'uploads/'; // Repertoire d'upload
    if (isset($_FILES['fichier']))
    {
     
    // On vérifit le type du fichier
    if ($_FILES['fichier']['type'] != 'image/png' && $_FILES['fichier']['type'] != 'image/jpeg' && $_FILES['fichier']['type'] != 'image/jpg' && $_FILES['fichier']['type'] != 'image/gif' && $_FILES['fichier']['type'] != 'image/bmp' && $_FILES['fichier']['type'] != 'image/jpg' && $_FILES['fichier']['type'] != 'image/png' && $_FILES['fichier']['type'] != 'image/ico')
    {
    $erreur = 'Le fichier doit être au format *.jpeg, *.bmp, *.jpg, *.png, *.ico *.gif ou *.png .';
    }
     
    // On vérifit le poids de l'image
    elseif ($_FILES['fichier']['size'] > $poids_max)
    {
    $erreur = 'L\'image doit être inférieur à ' . $poids_max/1024 . 'Ko.';
    }
     
    // On vérifit si le répertoire d'upload existe
    elseif (!file_exists($repertoire))
    {
    $erreur = 'Erreur, le dossier d\'upload n\'existe pas.';
    }
     
    // Si il y a une erreur on l'affiche sinon on peut uploader
    if(isset($erreur))
    {
    echo '' . $erreur . '<br><a href="javascript:history.back(1)">Retour</a>';
    }
    else
    {
     
    // On définit l'extention du fichier puis on le nomme par le timestamp actuel
    if ($_FILES['fichier']['type'] == 'image/jpeg') { $extention = '.jpeg'; }
    if ($_FILES['fichier']['type'] == 'image/jpeg') { $extention = '.jpg'; }
    if ($_FILES['fichier']['type'] == 'image/png') { $extention = '.png'; }
    if ($_FILES['fichier']['type'] == 'image/gif') { $extention = '.gif'; }
    if ($_FILES['fichier']['type'] == 'image/gif') { $extention = '.bmp'; }
    if ($_FILES['fichier']['type'] == 'image/gif') { $extention = '.jpg'; }
    if ($_FILES['fichier']['type'] == 'image/gif') { $extention = '.png'; }
    if ($_FILES['fichier']['type'] == 'image/gif') { $extention = '.ico'; }
    $nom_fichier = time().$extention;
     
    // On upload le fichier sur le serveur.
    if (move_uploaded_file($_FILES['fichier']['tmp_name'], $repertoire.$nom_fichier))
    {
    $url = 'www.monsite.com/'.$repertoire.''.$nom_fichier.'';
     
    }
     
    }
     
    }
    else
    {
    ?>
     
    <form action="modifvalidation.php" method="post" enctype="multipart/form-data">
     
    <input type="hidden" name="Id" value="<?php echo $id; ?>" size=30 />
    <input type="hidden" name="Photo" value="<?php echo $url; ?>" size=30 />
     
        <table width="100%" border="0" cellspacing="0" cellpadding="2">
        <tr>
        	<td width="50%" align="right">Initial </td>
            <td width="50%"><input type="text" name="Initial" value="<?php echo $initial; ?>" size=30 /> </td>
        </tr>
     
        <tr>
        	<td width="50%" align="right">Nom </td>
            <td width="50%"><input type="text" name="Nom" value="<?php echo $nom; ?>" size=30 /> </td>
        </tr>
     
        <tr>
        	<td width="50%" align="right">Prénom </td>
            <td width="50%"><input type="text" name="Prenom" value="<?php echo $prenom; ?>" size=30 /> </td>
        </tr>
     
        <tr>
        	<td width="50%" align="right">Numero </td>
            <td width="50%"><input type="text" name="Numero" value="<?php echo $numero; ?>" size=30 /> </td>
        </tr>
     
        <tr>
        	<td width="50%" align="right">Societé </td>
            <td width="50%"><input type="text" name="Societe" value="<?php echo $societe; ?>" size=30 /> </td>
        </tr>
     
     
        <tr>
        <td align="center" colspan="2">
    <a href="deletevalidation.php?Id=<?php echo "$id" ?>;"> <input type="button" value="Supprimer"> </a></td>
        </tr>
        </table>
     
    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $poids_max; ?>">
    <input type="file" name="fichier">
     
    <input type="submit" value="Envoyer">
    </form>
    <?php
    }
    ?>
    Notice: Undefined variable: url in C:\Program Files (x86)\EasyPHP5.2.10\www\test\test.php on line 62
    '..'

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <input type="hidden" name="Photo" value="<?php echo $url; ?>" size=30 />

    ça ne m'upload même plus les images.

    Y'a il faut adapté le code pour les implantés l'un dans l'autre mais compliquer avec les vérifications et tout.

  4. #4
    Membre éclairé Avatar de camyo
    Homme Profil pro
    Chef de projet NTIC
    Inscrit en
    Juin 2004
    Messages
    400
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Chef de projet NTIC

    Informations forums :
    Inscription : Juin 2004
    Messages : 400
    Par défaut
    La variable $url est une variable qui n'est défini que quand tu lance l'upload ..

    donc dans ton form, avant l'upload, elle existe pas

  5. #5
    Membre Expert Avatar de RunCodePhp
    Profil pro
    Inscrit en
    Janvier 2010
    Messages
    2 962
    Détails du profil
    Informations personnelles :
    Localisation : Réunion

    Informations forums :
    Inscription : Janvier 2010
    Messages : 2 962
    Par défaut
    Salut

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <input type="hidden" name="Photo" value="<?php echo $url; ?>" size=30 />
    Hormis le fait que $url n'est pas défini (n'existe pas) comme l'a précisé Camyo, aussi, on ne sait pas ce que tu espère obtenir dans ce champ caché, mais si c'est le chemin où se trouve la photo, ça, tu ne l'obtiendras pas.

Discussions similaires

  1. recuperer mon url d'upload d'image et inserer dans ma table ?
    Par nico le noob dans le forum Requêtes
    Réponses: 1
    Dernier message: 17/06/2010, 01h03
  2. Réponses: 7
    Dernier message: 03/03/2010, 18h33
  3. uploader une image dans un autre formulaire
    Par jonathan.ohayon dans le forum Balisage (X)HTML et validation W3C
    Réponses: 12
    Dernier message: 03/05/2006, 23h04
  4. [Upload] Insertion image dans mysql après upload
    Par alexmorel dans le forum Langage
    Réponses: 4
    Dernier message: 20/03/2006, 23h46
  5. Récuperation du code binaire d'une image dans une chaîne
    Par Nomely dans le forum Langages de programmation
    Réponses: 7
    Dernier message: 27/01/2006, 19h01

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