T-Bird Posted March 1, 2009 Share Posted March 1, 2009 K, so here's the deal. I want to get the full name (including path) of a file to be uploaded from a user. I stumbled upon the <input type="file"> HTML element but that doesn't do quite what I want. I don't need to actually upload the file, this page is going to be on the company's intranet, and all I need is the file path/name to store in the DB. These are potentially 2gb .zip files. It would be nonsensical to spend several minutes to accept the whole file upload when all I need is the name. Is there any way I can continue to use the file upload input from my HTML form and tell PHP to not accept the file, but still get the name. If not is there a similar HTML element I can use to keep the convenient Browse button. If necessary I'll just use a text box in which they will copy/paste the file path, but that's not as user-friendly. Thanks in advance for the help. Quote Link to comment Share on other sites More sharing options...
sillysillysilly Posted March 1, 2009 Share Posted March 1, 2009 I think this tutorial will help you. http://www.tizag.com/phpT/fileupload.php Quote Link to comment Share on other sites More sharing options...
T-Bird Posted March 1, 2009 Author Share Posted March 1, 2009 *sigh* I'm not one to look a gift horse in the mouth, but I would appreciate it if you'd actually read my request next time. I am fully aware of how to upload a file, my point is that I don't want to lag the user through a several minute waiting period of uploading a 2gig file just so I can keep the name/path and toss the rest. I'm looking for a way to bend the file input's functionality to not actually accept the file, just grab it's name. Thanks for the response, but not what I needed. Quote Link to comment Share on other sites More sharing options...
trq Posted March 1, 2009 Share Posted March 1, 2009 Sorry I can't offer I solution to this issue I can however tell you one thing, the solution won't be found server-side. this definately sounds like a job for javascript. In fact a quick search found this that might go close. Id take a look around jQuery aswell. Quote Link to comment Share on other sites More sharing options...
T-Bird Posted March 2, 2009 Author Share Posted March 2, 2009 Thank you. It hadn't occurred to me to trick JS into giving me the browse dialog. I had posted it in the server side help request because I thought there may be a PHP trick to block out the actual file transfer. I'll look into that article some more then flip this to solved if it works. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 2, 2009 Share Posted March 2, 2009 Create the form with an upload field and a hidden field. Then on submission of the form, using javascript, populate the hidden field with the value of the upload field and remove the upload field. Here's an example: <?php if(isset($_POST)) { $post = "Here are the posted values:<br />"; foreach($_POST as $name => $value) { $post .= "<b>Name:</b> $name<br /><b>Value: </b>$value<br /><br >\n"; } } ?> <html> <head> <script type="text/javascript"> function submission() { var uploadObj = document.getElementById('uploadFld'); var textObj = document.getElementById('file_name'); textObj.value = uploadObj.value; uploadObj.parentNode.removeChild(uploadObj); return true; } </script> </head> <body> <?php echo $post; ?> <form method="POST" onsubmit="return submission();"> Choose a file to upload: <input name="upload" id="uploadFld" type="file" onchange=""/><br /> File Path: <input type="hidden" name="file_name" id="file_name" value="" /><br /> <input type="submit" value="Upload File" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
killah Posted March 2, 2009 Share Posted March 2, 2009 Could you not just do something like this: <?php if(isset($_POST['file_name'])) { $file = $_FILES['file_name']['name']; mysql_query("INSERT INTO table (file,user) VALUES ('".$file."','".$user."')"); echo 'File inserted into table.'; } else { echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post"> File: <input type="file" name="file_name" /><br /> <input type="submit" value="Upload" /> </form>'; } But just exclude the actual create file content? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted March 2, 2009 Share Posted March 2, 2009 Could you not just do something like this: <?php if(isset($_POST['file_name'])) { $file = $_FILES['file_name']['name']; mysql_query("INSERT INTO table (file,user) VALUES ('".$file."','".$user."')"); echo 'File inserted into table.'; } else { echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post"> File: <input type="file" name="file_name" /><br /> <input type="submit" value="Upload" /> </form>'; } But just exclude the actual create file content? Except you'll only get the filename and not the path that way. Quote Link to comment Share on other sites More sharing options...
killah Posted March 2, 2009 Share Posted March 2, 2009 Cant ['name'] be changed to ['path'] then? Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 2, 2009 Share Posted March 2, 2009 Could you not just do something like this: <?php if(isset($_POST['file_name'])) { $file = $_FILES['file_name']['name']; mysql_query("INSERT INTO table (file,user) VALUES ('".$file."','".$user."')"); echo 'File inserted into table.'; } else { echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post"> File: <input type="file" name="file_name" /><br /> <input type="submit" value="Upload" /> </form>'; } But just exclude the actual create file content? I'm pretty sure the file is already uploaded to a temporary location by the time your processing script begins to execute. The OP wants to avoid the upload process. From w3schools: $_FILES["file"]["tmp_name"] - the name of the temporary copy of the file stored on the server Another thought: It might be enough to just not include the enctype parameter for the form to ensure the file is not uploaded (not tested). Would probably want to test this on a few different browsers to be sure. 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.