PHP Populate input type=file field from stored value

Posted on 2012-08-21
Medium Priority
Last Modified: 2012-08-21
When there is an error on the form I add the values back in the fields so the user doesn't have to renenter them.  All fields work fine except my file field.   I tried something like this:

<input type="file" name="file" id="file"  value="<?php if (isset($_FILES["file"]["tmp_name"])) echo $_FILES["file"]["tmp_name"]; ?>" />

Open in new window

That didn't seem to work.


Question by:christamcc
LVL 58

Accepted Solution

Gary earned 2000 total points
ID: 38318757
You can't, it's a security measure, otherwise someone could lift any file from your pc.
What you could is store the file on first postback ready for when they have made corrections and posted again so they don't have to set the file again and just let them know you already have the file.
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38318893
Gary's right, you can't preset a 'file' field.  That is to prevent someone from loading one of your files against your will.
LVL 111

Expert Comment

by:Ray Paseur
ID: 38318995
What GaryC123 and DaveBaldwin said.  No points for this answer, please, but consider the implications if a server-side script could read anything it wanted from your PC.  If you figure out a way to prepopulate the contents of $_FILES the security experts will shut your method down as quickly as they can find it.  Don't build an application that needs to rely on this kind of security hole.

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question