0

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.

  • 4
    remove the `@`, and look at your error log for warnings/errors. – Luuk Dec 19 '20 at 11:21
  • Does this answer your question? [Reference — What does this symbol mean in PHP?](https://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) and https://stackoverflow.com/questions/4872340/should-i-use-in-my-php-code – Luuk Dec 19 '20 at 11:24
  • **WARNING**: Using the error-suppressing YOLO operator (`@`) obscures problems with your code and makes debugging issues like this a whole lot more complicated. That's a tool of last resort and should only be used in exceptional circumstances. You should display an error message for the user, log a problem, initiate some kind of retry, or all of these things in conjunction. – tadman Dec 19 '20 at 11:29
  • Do a `var_dump(is_file('../install.sql'))` and check if the path is correct. Also make sure the file isn't empty (since an empty string would be evaluated as false here.) But yes, as @Luuk said, start by remove the `@` since that _suppresses_ useful error messages. – M. Eriksson Dec 19 '20 at 11:29
  • Are paths relative to your web root, or are they relative to the script itself? – tadman Dec 19 '20 at 11:29
  • Relative to the script @tadman – Yiğit Kerem Oktay Dec 19 '20 at 12:07
  • @Luuk thanks but it doesn't look like helping much :) – Yiğit Kerem Oktay Dec 19 '20 at 12:07
  • Why did it not help much? The error says that it could not find that file at the given location. If it cannot find the file, then it is not able to read it..... – Luuk Dec 19 '20 at 12:15
  • But the file is there, check the source code and directory tree @Luuk That's what my error handling says as well. – Yiğit Kerem Oktay Dec 19 '20 at 12:19
  • Try adding `print realpath("../install.sql")`, If nothing is returned, then file cannot be found, if it is found the complete absolute path is returned. see: [realpath](https://www.php.net/manual/en/function.realpath) – Luuk Dec 19 '20 at 12:24
  • I have found the problem @Luuk. Will post it now. – Yiğit Kerem Oktay Dec 19 '20 at 12:37

1 Answers1

0

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

  • Why did you add the `@` ? – Luuk Dec 19 '20 at 12:53
  • Because then if it can't include it, it also adds an error message while running the installer. I just don't want it to say file not found while running the installer. If there is another way, please enlighten me @Luuk :smile: – Yiğit Kerem Oktay Dec 19 '20 at 14:10
  • [file_exists](https://www.php.net/manual/en/function.file-exists.php) ? – Luuk Dec 19 '20 at 14:13