fry2010 Posted May 4, 2009 Share Posted May 4, 2009 Im not sure if this is really the right place for it. Basically im trying to specify the actuall path of an uploaded file. So basically the user is filling out a form, and they upload an image. Lets say the form has errors in it. At the moment I have it so that it will tell them what errors were made when they submit it. It will show them the form again but with the correct values already stored in the fields so they dont end up typing it out again. I would like to get this to work for the file upload field aswell. I have tried the following: <?php $img_file = $_FILES['img']; // DO FILE CHECKS ETC.... // THERE WAS AN ERROR IN THE FORM, BUT THE FILE UPLOAD WAS OK... if($error) { if(!$img_file_error) { ?> <input type="file" name="img" <?php echo 'value="'.$img_file['tmp_name'].'"; ?>> <?php } ?> So what I have tried to do is the same as you would do on a normal text field, by setting the value="" to what I want inside that field. But this doesnt work with type="file" fields. Is there another attribute that I can specify the directory that the user submits? I no it can be done with js, but this form is for people who dont have js enabled. Anyone have any ideas? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 $_FILES['img']['name']? Quote Link to comment Share on other sites More sharing options...
fry2010 Posted May 4, 2009 Author Share Posted May 4, 2009 yeah I tried that too, unfortunatly its the same thing. I have echoed out the values that $_FILES['img']['name'] and they have the correct text, its just not putting it inside the field. Should it be doing so then? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Are you aware there is an unterminated string in your echo? Quote Link to comment Share on other sites More sharing options...
fry2010 Posted May 4, 2009 Author Share Posted May 4, 2009 oh yeah that happened aswell, but I sorted that out. Dont no how you spotted it though without having an error thrown at you lol. Its there just because I hand typed that just now, so didnt check with that. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Did that fix it? Quote Link to comment Share on other sites More sharing options...
fry2010 Posted May 4, 2009 Author Share Posted May 4, 2009 No no.. that happened ages ago, its stil not happening. Infact I have checked the source code and it shows that its in the value="" field as: value="C:\WINDOWS\Temp\php1E1.tmp" Thats for ['tmp_name'] and for ['name'] it is: value="Sample.jpg" and that is in the actual HTML source code, so I imagine you cant use value="" to specify what you want inside the file type... Quote Link to comment Share on other sites More sharing options...
mapleleaf Posted May 4, 2009 Share Posted May 4, 2009 This looks like your problem: <input type="file" name="img" <?php echo 'value="'.$img_file['tmp_name']."; ?>> you can't do that as far as I know Quote Link to comment Share on other sites More sharing options...
fry2010 Posted May 4, 2009 Author Share Posted May 4, 2009 You mean that you cant use a value="" field to specify what you want inside a type="file"? or that my code had a delimeter missing? it should have been like this, i just made a typo ignore that: <input type="file" name="img" <?php echo 'value="'.$img_file['tmp_name'].'"'; ?>> Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 <input type="file" name="img" <?php echo 'value="'.$img_file['tmp_name'].'"'; ?> /> Quote Link to comment Share on other sites More sharing options...
fry2010 Posted May 4, 2009 Author Share Posted May 4, 2009 No not that either, I missed that out aswell lol, iv been trying to do valid html since reading another article about it. Here is the exact output from my browsers source code: <?php <div> <label for="main_img" >Main Image: (This will be visible as part of your listing, outside your main page. We recommend an image size of around 800px on the longest side.)</label> <input type="file" name="main_img" value="C:\WINDOWS\Temp\php1E8.tmp" /> ?> And in my actuall form field for the "main_img" field it is blank, no text at all... I really appreciate ur guys help in this. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 From Google, I read that it's not possible due to security issues. Quote Link to comment Share on other sites More sharing options...
fry2010 Posted May 4, 2009 Author Share Posted May 4, 2009 oh rite, that explains it then. However, I have come accross a site that actually keeps the value of the file upload inside it when you do a page refresh. I have switched javascript off and it still works?? I wonder how he did it? http://www.elated.com/articles/html-forms--hidden-fields-password-fields-and-file-upload-fields/ At the bottom there is a file upload field, and putting a file in there and then refreshing with js turned off it still remains there.. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted May 4, 2009 Share Posted May 4, 2009 That's because it is the browser keeping the value in the field on a page refresh. The only way to put a value in a file field is by using the browser's file selection dialog box. HTML and javascript cannot put a value in. Quote Link to comment Share on other sites More sharing options...
fry2010 Posted May 4, 2009 Author Share Posted May 4, 2009 Oh right. I thought I read somewhere about javascript being able to do so... Probably not exactly what I was looking for though I just skimmed it because it was js. I suppose the only real option I have is if the file validates, just place it underneath the file input box to show they already have it loaded. It will save them the hassle of having to search their files again. Thanks for the help ken, PFMa...., and maple. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Yeah I agree PFMaBiSmAd is such a long and hard name to spell. I have no idea how he manages to spell that every time logging in. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.