Setting value for <input type="file"> ?

Discussion in 'HTML & Website Design' started by Kerosene, Dec 17, 2008.

  1. #1
    I have a page where the user can upload an image, along with all their personal info (names, address, blabla). If they forget to fill in their name, then the form resets, and remembers everything they're previously entered - EXCEPT for the image location.

    I don't want the user to have to search for their image everytime they forget to fill in one of the required fields. But I can't get the upload image field to remember the POST values.

    I understand that <input type="file"> can't be set by most browsers as a security precaution (to avoid websites automatically grabbing user's files).

    Is there another way around this?
     
    Kerosene, Dec 17, 2008 IP
  2. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think you're wanting something like this:
    $_FILES['uploadedfile']['name']=$pathandfilename;
    PHP:
    I believe that auto-populates the input box you're using.
     
    Yesideez, Dec 17, 2008 IP
    Kerosene likes this.
  3. Kerosene

    Kerosene Alpha & Omega™ Staff

    Messages:
    11,366
    Likes Received:
    575
    Best Answers:
    4
    Trophy Points:
    385
    #3
    So I permit the form to go ahead and upload the file, even if some required fields haven't been completed... then go back and get them to complete the fields they missed?

    I'll give it a shot. Thanks :)
     
    Kerosene, Dec 17, 2008 IP
  4. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I've not used forms much but from what I remember that variable isn't read only.
     
    Yesideez, Dec 17, 2008 IP
  5. Kerosene

    Kerosene Alpha & Omega™ Staff

    Messages:
    11,366
    Likes Received:
    575
    Best Answers:
    4
    Trophy Points:
    385
    #5
    Bugger. I tried it, and there's 2 problems.

    1. $_FILES['image']['name'] only brings in the image name (e.g myimage.jpg), not the actual path to the file on the users PC.
    2. It still won't populate the value for <input type="file">
     
    Kerosene, Dec 17, 2008 IP
  6. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I believe it's a security precaution so forms can't be set to pull files from users' computers.
     
    Yesideez, Dec 17, 2008 IP
  7. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Just read your post back again - how about have Javascript validate the form first? That way if anything fails validation nothing is submitted and the form stays exactly as it is for editing!
     
    Yesideez, Dec 17, 2008 IP
  8. Kerosene

    Kerosene Alpha & Omega™ Staff

    Messages:
    11,366
    Likes Received:
    575
    Best Answers:
    4
    Trophy Points:
    385
    #8
    Yeah. That's what I thought (in my first post). I was hoping there was a way around it.

    I think I'll just have to upload the image, then send the user back to the form, and grey out the image field.
     
    Kerosene, Dec 17, 2008 IP
  9. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #9
    No need for that - what if the user decides not to carry on registering? What happens with the file?

    Just do as I suggested in my previous post - no need to upload anything until the Javascript validates the rest of the form although I'd still keep checking the variables with PHP when received just in case anyone tries to make their own HTML form bypassing the Javascript check.
     
    Yesideez, Dec 17, 2008 IP
  10. Kerosene

    Kerosene Alpha & Omega™ Staff

    Messages:
    11,366
    Likes Received:
    575
    Best Answers:
    4
    Trophy Points:
    385
    #10
    You must have been editing your post as I typed mine :)

    I'm so wrapped up in PHP that I forgot about JS form validating :p I'm getting dumber by the day...

    Cheerz :D + REP
     
    Kerosene, Dec 17, 2008 IP
  11. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Thank-you very much for the rep - I believe that's my first (even with this being my 110th post and I've only posted 1 intro topic and asked 1 question... not bad lol)
     
    Yesideez, Dec 17, 2008 IP
  12. m_wc

    m_wc Well-Known Member

    Messages:
    857
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    125
    #12
    m_wc, Dec 18, 2008 IP