Image display with $_POST
samn1
Posted messages
88
Status
Member
-
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
the second is my .php file that handles the display:
I need your help
in advance thanks
Best regards
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
-
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... -
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 -
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'>";
-
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 -
Thank you very much, Father, for all your remarks. See you soon.