<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Уроки PHP, Mootools, JavaScript, ActionScript, HTML, CSS &#187; PHP</title>
	<atom:link href="http://php.infoniac.ru/tag/php/feed" rel="self" type="application/rss+xml" />
	<link>http://php.infoniac.ru</link>
	<description>Уроки программирования от новичка новичкам</description>
	<lastBuildDate>Thu, 09 Sep 2010 18:22:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Как правильно читать файлы с помощью PHP?</title>
		<link>http://php.infoniac.ru/php/fwrite.html</link>
		<comments>http://php.infoniac.ru/php/fwrite.html#comments</comments>
		<pubDate>Sun, 05 Oct 2008 13:21:17 +0000</pubDate>
		<dc:creator>proglammer</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[файл]]></category>
		<category><![CDATA[читать файлы]]></category>

		<guid isPermaLink="false">http://php.infoniac.ru/?p=6</guid>
		<description><![CDATA[Как правильно читать файлы с помощью PHP? Этим вопросом задавался любой уважающий себя web-coder. Базовыми функциями для работы с файлами, являются: fopen, fclose и feof; функции чтения: fgets, fgetss и fscanf. Для упрощения работ с файлами был составлен класс. Составляя новый код, каждый раз при возвращении к данной проблеме &#8211; нужно просто подключить файл с [...]]]></description>
			<content:encoded><![CDATA[<p>Как правильно читать файлы с помощью PHP? Этим вопросом задавался любой уважающий себя web-coder.  </p>
<p>Базовыми функциями для работы с файлами, являются: <strong>fopen</strong>, <strong>fclose </strong>и <strong>feof</strong>; функции чтения: <strong>fgets</strong>, <strong>fgetss</strong> и <strong>fscanf</strong>.</p>
<p>Для упрощения работ с файлами был составлен класс.  Составляя новый код, каждый раз при возвращении к данной проблеме &#8211; нужно просто подключить файл с этим классом, что даёт возможность использовать минимальное количество времени и кода для решения задач обработки файлов.</p>
<p><span id="more-6"></span></p>
<p><strong>Листинг 1. Пример класса для работы с файлами:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>?
<span style="color: #000000; font-weight: bold;">class</span> writefile <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$fp</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$patch</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">function</span> close<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>	<span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">function</span> write<span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fp</span><span style="color: #339933;">,</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">function</span> read<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$fs</span><span style="color: #339933;">=</span><span style="color: #990000;">filesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">patch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$fs</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot; <span style="color: #006699; font-weight: bold;">{$this-&gt;patch}</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fp</span><span style="color: #339933;">,</span><span style="color: #990000;">filesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">patch</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">function</span> open<span style="color: #009900;">&#40;</span><span style="color: #000088;">$patch</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">patch</span><span style="color: #339933;">=</span> <span style="color: #000088;">$patch</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fp</span><span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$patch</span><span style="color: #339933;">,</span><span style="color: #0000ff;">''</span>w<span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">function</span> open_r<span style="color: #009900;">&#40;</span><span style="color: #000088;">$patch</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">patch</span><span style="color: #339933;">=</span> <span style="color: #000088;">$patch</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fp</span><span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$patch</span><span style="color: #339933;">,</span><span style="color: #0000ff;">''</span>r<span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>strong<span style="color: #339933;">&gt;</span>Пример использования класса для работы с файлами<span style="color: #339933;">:&lt;/</span>strong<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>?
<span style="color: #339933;">...</span>
<span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span>classes<span style="color: #339933;">/</span>writefile<span style="color: #339933;">.</span>php<span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// подключаем класс</span>
<span style="color: #339933;">...</span>
<span style="color: #000088;">$writefile</span><span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> writefile<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$writefile</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">open_r</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #990000;">mail</span><span style="color: #339933;">.</span>tpl<span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// открываем для чтения</span>
<span style="color: #000088;">$mailTpl</span><span style="color: #339933;">=</span> <span style="color: #000088;">$writefile</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// возвращаем содержимое файла</span>
<span style="color: #339933;">...</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://php.infoniac.ru/php/fwrite.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
