Page 1 of 1

Empty-handed Upload!

Posted: Tue Aug 16, 2005 3:00 am
by harrisonad
I want to check if a user had given an existing file for upload. But the $_FILES['userfile'] array only got the name of the file stripped of its directory.
If the user entered 'C:\documents\report1.xls', then $_FILES['userfile']['name'] only contains 'report1.xls'.

In order to get the complete directory of the file, I added a hidden field to the form to copy the value of file input to it upon for submission.

Code: Select all

<form ... onsubmit='filepath.value=userfile.value'>
// ...
<input type=hidden name='filepath'>
<input type=file name='userfile'>
// ...
</form>
The filepath hidden field will be sent on $_POST and the rest on $_FILES.

The problem is that all the function (at least, all I know) available for this situation, such as file_exists and is_file, are always returning false even if the file really exists.

Code: Select all

$filepath = $_POST['filepath'];
if(file_exists($filepath)){
// .... or
if(is_file($filepath)){
// always returns true
Am i missing something here?
How can I check if the file touplaoded exists?

Thanks in advance

Re: Empty-handed Upload!

Posted: Tue Aug 16, 2005 3:32 am
by anjanesh
harrisonad wrote: The problem is that all the function (at least, all I know) available for this situation, such as file_exists and is_file, are always returning false even if the file really exists.
These functions are related to files on your server (or maybe even remote url based filename) - not client filenames.
I dont think you can do any checking on the client part.

Posted: Tue Aug 16, 2005 3:34 am
by harrisonad
Is that so? Thank you sir.

Posted: Tue Aug 16, 2005 3:49 am
by anjanesh
BTW, just wanted to know if you managed to get $_POST['filepath'] the value of 'C:\documents\report1.xls'.
the input files is readonly and not writeable - but I didnt know you could obtain the value of a file input using JS.

Posted: Tue Aug 16, 2005 4:21 am
by harrisonad
anjanesh wrote:the input files is readonly and not writeable
I don't understand.
The filepath input is just a hidden field. I can't see why it can't be writable.

Posted: Tue Aug 16, 2005 4:29 am
by anjanesh
input file - I was refering to

Code: Select all

<input type=file name='userfile'>