PHP/File Directory/file put contents

Материал из Web эксперт
Перейти к: навигация, поиск

Changing a file in place

 
<?php
$contents = file_get_contents("pickles.txt");
$contents = strtoupper($contents);
file_put_contents("pickles.txt", $contents);
?>



Checking for an error from file_put_contents()

 
<?
$zip = 10040;
$weather_page = file_get_contents("http://www.demo.ru/z.php?inputstring=" . $zip);
if ($weather_page === false) {
    print "Couldn"t get weather for $zip";
} else {
    $page = strstr($weather_page,"AAA");
    $table_start = strpos($page, "<table");
    $table_end  = strpos($page, "</table>") + 8;
    $forecast = substr($page, $table_start, $table_end - $table_start);
    print $forecast;
    $saved_file = file_put_contents("weather-$zip.txt", $matches[1]);
    if (($saved_file === false) || ($saved_file == -1)) {
        print "Couldn"t save weather to weather-$zip.txt";
    }
}
?>



Saving a file with file_put_contents()

 
$zip = 98052;
$weather_page = file_get_contents("http://www.demo.ru/z.php?inputstring=" . $zip);
$page = strstr($weather_page,"Detailed Forecast");
$table_start = strpos($page, "<table");
$table_end  = strpos($page, "</table>") + 8;
$forecast = substr($page, $table_start, $table_end - $table_start);
print $forecast;
file_put_contents("weather-$zip.txt", $forecast);