2

I have the following simple script (stripped down):

define("LANG_DIR", "../../app/lang/");
// define("LANG_DIR", "/var/www/html/app/lang/");

// #############
// Some parsing of an CSV file, from with we will create .php files
// #############

foreach ($fileContent as $fileName => $value) {
    $fileString = FILE_START;

    foreach ($value as $arrayKey => $arrayValue) {
        $fileString .= TAB . "'" . $arrayKey . "'" . TAB . TAB . "=>" . TAB . TAB .  "'" . $arrayValue . "'," . NL;
    }

    $fileString .= FILE_END;

    $filePath = trim(LANG_DIR . $desLang . "/" . $fileName . ".php");
    echo $filePath;
    file_put_contents($filePath, $fileString);
}

And these are the error's I receive:

../../app/lang/de/behandeling.php
Warning: file_put_contents() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 61
../../app/lang/de/bewaarplaats.php
Warning: file_put_contents() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 61
../../app/lang/de/cron.php
Warning: file_put_contents() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 61
../../app/lang/de/gebruikers.php
Warning: file_put_contents() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 61
../../app/lang/de/handelshuis.php
Warning: file_put_contents() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 61
../../app/lang/de/heftrucks.php
Warning: file_put_contents() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 61
../../app/lang/de/history.php
Warning: file_put_contents() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 61

I get the same error when I use the other (commented) define.

The thing is, that I use the same define's in an another script which is in the same folder, and from there I can access the files. So the path's are right, only file_put_contents can't find it.

edit as requested by @e-Learner here's the output I get when I use

var_dump(is_file(LANG_DIR . $desLang . "/" . $fileName . ".php"));

// This will output:

Warning: is_file() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 63
NULL 
Warning: is_file() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 63
NULL 
Warning: is_file() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 63
NULL 
Warning: is_file() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 63
NULL 
Warning: is_file() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 63
NULL 
Warning: is_file() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 63
NULL 

edit 2 Changed some more code:

$filePath = trim(LANG_DIR . $desLang . "/" . $fileName . ".php");

echo $filePath . "<br />";
var_dump(is_file("/var/www/html/app/lang/de/behandeling.php"));
var_dump(is_file($filePath));

This will result in:

/var/www/html/app/lang/de/behandeling.php
/var/www/html/app/lang/de/behandeling.php 
bool(true) 
Warning: is_file() expects parameter 1 to be a valid path, string given in /var/www/html/public/scripts/languageImport.php on line 68

As you can see, the script can access the file when I use an whole string. But when I create the string on the fly (by inserting the $fileName), it goes wrong... But both strings are the same

Mathlight
  • 6,436
  • 17
  • 62
  • 107
  • 1
    why you changed an absolute path in relative in define? use absolute path, take for sure, that it is a folder, writable .. – donald123 Jun 29 '15 at 12:44
  • @donald123 That was just for testing, to check if that solved the problem. But it didn't, that's why it's commented. But i wanted to show that I tried that to – Mathlight Jun 29 '15 at 12:45
  • try doing your `trim()`-ing outside of the call to `file_put_contents`. – NaijaProgrammer Jun 29 '15 at 12:46
  • 1
    Test if your path is correct before `if(is_dir(LANG_DIR . $desLang . "/" . $fileName . ".php))` then `file_put_content` – e-Learner Jun 29 '15 at 12:48
  • @NaijaProgrammer tried, but didn't work – Mathlight Jun 29 '15 at 12:48
  • @e-Learner tried it as following: `if(is_dir(LANG_DIR . $desLang . "/" )){ file_put_contents($filePath, $fileString); }` but now I get the same error with the `is_dir` function – Mathlight Jun 29 '15 at 12:50
  • 1
    @Mathlight for test purpose don't use the file_put_content. You can copy/paste your `foreach` loop and comment the original. In the past loop remove file_put_content and `var_dump` results of `is_file` with the complete file name as `var_dump(is_file(LANG_DIR . $desLang . "/" . $fileName . ".php))` – e-Learner Jun 29 '15 at 12:59
  • @e-Learner I edited the question with the outcome of it. It's still giving the error... – Mathlight Jun 29 '15 at 13:21
  • 1
    try echo `$_SERVER("DOCUMENT_ROOT)` and as what you will see, provide the asolute path as `/var/www/app/lang/...` (if the app dir is in `www`) and test `is_file(/var/www/app/lang/de/gebruikers.php)` note that its depend that your app dir is located. if its in `html` your test path will be `is_file(/var/www/html/app/lang/de/gebruikers.php)` – e-Learner Jun 29 '15 at 14:11
  • @e-Learner Thank you for bearing this one with me. I've tried what you said, and I found something strange. When I use an fixed string, it's working. But when I use the dynamic string, It won't? – Mathlight Jun 29 '15 at 14:22

2 Answers2

5

Alright, after one big headace, I've found the solution, given by @naviciroel

I got the same error before but I don't know if this solution of mine works on your problem you need to remove the "\0" try replace it:

$cleaned = strval(str_replace("\0", "", $buttons_first));

Community
  • 1
  • 1
Mathlight
  • 6,436
  • 17
  • 62
  • 107
-1

Have the same problem here after moving to another provider.
I had no \0 bytes in the absolute path.

Strange...

touch($path_to_file); // works

// works too:
$fh = fopen($path_to_file, "w");
fwrite($fh, $data);
fclose($fh);
Harald Ernst
  • 364
  • 4
  • 10