4

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

3
  • 3
    When you execute the getcwd() function, do you get the right directory? Commented Jan 14, 2019 at 8:23
  • That's it! It stays in the folder-location of my index.php instead of the users.php Thank you! Commented Jan 14, 2019 at 8:24
  • 2
    @Tim Doesn't matter where your file located.. The matter is the location where you are calling the file. Good Job #Koen Commented Jan 14, 2019 at 8:27

2 Answers 2

2

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
?>
Sign up to request clarification or add additional context in comments.

1 Comment

This isn't really the answer, it should be something like fopen(dirname(__FILE__). ' /userinf.csv', 'r')
0

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");

1 Comment

Using relative paths for final location based on location of script can be desirable sometimes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.