PHP Populate input type=file field from stored value

Posted on 2012-08-21
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

    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 82

    Expert Comment

    by:Dave Baldwin
    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 107

    Expert Comment

    by:Ray Paseur
    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

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    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.

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now