DelphiFAQ Home Search:

InterBase, MS-SQL, mysql, Oracle
C#, C++, Delphi, Java,
JavaScript, perl, php, Visual Basic, VBScript
Apache, Network, Shell
Web Publishing
JavaScript, perl CGI, VBScript, Web Hosting
Apache, File Types, Internet Explorer,
Network, Printing, Processes
Outside the Cube
Auto, Computer Hardware,
Finances, Dating Scams,
Household, Male Dating Scammers,
Other Scams, Travel

Featured Article

Original file name in a http file upload form


I need to provide a file upload form on my web site and want to preserve the original file name.
Right now the uploaded files arrive on my web server in the temp folder and have file names like

How can I obtain the original file name?


The http upload protocol does not capture the local file name on the client side. What you can do is to write some javascript function that hooks into the file upload field's onchange() event.
There take the field's value (which will be the remote file name) and copy it into a hidden input field.
The hidden input field will be passed as part of your form.
You CGI script then can evaluate it and maybe do something useful with it.

Possible values for the remote file would be:

"c:\documents and settings\photo.jpg"

and others, depending on the remote operating system. Your CGI script needs to be prepared for all these possibilities and - in the examples - it probably should extract 'photo.jpg' as the file name.

The code below looks for the \ and for the / as path delimiters.

Make sure to set the form's encoding attribute with enctype="multipart/form-data"

<form enctype="multipart/form-data" action="" method=post>
 <script type="text/javascript" language="JavaScript">
 function file_onchange() {
   var s=document.getElementById('userfile').value;
   var r=s.lastIndexOf('/');
   if (r<0) {
   if (r>=0) {
 <input type=file name=userfile id=userfile onChange="file_onchange()">
 <input type=hidden name=remotename id=remotename>
 <input type=submit value="send file">

Generated 16:02:09 on May 27, 2020