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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot open a file

Status
Not open for further replies.

xtreemnet

Programmer
Joined
Aug 9, 2003
Messages
88
Location
NZ
Hi

I am new to PHP. I am trying to open a file using "fopen", but its not working. following is the code I am using:

<html>
<?php
$filename = 'chees.doc';
$fp = fopen($filename, "r");

if(!$fp) {
echo "Cannot open CSV file ($filename)";
return -1;
}
echo "Opened CSV file ($filename)";
fclose ($fp);
?>
</html>

This works when run from local machine. but its uploaded on the web site, it says "Cannot open CSV file (chees.doc)".
This is such a simple code, but I do not understand what's wrong.

Thanks,
 
I would guess its a problem with the server finding the file.

I think you would have to specify the path from the root of your server to the file.
 
chees.doc will have to be in the same directory as your PHP file. If it's not, change this: $filename = 'chees.doc'; to something like this: $filename = 'files/chees.doc';. (Of course "files" would be the actual name and path of the directory where cheese.doc resides.

There's always a better way. The fun is trying to find it!
 
The file "chees.doc" is in the same directory as my php file.
 
All PHP scripts run through a web server have the permissions of the user as which the web server runs.

What is that user, and does that user have permission to read the file?

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I will check the permissions and report back.
 
What I am trying to do is developing a web page where the user can browse for a CSV file on his local drive and then click on a button which calls another php page where a script reads the csv file and inserts the data into mysql table. But I am stuck with reading the file.

So if a user is selecting a local file, does he still needs to have permissionto read the file?
 
You need to upload the file to the server so that the webserver can include() it.

fopen() requires an absolute path, and this would be RatherTricky(tm) without uploading it :-)

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Hi

Its working now. I had to use the form with "enctype" set to "multipart/form-data".

Thanks everybody.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top