0

I have a form and when the user submits the form I want to save information in inputs, for cases where error happen, users dont need to write the information again.

So Im using this method in code below and it works fine, but only for my input type="file" dont works, when I submit the form, the file that the user choose is not saving.

Is necessary some trick in this case of <input type="file">?

 <form action="" method="post" enctype="multipart/form-data">

       <label class="line">
            <span class="data">Select an Image:</span>
            <input type="file" class="fileinput" name="thumb" size="60" value="<?php if(isset($_POST['thumb'])) echo $f['thumb'] ?>" />
        </label>

        <label class="line">
            <span class="data">Title:</span>
            <input type="text" name="title" value="<?php if(isset($_POST['title'])) echo $f['title']; ?>" />
        </label>

        <label class="line">
            <span class="data">Content:</span>
            <textarea name="content"  rows="15"><?php if(isset($_POST['content'])) echo htmlspecialchars($f['content']); ?></textarea>
        </label>

           <label class="line">
            <span class="data">Date:</span>
            <input type="text" name="date" value="<?php if(isset($_POST['date'])) echo $f['date']; else echo date('d/m/Y H:i:s'); ?>" />
        </label>

        <input type="submit" value="Insert" name="sendForm"/>

    </form>

My code where I Do the upload:

if(!empty($_FILES['thumb']['tmp_name']))
{
  $folder   = '../uploads/';
  $year     = date('Y');
  $month    = date('m');
  $img = $_FILES['thumb'];
  $ext = substr($img['name'],-3);
  uploadImage($img['tmp_name'], $f['url'].'.'.$ext, $folder.$year.'/'.$month.'/');

}
UserX
  • 1,295
  • 7
  • 25
  • 39
  • 1
    http://stackoverflow.com/questions/1696877/how-to-set-a-value-to-a-file-input-in-html – Vagabond Apr 30 '14 at 17:39
  • have you added enctype='multipart/form-data' to form ? – user3470953 Apr 30 '14 at 17:40
  • Yes, but I forget to put here, sorry. – UserX Apr 30 '14 at 17:41
  • As the link tramp points to says, you can't do this... if you could, you could reload any file name in the field and effectively have a way to download ANY file you would want. Obviously, that would be a little dangerous. – Mattt Apr 30 '14 at 17:46
  • please post the PHP code that does the actual uploading – Guns Apr 30 '14 at 17:47
  • Are you reading `$_FILE` in stead of `$_POST` in your php code? – Timmetje Apr 30 '14 at 18:14
  • Guns I update my post with the code where I do the upload! – UserX Apr 30 '14 at 18:29
  • Tim Dev I didnt understood your question! Can you please explain better? – UserX May 01 '14 at 00:05
  • tramp thanks for your answer, but that link you gave me is not about my issue... – UserX May 01 '14 at 00:20
  • _“is not the same of my problem”_ – it _is_, you just have not realized it yet. You __can not__ pre-fill the `value` of an `input type=file` – the user either has to chose the file _again_ if they fill out this form a second time due to errors in first submission, or you have to store the file that was uploaded on the _first_ submission somewhere on your server when you are processing that _first_ submission. – CBroe May 01 '14 at 00:23
  • Ah ok...I get it now, so its not correct doing this, even if it is possible with some trick, is not recommended for security reasons right? – UserX May 01 '14 at 00:33

0 Answers0