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.

11
  • 4
    remove the @, and look at your error log for warnings/errors. Commented Dec 19, 2020 at 11:21
  • Does this answer your question? Reference — What does this symbol mean in PHP? and stackoverflow.com/questions/4872340/should-i-use-in-my-php-code Commented Dec 19, 2020 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. Commented Dec 19, 2020 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. Commented Dec 19, 2020 at 11:29
  • Are paths relative to your web root, or are they relative to the script itself? Commented Dec 19, 2020 at 11:29

1 Answer 1

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

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

3 Comments

Why did you add the @ ? 😥
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:

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.