timstring67 Posted March 14, 2013 Share Posted March 14, 2013 I borrowed this from several different sources. The HTML code: <form action="ImportTOA.php" method="POST" accept="text/csv"> <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> Upload Daily TOA Logs: <br /><input name="userfile" type="file" onchange="this.form.submit()" /> </form> and then, the PHP script: $ImportFile = $_FILES['userfile']['name']; For some strange reason, the form is not passing 'username' to the action file. What is the simple solution that I'm overlooking? Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 14, 2013 Share Posted March 14, 2013 No "strangeness" to it. Take a look at this tutorial on file uploads and you should see what the problem is. http://www.tizag.com/phpT/fileupload.php Quote Link to comment Share on other sites More sharing options...
TimString Posted March 14, 2013 Share Posted March 14, 2013 Sorry, I did not mention at all what I'm trying to accomplish. I'm not trying to upload a file. There are many tutorials on that. All I need is to pass a file name from the form to my php action script, and let the script fold, spindle, and mutilate that file to put it into a MySQL database. The form calls up the file dialog, but doesn't pass the file name that is selected. Quote Link to comment Share on other sites More sharing options...
TimString Posted March 14, 2013 Share Posted March 14, 2013 (edited) OK. With a sufficiently narrow Google query, I found the answer. Should anyone come along to read this thread, here's the code I borrowed that worked: <form enctype="multipart/form-data" action="ImportTOA.php" method="POST" accept="text/csv"> <!-- MAX_FILE_SIZE must precede the file input field --> <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> <!-- Name of input element determines name in $_FILES array --> Upload Daily TOA Logs: <br /><input name="userfile" type="file" onChange = "this.form.submit()" /> </form> Edited March 14, 2013 by TimString Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 14, 2013 Share Posted March 14, 2013 Hmm . . . the fact that you say you don't want to actually upload the file is irrelevant because you are using a FILE input field. The sole purpose of that field is to upload a file. And the reason you were not getting the file name (which is included as part of the file upload) is specifically because you didn't configure your form for a file upload. If you had actually read the tutorial I linked to, that should have been apparent. To do a file upload you must set the form enctype to "multipart/form-data". Since you were not doing that, none of the information for that FILE field was being passed. So, even though you say you don't want to upload the file, it is still being passed in the form submission. If you aren't going to actually save/read that file and only want the name, there is another option. If this is for an internal tool where you have a known user base and can be certain they have JavaScript enabled, you can set the form to NOT upload the file. Then, include the FILE field in the form and add an onchange event to the field to populate a hidden field with the value of the FILE field. Then, when the form is submitted the FILE field will not be included in the POST data, but you will have the name in the hidden field. File: <input type="FILE" id="file" onchange="document.getElementById('filename').value=this.value;" /><br> FileName: <input type="text" name="filename" id="filename" /> 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.