Search code examples
phpfile-get-contents

PHP: Not able to get file contents


I want to get the contents of my install.sql file that sits one directory up but when I use file_get_contents("../install.sql") it returns false.

How can I fix it?

I have also tried to use fopen and fread.

<?php
 // Get the install SQL Files
 if (@$install_sql = file_get_contents("../install.sql")){

    // Prepare the statement to install
    $install_stmt = $conn_test->prepare($install_sql);

    // Execute the install
    $install_stmt->execute();
 } else {
    // This is where the code ends up
 }
?>

Code shortened
Full code available here

For people suggesting to remove '@' to show errors:

Warning:
file_get_contents(../install.sql): failed to open stream: No such file or directory in
/Users/yigitkerem/Code/SkyfallenSecureForms/Configuration/install.php
on line
55

For me it wasn't making any more sense, that was why I didn't include it, but here we go in case there is something that I don't know.


Solution

  • I have fixed the problem. I will now briefly describe it here as well.

    So this install file was called from another file,

    // Unless the installation was complete, the Database Config should exist, if not, run the install.
    if((@include_once SSF_ABSPATH . "/Configuration/SecureFormDatabaseConfiguration.php") === false){
    
        // Include the install file
        include_once SSF_ABSPATH."/Configuration/install.php";
    
        // Stop further execution
        die();
    }
    

    I was using the path relative to the install.php file inside the Configuration folder but it turns out the path I used, should have been relative to the file it made the call from.

    So to not come across the same problem in the future, I now define a ABSOLUTE PATH from the root file just like WordPress to use as a reference, as expected now I don't need to consider about where the file was called from.

    Thanks to @Luuk @tadman for helping me out