【发布时间】:2020-12-24 09:59:39
【问题描述】:
我正在尝试使用 PHP 获取文件的内容。然后将其添加到新文件中。我正在使用file_get_contents(),但它显示错误:
PHP Warning: file_get_contents(./home-starter.php): failed to open stream: No such file or directory
即使路径是正确的。两个文件都在同一个文件夹中。
Main Folder
errorfile.php
home-starter.php
有什么建议吗?
<?php
require_once('private/initialize.php');
$city = 'Lahore';
$keyword = 'Homemade Food';
$areas = ['DHA', 'Bahria Town', 'Cantt', 'Gulberg', 'Valencia Town', 'Sui Gas Housing Society', 'Izmir Town', 'Faisal Town', 'Garden Town', 'Johar Town', 'Punjab Housing Society', 'Canal Housing Societies', 'State Life Housing Society', 'Model Town', 'Liaqatabad', 'Rehmanpura', 'Samanabad', 'Muslim Town', 'Town Ship', 'Iqbal Town', 'Ferozepure Road', 'Multan Road', 'Baghbanpura', 'Mughalpura', 'Walton'];
foreach ($areas as $area) {
$permalink = strtolower(str_replace(' ', '-', $keyword)) . '-' . strtolower(str_replace(' ', '-', $area)) . '-' . strtolower($city);
$page_title = 'Homemade Food Delivery - Lunch Dinner - ' . $area . ' - ' . $city;
$meta_desc = 'Lunch.pk is Pakistan #1 website to order homemade food in ' . $area . ' ' . $city . '. You get tasty and healthy food cooked and delivered by families near you.';
$location = $area . ' ' . $city;
$filename = $permalink . '.php';
$content = file_get_contents( __DIR__ . '/home-starter.php');
$content = str_replace('[page-title]', $page_title, $content);
$content = str_replace('[meta-desc]', $meta_desc, $content);
$content = str_replace('[location]', $location, $content);
$is_file_created = file_put_contents($filename, $content);
if (!$is_file_created) {
echo 'Err';
break;
}
}
【问题讨论】:
-
试试
file_get_contents(__DIR__ . '/home-starter.php'),看看有什么不同。 -
@MagnusEriksson 还是一样!
-
在此处粘贴实际代码。可能是错字什么的。
-
如果你回显
getcwd()怎么办?这是正确的路径吗? -
@Michel 我已经复制了它打印的路径但仍然是同样的错误:(
标签: php file-get-contents