Jump to content

Get Filename


T-Bird

Recommended Posts

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.

Link to comment
Share on other sites

*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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.