FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » form - file - sumple question
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: form - file - sumple question [message #176530 is a reply to message #176443] Mon, 09 January 2012 08:38 Go to previous messageGo to previous message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma:
Senior Member
El 07/01/2012 15:06, Leo Decap escribió/wrote:
> I have a form which users can fill in.
> If the user hasn't filled in a required field, the form reloads after
> the Submit with all the data the user has entered filled in (and the
> opportunity to fill in the missing data).
> I get the data through $_POST and echo the allready filled in data on
> the form. Easy.
>
> I have 1 field - type File - where the user can choose a photo to upload.
> On reload of the form, I would love to see the entered data (path?)
> reappear, but I can't figure out how to do this..........

This approach hits against two walls:

1. The $_FILES array does not contain the original path, mainly because
the browser does not send it to the server.

2. Most browsers ignore pre-filled <input type="file"> elements.

Additionally, you have no control about the uploaded file size until the
file is actually received. If you force the user to upload the file
again and it happens to be a large file, your form can become pretty
unusable.

You need to use a different technique. The simplest one is to move
successfully uploaded files to a temporary location you control and link
them to your form through a reasonably large random ID (so it cannot be
guessed) that can be transmitted through a set of <input type="hidden">
fields.

E.g., if the original $_FILES entry is this:

array(1) {
["avatar"]=>
array(5) {
["name"]=>
string(10) "Logo01.gif"
["type"]=>
string(9) "image/gif"
["tmp_name"]=>
string(27) "C:\WINDOWS\Temp\php1728.tmp"
["error"]=>
int(0)
["size"]=>
int(732)
}
}

.... you can use uniqid('tmp_') to generate a random name and save your
file as:

C:\Sites\example.com\temp\tmp_4f0aa7072cb3e

.... and add this to your form:

<input type="hidden" name="avatar[id]" value="tmp_4f0aa7072cb3e">
<input type="hidden" name="avatar[name]" value="Logo01.gif">
<input type="hidden" name="avatar[type]" value="image/gif">

When all the entered data is correct, you can move the temporary file to
its definitive location.


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Best PHP Training | PHP Development Training | PHP Training Institute
Next Topic: Running function in the background?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 28 15:56:08 GMT 2024

Total time taken to generate the page: 0.04831 seconds