Jump to content

Displaying an uploaded image using a form.


AlenD

Recommended Posts

I'm beginning with simple codes to understand how things work and work my way up with what I need.

 

My upload form looks like this:

 

<form name="upload about" action="display.php" enctype="multipart/form-data" method="post">

Header: <input type="text" name="header" />

Body: <input type="text" name="body"  />

<input type="hidden" name="MAX_FILE_SIZE" value="30000" />

Image: <input type="file" name="pic"  />

<input type="submit" />

</form>

 

And my display page is just as simple.

 

<?php echo $_POST["header"]; ?>

<?php echo $_POST["body"]; ?>

<?php echo $_FILES['pic']['name']; ?>

 

The problem is, whenever I submit the data, everything is fine, except the image does not display and just posts the filename of the image I uploaded.

According to my research setting the enctype to multipart/formdata should display the image, but it does not. Can anyone tell me what's wrong?

Link to comment
Share on other sites

Hello sir, I have been reading that documentation, as you can see the example I have given uses the same example given in the POST method page. It has been said that the important things are that the file size shouldn't exceed the limit, and I have to set the form enctype, in which I have put attention to both. Can you please tell me what is wrong so I may pinpoint my mistake and learn from it?

Link to comment
Share on other sites

Your code is working as intended. You ask PHP to display (among other things) the name of the uploaded file with this line

<?php echo $_FILES['pic']['name']; ?>

 

if you wanted your browser to render an image, you would need to tell it to do so with an img tag. There are a few steps that you must take before you can do this though (like moving the file from the temporary directory where all uploaded images are stored). I suggest you continue reading the manual entry, and a few other file uploading tutorials if needed.

Link to comment
Share on other sites

Thank you sir for directing me to the right path, I have successfully transfered the temporary folder to a designated folder in the web tree, then display it with image tags. But my current problem is the image path has a fixed name, and if I upload a different file with a different file name, the image will not be displayed because indeed the file name is different. What is the way to make the file for the image tag dynamic?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.