Jump to content
  • 0

Предупреждение при использовании file_get_contents


Cerberus
 Share

Question

Добрый день.

Есть массив с именами файлов (файлы существуют):


$arrFiles = (
[0] => "id_1.txt",
[1] => "id_2.txt",
[2] => "id_3.txt"
);

Который получается после


$arrFiles = file(names.txt);

Хочу в цикле получить содержимое файлов в строку


for($i=0; $i <= 2; $i++){
$content = file_get_contents("dump/".$arrFiles[$i]);
}

Но получаю вот такое

Warning: file_get_contents(dump/id_1.txt ) [function.file_get_contents]: failed to open stream: Invalid argument in Z:\home\test\test.php on line 8
на каждом цикле. При этом если написать

$content = file_get_contents("dump/id_1.txt");

всё нормально. Не могу понять, что не так?

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

а почему бы не сделать вот так?


foreach($arrFiles as $arrItemFiles) {
$content[] = file_get_contents("dump/".$arrItemFiles);
}

в $content будет массив строк полученных из файлов с которым так же можно потом работать, или я чего то не понял?

Link to comment
Share on other sites

  • 0

С foreach тоже хороший вариант, но ругается именно на file_get_contents.

Свою ошибку я уже нашёл, спасибо откликнувшимся.

После


$path = "dump/".$arrFiles[$i];
$path = trim($path);
$content = file_get_contents($path);

предупреждения больше нет. Как говорится: "Проверяй, что за путь подсовываешь" :-)

Link to comment
Share on other sites

  • 0

Проверил файл с именами (на самом деле размер файла несколько больше, чем я указал), мда, действительно после расширения файла очень часто встречается знак пробела.....

А поскольку после file в массив попадает строка целиком, то получил то, что получил.

Благодарю всех откликнувшихся!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. See more about our Guidelines and Privacy Policy