Image display with $_POST

samn1 Posted messages 88 Status Member -  
samn1 Posted messages 88 Status Member -
Hello,
Please here is my code that displays an uploaded image and it doesn’t work!
I have two files; the first is HTML, here it is
<html> <head><title>testfichier</title></head> <body> upload your file: <form name="f1" action="fichierAff.php" method="post"> <input type="file" name="fichier" size="60"> <input type="hidden" name="nomFichier"> <input type="submit" name="b1" value="envoyer" /></form> </body>


the second is my .php file that handles the display:
<?php $r=$_POST['nomFichier']; echo " votre image uploadée est $r:::<img src=$r height=50 width=44>"; ?>


I need your help
in advance thanks
Best regards

5 answers

  1. ReDLoG
     
    Hello,
    firstly, you’re trying to display an empty value! Indeed $_POST['nomFichier'] corresponds to this hidden field in your form that contains no value because there is no "value":
    <input type="hidden" name="nomFichier">

    2nd, your uploaded image is accessible from the file field of your form by the array $_FILES['fichier'] :
    <input type="file" name="fichier" size="60">

    The size value you entered corresponds to a 60 KB file, which is very small...
    By reading the PHP documentation, you would have found (and thus learned) on your own where your error lies in the handling of uploaded files, I invite you to consult it without further delay...
    0
  2. le père
     
    Hello

    In your form tag you’re missing the enctype="multipart/form-data" which is essential for uploading files:

    <form name="f1" action="fichierAff.php" method="post" enctype="multipart/form-data">


    As for the processing, not related to what you’re doing, find a tutorial or read the docs:
    http://www.php.net/manual/fr/features.file-upload.post-method.php
    It’s not very difficult
    0
    1. samn1 Posted messages 88 Status Member
       
      Thank you. Here is a faithful translation to English: Thank you very much,
      I found some examples and I am following them!
      Now I would like to display the images that exist in a folder. Will this script be enough?: (knowing that this directory contains only images!)
       "; } ?> 
      0
  3. le père
     
    echo "<img src=$file>";

    When you do that, the 'src' contains only the image name, with no path. The client will therefore try to fetch it from the same folder as your script, which is probably not the correct place.

    You need to add the relative path to go from your script to the folder that contains the images, something like:
    echo "<img src='images/$file'>";
    0
    1. le père
       
      Additionally, in your while ($file = readdir($dir)) loop, you should test that $file is really a file with is_file; otherwise you’ll try to display subfolders (there can be . and .. among others). You should also verify that the files you find are indeed images and not anything else.
      0
    2. samn1 Posted messages 88 Status Member
       
      I tried this and it still doesn’t work:
       <?php echo "bonjour"; $directory="C:\Users\HP\Pictures\Nouveau dossier"; opendir($directory); if ($dir=opendir($directory)) { while ($file=readdir($dir)) if (is_file($file)) echo "<img src=C:\Users\HP\Pictures\Nouveau dossier\\".$file.">"; } ?> 
      0
  4. le père
     
     opendir($directory); if ($dir=opendir($directory))

    Why open the directory twice? your first opendir($directory); is useless.

    Then, with your echo "<img src=C:\Users\HP\Pictures\Nouveau dossier\\".$file.">"; the path name is C:\etc.. that is, a file on your visitor's PC, not on your server. As long as you are your only visitor, it doesn't matter much, but when your site goes live, your visitors won't see anything. And anyone who doesn't have Windows will not understand C;\...
    Moreover, with an error, you put two \ at the end.

    I had written It is necessary to add the relative path. Each word has meaning, I hadn't written "relative" to be cute or scholarly. In fact, you can put an absolute address (with http://yoursite/Pictures, etc.) But in practice, the simplest is often to use a relative path, that is to say only what needs to be added to the folder that contains your script to reach the one that contains the images, even if it means using ../ to go up one level
    0
    1. le père
       
      Last details: add " or ' in src='something'. And you would gain a lot by avoiding spaces and special characters in folder names (no New Folder). It's not forbidden, but then you have to use functions that will unnecessarily complicate things for you.
      0
  5. samn1 Posted messages 88 Status Member
     
    Thank you very much, Father, for all your remarks. See you soon.
    0