Empty-handed Upload!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Empty-handed Upload!

Post 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
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Re: Empty-handed Upload!

Post 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.
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post by harrisonad »

Is that so? Thank you sir.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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.
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post 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.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

input file - I was refering to

Code: Select all

<input type=file name='userfile'>
Post Reply