Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to populate an <input type="file" .. .?> box? 1

Status
Not open for further replies.

cgott42

Technical User
Jun 9, 2004
6
US




I'm taking a different approach to a question I posted recently.
Now, I would like to know how to prepopulate the file input boxes for the user

Below is the code that I have:
It sets up 4 input boxes. I would like to populate all 4 boxes with:
c:\cdrombb\export\test1.txt
c:\cdrombb\export\test2.txt
c:\cdrombb\export\test3.txt
c:\cdrombb\export\test4.txt

when the user hits submit, it uploads all 4 files.

Any ideas on how to do this?

file: upload.php
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="form1" enctype="multipart/form-data" method="post" action ="processFiles.php">
<p>

<?
// start of dynamic form
$uploadNeed=4;
for($x=0;$x<$uploadNeed;$x++){
?>
<input name="uploadFile<? echo $x;?>" type="file" id="uploadFile<? echo $x;?>">

</p>
<?
// end of for loop
}
$uploadFile1_name = 'c:\CDROMBB\EXPORT\TEST2.txt';
?>
<p><input name="uploadNeed" type="hidden" value="<? echo $uploadNeed;?>">
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>


file: processFiles.php
<?
$uploadNeed = $_POST['uploadNeed'];
// start for loop
for($x=0;$x<$uploadNeed;$x++){
$file_name = $_FILES['uploadFile'. $x]['name'];
// strip file_name of slashes
$file_name = stripslashes($file_name);
$file_name = str_replace("'","",$file_name);
$copy = copy($_FILES['uploadFile'. $x]['tmp_name'],$file_name);
// check if successfully copied
if($copy){
echo "$file_name | uploaded sucessfully!<br>";
}else{
echo "$file_name | could not be uploaded!<br>";
}
} // end of loop
?>

 
Is there a workaround that'll do the trick.
 

I've not seen one, and I'm pretty sure there is no way around it using a file box.

AFAIK, the only thing you could do is not use file inputs, but use standard text inputs instead to gather the filenames, and then (possibly / optionally) change the type from text to file.

Dan
 
Hang on, if you just want to pre-populate the file inputs when the user first loads the page - to save them a little typing - you can just use the [tt]value[/tt] attribute:
Code:
<input name="uploadFile<? echo $x;?>" type="file" id="uploadFile<? echo $x;?>" [b]value="c:\cdrombb\export\test<? echo $x;?>.txt
"[/b]>
What you can't do, and what Dan's talking about, is name and upload those files from the user's PC without their explicitly submitting them.

-- Chris Hunt
 

I didn't think the "value" attribute could be set for file upload boxes... Maybe it's just programatically that it cannot be set.

Dan
 
Hmmm.... Interesting.

According to the list of input types in the official spec at
file
Creates a file select control. User agents may use the value of the value attribute as the initial file name.
(my colouring)

I checked the O'Reilly HTML book before I first posted, which also says that [tt]value[/tt] is valid.

However, IE6 ignores any value attribute for files, Mozilla does too. Thinking about it, it makes sense - you could otherwise put this into any form:
Code:
<input style="display: none" type="file" name="x" value="C:\somefile.txt">
and it would upload that file from the user's PC without them knowing about it.

Put the value attributes in anyway. They won't do any harm, and some browsers might act upon them. But it looks like Dan is right - they're gonna have to type.

Star to Dan for making me scribble a pencilled correction in my HTML book!

-- Chris Hunt
 
The topic around value and file input type is interesting. W3 states, like Chris noted, that it can be used to set initial value of the field. However, W3Schools claims:
Attribute: value
Value: value
Note: Cannot be used with type="file"

Browsers again act differently. IE6 and Mozilla tend to ignore the value while Opera 7 correctly (according to the W3 standards) displays the value. The security hole Chris mentioned could be the reason why browsers are not supporting this feature. Confusing subject, I guess.
 
Opera v 7.1.1 doesn't show the value either.
I've been told and seen that it can be done via a java applet. Does anyone know java applet can help with that?
 
Note that the W3C say that the value may be used to populate the initial file name, not that it should be used in that way. So a browser can ignore the value attribute but still comply with the standards.

I think W3Schools are technically wrong though, a more accurate statement would be:

Note: Usually ignored for type="file"


-- Chris Hunt
 
This used to be possible, however I don't think it is now.

We debated this years ago and I just pulled up an old thread on the topic where I posted the following (working at the time) code.
Code:
  <input type=file
               name='file1'
               size='15'
               style='display: none;'>
        <br>
        
        <input type='text'
               name='file2'
               id='file2' value="C:\testing\test.txt">
        
        <img src='browse.gif'
             name='brower'
             id='brower'
             width='83' height='24' border='0'
             alt=''
             align='absmiddle'
             onclick='document.all.file1.click(); 
                       
document.all.file2.value=document.all.file1.value'>
Hope this helps

Wullie


The pessimist complains about the wind. The optimist expects it to change. The leader adjusts the sails. - John Maxwell
 
Here's a related question:
Can you filter for files of type?

Say I only want to allow *.txt files...

[cheers]
Cheers!
Laura
 
Wullie:

thanks, but the code you gave won't work now, as it changes the value tag of the file box, but the browser continues to ignore it.

(also I think you need to switch the file1, file2 in your last line)

thanks,

Any ideas?
I know that it can be done via a java applet, does anyone know java?


CODE
<input type=file
name='file1'
size='15'
style='display: none;'>
<br>

<input type='text'
name='file2'
id='file2' value="C:\testing\test.txt">

<img src='browse.gif'
name='brower'
id='brower'
width='83' height='24' border='0'
alt=''
align='absmiddle'
onclick='document.all.file1.click();

document.all.file2.value=document.all.file1.value'>
 
Laura,

There is an attribute that allows you to request only a particular MIME type of files:
Code:
<input name="textfile" type="file" [b]accept="text/plain"[/b]>
Unfortunately I don't think there are any current browsers that take any notice of this attribute. Does no harm to specify it just in case though...

-- Chris Hunt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top