2

I am trying to open a file for reading in php script but having trouble.

This is my code

$fileHandle = fopen("1234_main.csv", "r")or die("Unable to open");
if (!file_exists($fileHandle))
{
    echo "Cannot find file.";
}

The script is in the same directory as the file I am trying to read and there are no other read/write permission errors as I can create/read other files in the same directory.

When I run the script I just get the "Cannot find file" error message. Why is this error message being shown? Surely if fopen() can't open the file the "or die statement" should end the script?

Also, why can't I open the file when it definitely exists and is in the same location as the script (I have also tried using the full path of the filename instead of just the filename).

I am fairly new to php (but have exp in c++) so if its a stupid question I apologize.

Many thanks

4 Answers 4

3

In PHP, file_exists() expects a file name rather than a handle. Try this:

$fileName = "1234_main.csv";
if (!file_exists($fileName))
{
    echo "Cannot find file.";
} else {
    $fileHandle = fopen($fileName, "r")or die("Unable to open");
}

Also keep in mind that filenames have to be specified relative to the originally requested php-script when executing scripts on a web server.

Sign up to request clarification or add additional context in comments.

4 Comments

ok - good to know about file_exists() but it still can't find the file.
How are you executing the script? For web servers, the filename is relative to the webroot.
Web server. Do I need to put a file path in if the script is in the same directory as the file that's being opened?
That depends whether you request the php file directly or include it from another file. The .csv path has to be provided relatively to the requested .php
1

You can use file_get_content() for this operation. On failure, file_get_contents() will return FALSE.For example

$file = file_get_contents('1234_main.csv');

if( $file === false ){
    echo "Cannot find file.";   
}

2 Comments

Use $file === false in this case! Check the big red hint: php.net/manual/en/function.file-get-contents.php Also, OP might not want the entire contents.
Maybe we can use function file() for this situation.
0

file_exists() take the file-name as input, but the logic of your code has problem. You first try to open a file then you check its existence?

You first should check its existence by file_exists("1234_main.csv") and if it exists try to open it.

14 Comments

Yeh my logic is backwards - I think it was that i put the file_exists() in when trying to find out why I couldn't open a file an was trying to find out why.
How do you know it doesn't open it? if it couldn't you should receive "Unable to open"
after attempting to open the file. I was testing whether the $fileHandle == null. and that was evaluating to true.
Try to read it by $content = fread($fileHandle, filesize($filename)); and echo $content to see if you can read it or not.
@unknownSPY fopen returns FALSE on error, you should check it against FALSE
|
0

file_exists takes a string, not a file handle. See http://php.net/manual/en/function.file-exists.php

Comments

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.