<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/class.phardata.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'phardata.addfromstring.php',
    1 => 'PharData::addFromString',
    2 => 'Add a file from a string to the tar/zip archive',
  ),
  'up' => 
  array (
    0 => 'class.phardata.php',
    1 => 'PharData',
  ),
  'prev' => 
  array (
    0 => 'phardata.addfile.php',
    1 => 'PharData::addFile',
  ),
  'next' => 
  array (
    0 => 'phardata.buildfromdirectory.php',
    1 => 'PharData::buildFromDirectory',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/phar/PharData/addFromString.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="phardata.addfromstring" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">PharData::addFromString</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8, PECL phar &gt;= 2.0.0)</p><p class="refpurpose"><span class="refname">PharData::addFromString</span> &mdash; <span class="dc-title">Add a file from a string to the tar/zip archive</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-phardata.addfromstring-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>PharData::addFromString</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$localName</code></span>, <span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$contents</code></span>): <span class="type"><a href="language.types.void.php" class="type void">void</a></span></div>


  <p class="para rdfs-comment">
   With this method, any string can be added to the tar/zip archive.
   The file will be stored in the archive with <code class="literal">localname</code> as its
   path.  This method is similar to <span class="function"><a href="ziparchive.addfromstring.php" class="function">ZipArchive::addFromString()</a></span>.
  </p>

 </div>

 <div class="refsect1 parameters" id="refsect1-phardata.addfromstring-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">localName</code></dt>
     <dd>
      <p class="para">
       Path that the file will be stored in the archive.
      </p>
     </dd>
    
    
     <dt><code class="parameter">contents</code></dt>
     <dd>
      <p class="para">
       The file contents to store
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-phardata.addfromstring-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   no return value, exception is thrown on failure.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-phardata.addfromstring-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 A <span class="function"><strong>PharData::addFromString()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">try {<br />    </span><span style="color: #0000BB">$a </span><span style="color: #007700">= new </span><span style="color: #0000BB">PharData</span><span style="color: #007700">(</span><span style="color: #DD0000">'/path/to/my.tar'</span><span style="color: #007700">);<br /><br />    </span><span style="color: #0000BB">$a</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addFromString</span><span style="color: #007700">(</span><span style="color: #DD0000">'path/to/file.txt'</span><span style="color: #007700">, </span><span style="color: #DD0000">'my simple file'</span><span style="color: #007700">);<br />    </span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #DD0000">'path/to/file.txt'</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">getContent</span><span style="color: #007700">();<br /><br />    </span><span style="color: #FF8000">// to add contents from a stream handle for large files, use offsetSet()<br />    </span><span style="color: #0000BB">$c </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'/path/to/hugefile.bin'</span><span style="color: #007700">);<br />    </span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #DD0000">'largefile.bin'</span><span style="color: #007700">] = </span><span style="color: #0000BB">$c</span><span style="color: #007700">;<br />    </span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br />} catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {<br />    </span><span style="color: #FF8000">// handle errors here<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-phardata.addfromstring-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
<span class="simpara">
  <span class="function"><a href="phardata.addfile.php" class="function">PharData::addFile()</a></span>, <span class="function"><strong>PharData::addFromString()</strong></span> and <span class="function"><a href="phardata.offsetset.php" class="function">PharData::offsetSet()</a></span>
  save a new phar archive each time they are called. If performance is a concern,
  <span class="function"><a href="phardata.buildfromdirectory.php" class="function">PharData::buildFromDirectory()</a></span> or <span class="function"><a href="phardata.buildfromiterator.php" class="function">PharData::buildFromIterator()</a></span>
  should be used instead.
</span>
</p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-phardata.addfromstring-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="phardata.offsetset.php" class="function" rel="rdfs-seeAlso">PharData::offsetSet()</a> - Set the contents of a file within the tar/zip to those of an external file or string</span></li>
    <li><span class="function"><a href="phar.addfromstring.php" class="function" rel="rdfs-seeAlso">Phar::addFromString()</a> - 以字符串的形式添加一个文件到 phar 档案</span></li>
    <li><span class="function"><a href="phardata.addfile.php" class="function" rel="rdfs-seeAlso">PharData::addFile()</a> - Add a file from the filesystem to the tar/zip archive</span></li>
    <li><span class="function"><a href="phardata.addemptydir.php" class="function" rel="rdfs-seeAlso">PharData::addEmptyDir()</a> - Add an empty directory to the tar/zip archive</span></li>
   </ul>
  </p>
 </div>



</div><?php manual_footer($setup); ?>