「PHP Programming/Files」の版間の差分
削除された内容 追加された内容
M +category |
Use lowercase variable and function names for consistency throughout the book and usual conventions |
||
1 行
Working with files is an important part of any programming language and PHP is no different. Whatever your reasons are for wanting to manipulate files, PHP will happily accommodate them through the use of a handful of functions. You should have read and be comfortable with the concepts presented in the first five sections of this book before beginning here.
==
{{Code:Basic
8 行
|<source lang="php">
<?php
$
fclose($
?>
</source>
29 行
|<source lang="php">
<?php
$
echo $
?>
</source>
40 行
|<source lang="php">
<?php
$
foreach($
$
echo "Line $
}
?>
56 行
|<source lang="php">
<?php
$
$
fclose($
echo $
?>
</source>
71 行
|<source lang="php">
<?php
$
function
if(false !== strpos($
elseif(false !== strpos($
else return "\n";
}
/* This is equivalent to file_get_contents($
$
$
fclose($
/* This is equivalent to file($
type. Windows systems use \r\n, Macintosh \r and Unix \n. File($
automatically detect line-endings whereas fread/file_get_contents won't */
$
$
$
/* This is also equivalent to file_get_contents($
$
$
/* This is equivalent to fread($
$
$
?>
</source>
111 行
|<source lang="php">
<?php
$
$
fwrite($
fclose($
echo file_get_contents('data.txt');
128 行
|<source lang="php">
<?php
$
$
fwrite($
fclose($
echo file_get_contents('data.txt');
145 行
|<source lang="php">
<?php
$
$
fwrite($
fclose($
echo file_get_contents('newfile.txt');
172 行
|<source lang="php">
<?php
$
if(!$
echo 'PHP does not have permission to read this file or the file
in question doesn\'t exist.';
} else {
$
fclose($
}
$
if(!$
echo 'PHP either does not have permission to write to this file or
it does not have permission to create this file in the current directory.';
} else {
fwrite($
fclose($
}
$
if(!$
echo 'Either this file exists or PHP does not have permission to
create this file in the current directory.';
} else {
fwrite($
fclose($
}
?>
207 行
|<source lang="php">
<?php
$
if(!file_exists($
// No point in reading since there is no content
$
// But might want to create the file instead
$
if(!$
echo 'PHP does not have permission to create a file in the current directory.';
} else {
fwrite($
fclose($
}
} else {
// The file does exist so we can try to read its contents
if(is_readable($
$
} else {
echo 'PHP does not have permission to read that file.';
230 行
}
if(file_exists($
$
fwrite($
fclose($
}
?>
248 行
Why is this important? Well, when you read a file into a string with [http://php.net/file_get_contents file_get_contents()], the string will be one long line with those line-endings all over the place. Sometimes they will get in the way of things you want to do with the string so you can remove them with:
<source lang="php"><?php
$
?></source>
Other times you may need to know what kind of line-ending is being used throughout the text in order to be consistent with any new text you add. Luckily, in 99% of cases, the line-endings will never change type throughout the text so the custom function '
<source lang="php"><?php
function
if(false !== strpos($
elseif(false !== strpos($
else return "\n";
}
281 行
<?php
/* This part of the script saves the data to a file */
$
'id' => 114,
'first name' => 'Foo',
288 行
'country' => 'England'
);
$
$
fwrite($
fclose($
/* Then, later on, we retrieve the data from the file and output it */
$
$
$
foreach($
$
$
}
echo $
?>
</source>
322 行
|<source lang="php">
<?php
$
$
// PHP 4, overwrite entire file with data
$
fwrite($
fclose($
// PHP 5
file_put_contents($
// PHP 4, append to a file
$
fwrite($
fclose($
// PHP 5
file_put_contents($
?>
</source>
349 行
<?php
if(!function_exists('file_put_contents')) {
function file_put_contents($
if(!$
else $
$
if(!$
$
fclose($
return $
}
}
|