Tim
Tim

Reputation: 143

PHP fopen doesn't find existing file

I'm currently writting a login-system with PHP, for that I need to read the files with some user-information in it. But after changing the folder system, PHP fopen doesn't read the files anymore.

Both the users.php and userinf.csv files are in the samle folder.

I allready tried to change the filepath, hard-coded the filepath , recreated the file. All of which file.

//Read file
$fp = fopen("userinf.csv", "r");
if(!$fp)
{
    echo "File couldn't be read";
    return false;
}

Before changing the file system, it worked. But now I am geting the error: Warning: fopen(userinf.csv): failed to open stream: No such file or directory in FILEPATH on line 45

Upvotes: 4

Views: 1485

Answers (2)

Koen Hollander
Koen Hollander

Reputation: 1711

When you use the fread function without any reference it could fail. I always say that you need to check your path first with getcwd()

<?php
echo getcwd(); //Current Working Directory
?>

Upvotes: 2

mwieczorek
mwieczorek

Reputation: 2252

Use absolute paths, always. It removes any ambiguity. Using a relative path may change based on where your script is located, among other things, depending on your system.

$fp = fopen("/home/somewhere/blah/userinf.csv", "r");

You can always use a variable for the path as well:

// Somewhere in your code
define('ROOT_PATH', "/home/somewhere/blah");

// In the implementation
$fp = fopen(ROOT_PATH . "/userinf.csv", "r");

Upvotes: 0

Related Questions