Zend_File_Transfer-Introduction.xml 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.file.transfer.introduction">
  4. <title>Zend_File_Transfer</title>
  5. <para>
  6. <classname>Zend_File_Transfer</classname> provides extensive support for file uploads and
  7. downloads. It comes with built-in validators for files plus functionality to change files
  8. with filters. Protocol adapters allow <classname>Zend_File_Transfer</classname> to expose
  9. the same <acronym>API</acronym> for transport protocols like <acronym>HTTP</acronym>, FTP,
  10. WEBDAV and more.
  11. </para>
  12. <note>
  13. <title>Limitation</title>
  14. <para>
  15. The current implementation of <classname>Zend_File_Transfer</classname> is limited to
  16. <acronym>HTTP</acronym> Post Uploads. Other adapters supporting downloads and other
  17. protocols will be added in future releases. Unimplemented methods will throw an
  18. exception. For now, you should use
  19. <classname>Zend_File_Transfer_Adapter_Http</classname> directly. As soon as
  20. there are multiple adapters available you can use a common interface.
  21. </para>
  22. </note>
  23. <note>
  24. <title>Forms</title>
  25. <para>
  26. When you are using <classname>Zend_Form</classname> you should use the
  27. <acronym>API</acronym>s provided by <classname>Zend_Form</classname> and not
  28. <classname>Zend_File_Transfer</classname> directly. The file transfer support in
  29. <classname>Zend_Form</classname> is implemented with
  30. <classname>Zend_File_Transfer</classname>, so the information in this chapter may
  31. be useful for advanced users of <classname>Zend_Form</classname>.
  32. </para>
  33. </note>
  34. <para>
  35. The usage of <classname>Zend_File_Transfer</classname> is relatively simple. It consists of
  36. two parts. The <acronym>HTTP</acronym> form does the upload, while the
  37. <classname>Zend_File_Transfer</classname> handles the uploaded files. See the following
  38. example:
  39. </para>
  40. <example id="zend.file.transfer.introduction.example">
  41. <title>Simple Form for Uploading Files</title>
  42. <para>
  43. This example illustrates basic file uploading.
  44. The first part is the file form. In our example there is one file to upload.
  45. </para>
  46. <programlisting language="xml"><![CDATA[
  47. <form enctype="multipart/form-data" action="/file/upload" method="POST">
  48. <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
  49. Choose a file to upload: <input name="uploadedfile" type="file" />
  50. <br />
  51. <input type="submit" value="Upload File" />
  52. </form>
  53. ]]></programlisting>
  54. <para>
  55. For convenience, you can use <link
  56. linkend="zend.form.standardElements.file">Zend_Form_Element_File</link> instead of
  57. building the HTML manually.
  58. </para>
  59. <para>
  60. The next step is to create the receiver of the upload. In our example the receiver is
  61. located at <code>/file/upload</code>. So next we will create the <code>file</code>
  62. controller and the <code>upload</code> action.
  63. </para>
  64. <programlisting language="php"><![CDATA[
  65. $adapter = new Zend_File_Transfer_Adapter_Http();
  66. $adapter->setDestination('C:\temp');
  67. if (!$adapter->receive()) {
  68. $messages = $adapter->getMessages();
  69. echo implode("\n", $messages);
  70. }
  71. ]]></programlisting>
  72. <para>
  73. This code listing demonstrates the simplest usage of
  74. <classname>Zend_File_Transfer</classname>. A local destination is set with the
  75. <code>setDestination</code> method, then the <methodname>receive()</methodname> method
  76. is called. if there are any upload errors, an error will be returned.
  77. </para>
  78. </example>
  79. <note>
  80. <title>Attention</title>
  81. <para>
  82. This example is suitable only for demonstrating the basic <acronym>API</acronym> of
  83. <classname>Zend_File_Transfer</classname>. You should <emphasis>never</emphasis> use
  84. this code listing in a production environment, because severe security issues may be
  85. introduced. You should always use validators to increase security.
  86. </para>
  87. </note>
  88. <sect2 id="zend.file.transfer.introduction.adapters">
  89. <title>Supported Adapters for Zend_File_Transfer</title>
  90. <para>
  91. <classname>Zend_File_Transfer</classname> is designed to support a variety of adapters
  92. and transfer directions. With <classname>Zend_File_Transfer</classname> you can upload,
  93. download and even forward (upload one adapter and download with another adapter at the
  94. same time) files.
  95. </para>
  96. </sect2>
  97. <sect2 id="zend.file.transfer.introduction.options">
  98. <title>Options for Zend_File_Transfer</title>
  99. <para>
  100. <classname>Zend_File_Transfer</classname> and its adapters support different options.
  101. You can set all options either by passing them to the constructor or by calling
  102. <methodname>setOptions($options)</methodname>. <methodname>getOptions()</methodname>
  103. will return the options that are currently set. The following is a list of all supported
  104. options.
  105. </para>
  106. <itemizedlist>
  107. <listitem>
  108. <para>
  109. <emphasis>ignoreNoFile</emphasis>: If this option is set to
  110. <constant>TRUE</constant>, all validators will ignore files that have not been
  111. uploaded by the form. The default value is <constant>FALSE</constant> which
  112. results in an error if no files were specified.
  113. </para>
  114. </listitem>
  115. </itemizedlist>
  116. </sect2>
  117. <sect2 id="zend.file.transfer.introduction.checking">
  118. <title>Checking Files</title>
  119. <para>
  120. <classname>Zend_File_Transfer</classname> has several methods that check for various
  121. states of the specified file. These are useful if you must process files after they have
  122. been uploaded. These methods include:
  123. </para>
  124. <itemizedlist>
  125. <listitem>
  126. <para>
  127. <emphasis>isValid($files = null)</emphasis>: This method will check if the
  128. given files are valid, based on the validators that are attached to the files.
  129. If no files are specified, all files will be checked. You can call
  130. <methodname>isValid()</methodname> before calling
  131. <methodname>receive()</methodname>; in this case,
  132. <methodname>receive()</methodname> will not call <code>isValid</code> internally
  133. again when receiving the file.
  134. </para>
  135. </listitem>
  136. <listitem>
  137. <para>
  138. <emphasis>isUploaded($files = null)</emphasis>: This method will check if the
  139. specified files have been uploaded by the user. This is useful when you have
  140. defined one or more optional files. When no files are specified, all files will
  141. be checked.
  142. </para>
  143. </listitem>
  144. <listitem>
  145. <para>
  146. <emphasis>isReceived($files = null)</emphasis>: This method will check if the
  147. given files have already been received. When no files are specified, all files
  148. will be checked.
  149. </para>
  150. </listitem>
  151. </itemizedlist>
  152. <example id="zend.file.transfer.introduction.checking.example">
  153. <title>Checking Files</title>
  154. <programlisting language="php"><![CDATA[
  155. $upload = new Zend_File_Transfer();
  156. // Returns all known internal file information
  157. $files = $upload->getFileInfo();
  158. foreach ($files as $file => $info) {
  159. // file uploaded ?
  160. if (!$upload->isUploaded($file)) {
  161. print "Why havn't you uploaded the file ?";
  162. continue;
  163. }
  164. // validators are ok ?
  165. if (!$upload->isValid($file)) {
  166. print "Sorry but $file is not what we wanted";
  167. continue;
  168. }
  169. }
  170. $upload->receive();
  171. ]]></programlisting>
  172. </example>
  173. </sect2>
  174. <sect2 id="zend.file.transfer.introduction.informations">
  175. <title>Additional File Informations</title>
  176. <para>
  177. <classname>Zend_File_Transfer</classname> can return additional information on files.
  178. The following methods are available:
  179. </para>
  180. <itemizedlist>
  181. <listitem>
  182. <para>
  183. <emphasis>getFileName($file = null, $path = true)</emphasis>: This method
  184. will return the real file name of a transferred file.
  185. </para>
  186. </listitem>
  187. <listitem>
  188. <para>
  189. <emphasis>getFileInfo($file = null)</emphasis>: This method will return all
  190. internal information for the given file.
  191. </para>
  192. </listitem>
  193. <listitem>
  194. <para>
  195. <emphasis>getFileSize($file = null)</emphasis>: This method will return the
  196. real filesize for the given file.
  197. </para>
  198. </listitem>
  199. <listitem>
  200. <para>
  201. <emphasis>getHash($hash = 'crc32', $files = null)</emphasis>: This method
  202. returns a hash of the content of a given transferred file.
  203. </para>
  204. </listitem>
  205. <listitem>
  206. <para>
  207. <emphasis>getMimeType($files = null)</emphasis>: This method returns the
  208. mimetype of a given transferred file.
  209. </para>
  210. </listitem>
  211. </itemizedlist>
  212. <para>
  213. <methodname>getFileName()</methodname> accepts the name of the element as first
  214. parameter. If no name is given, all known filenames will be returned in an array. If the
  215. file is a multifile, you will also get an array. If there is only a single file a string
  216. will be returned.
  217. </para>
  218. <para>
  219. By default file names will be returned with the complete path. If you only need the file
  220. name without path, you can set the second parameter, <varname>$path</varname>, which
  221. will truncate the file path when set to <constant>FALSE</constant>.
  222. </para>
  223. <example id="zend.file.transfer.introduction.informations.example1">
  224. <title>Getting the Filename</title>
  225. <programlisting language="php"><![CDATA[
  226. $upload = new Zend_File_Transfer();
  227. $upload->receive();
  228. // Returns the file names from all files
  229. $names = $upload->getFileName();
  230. // Returns the file names from the 'foo' form element
  231. $names = $upload->getFileName('foo');
  232. ]]></programlisting>
  233. </example>
  234. <note>
  235. <para>
  236. Note that the file name can change after you receive the file, because all filters
  237. will be applied once the file is received. So you should always call
  238. <methodname>getFileName()</methodname> after the files have been received.
  239. </para>
  240. </note>
  241. <para>
  242. <methodname>getFileSize()</methodname> returns per default the real filesize in SI
  243. notation which means you will get <code>2kB</code> instead of <code>2048</code>. If you
  244. need only the plain size set the <code>useByteString</code> option to
  245. <constant>FALSE</constant>.
  246. </para>
  247. <example id="zend.file.transfer.introduction.informations.example.getfilesize">
  248. <title>Getting the size of a file</title>
  249. <programlisting language="php"><![CDATA[
  250. $upload = new Zend_File_Transfer();
  251. $upload->receive();
  252. // Returns the sizes from all files as array if more than one file was uploaded
  253. $size = $upload->getFileSize();
  254. // Switches of the SI notation to return plain numbers
  255. $upload->setOption(array('useByteString' => false));
  256. $size = $upload->getFileSize();
  257. ]]></programlisting>
  258. </example>
  259. <note>
  260. <title>Client given filesize</title>
  261. <para>
  262. Note that the filesize which is given by the client is not seen as save input.
  263. Therefor the real size of the file will be detected and returned instead of the
  264. filesize sent by the client.
  265. </para>
  266. </note>
  267. <para>
  268. <methodname>getHash()</methodname> accepts the name of a hash algorithm as first
  269. parameter. For a list of known algorithms refer to
  270. <ulink url="http://php.net/hash_algos">PHP's hash_algos method</ulink>. If you don't
  271. specify an algorithm, the <code>crc32</code> algorithm will be used by default.
  272. </para>
  273. <example id="zend.file.transfer.introduction.informations.example2">
  274. <title>Getting the hash of a file</title>
  275. <programlisting language="php"><![CDATA[
  276. $upload = new Zend_File_Transfer();
  277. $upload->receive();
  278. // Returns the hashes from all files as array if more than one file was uploaded
  279. $hash = $upload->getHash('md5');
  280. // Returns the hash for the 'foo' form element
  281. $names = $upload->getHash('crc32', 'foo');
  282. ]]></programlisting>
  283. </example>
  284. <note>
  285. <title>Return value</title>
  286. <para>
  287. Note that if the given file or form name contains more than one file, the returned
  288. value will be an array.
  289. </para>
  290. </note>
  291. <para>
  292. <methodname>getMimeType()</methodname> returns the mimetype of a file. If more than one
  293. file was uploaded it returns an array, otherwise a string.
  294. </para>
  295. <example id="zend.file.transfer.introduction.informations.getmimetype">
  296. <title>Getting the mimetype of a file</title>
  297. <programlisting language="php"><![CDATA[
  298. $upload = new Zend_File_Transfer();
  299. $upload->receive();
  300. $mime = $upload->getMimeType();
  301. // Returns the mimetype for the 'foo' form element
  302. $names = $upload->getMimeType('foo');
  303. ]]></programlisting>
  304. </example>
  305. <note>
  306. <title>Client given mimetype</title>
  307. <para>
  308. Note that the mimetype which is given by the client is not seen as save input.
  309. Therefor the real mimetype of the file will be detected and returned instead of the
  310. filesize sent by the client.
  311. </para>
  312. </note>
  313. <warning>
  314. <title>Possible exception</title>
  315. <para>
  316. Note that this method uses the fileinfo extension if it is available. If this
  317. extension can not be found, it uses the mimemagic extension. When no extension was
  318. found it raises an exception.
  319. </para>
  320. </warning>
  321. <warning>
  322. <title>Original data within $_FILES</title>
  323. <para>
  324. Due to security reasons also the original data within $_FILES will be overridden
  325. as soon as <classname>Zend_File_Transfer</classname> is initiated. When you want
  326. to omit this behaviour and have the original data simply set the
  327. <property>detectInfos</property> option to false at initiation.
  328. </para>
  329. <para>
  330. This option will have no effect after you initiated
  331. <classname>Zend_File_Transfer</classname>.
  332. </para>
  333. </warning>
  334. </sect2>
  335. <sect2 id="zend.file.transfer.introduction.uploadprogress">
  336. <title>Progress for file uploads</title>
  337. <para>
  338. <classname>Zend_File_Transfer</classname> can give you the actual state of a fileupload
  339. in progress. To use this feature you need either the <acronym>APC</acronym> extension
  340. which is provided with most default <acronym>PHP</acronym> installations, or the
  341. <code>uploadprogress</code> extension. Both extensions are detected and used
  342. automatically. To be able to get the progress you need to meet some prerequisites.
  343. </para>
  344. <para>
  345. First, you need to have either <acronym>APC</acronym> or <code>uploadprogress</code> to
  346. be enabled. Note that you can disable this feature of <acronym>APC</acronym> within your
  347. php.ini.
  348. </para>
  349. <para>
  350. Second, you need to have the proper hidden fields added in the form which sends the
  351. files. When you use <classname>Zend_Form_Element_File</classname> this hidden fields are
  352. automatically added by <classname>Zend_Form</classname>.
  353. </para>
  354. <para>
  355. When the above two points are provided then you are able to get the actual progress of
  356. the file upload by using the <code>getProgress</code> method. Actually there are 2
  357. official ways to handle this.
  358. </para>
  359. <sect3 id="zend.file.transfer.introduction.uploadprogress.progressadapter">
  360. <title>Using a progressbar adapter</title>
  361. <para>
  362. You can use the convinient <emphasis>Zend_ProgressBar</emphasis> to get the actual
  363. progress and can display it in a simple manner to your user.
  364. </para>
  365. <para>
  366. To archive this, you have to add the wished
  367. <emphasis>Zend_ProgressBar_Adapter</emphasis> to
  368. <methodname>getProgress()</methodname> when you are calling it the first time. For
  369. details about the right adapter to use, look into the chapter <link
  370. linkend="zend.progressbar.adapters">Zend_ProgressBar Standard Adapters</link>.
  371. </para>
  372. <example id="zend.file.transfer.introduction.uploadprogress.progressadapter.example1">
  373. <title>Using the progressbar adapter to retrieve the actual state</title>
  374. <programlisting language="php"><![CDATA[
  375. $adapter = new Zend_ProgressBar_Adapter_Console();
  376. $upload = Zend_File_Transfer_Adapter_Http::getProgress($adapter);
  377. $upload = null;
  378. while (!$upload['done']) {
  379. $upload = Zend_File_Transfer_Adapter_Http:getProgress($upload);
  380. }
  381. ]]></programlisting>
  382. </example>
  383. <para>
  384. The complete handling is done by <methodname>getProgress()</methodname> for you in
  385. the background.
  386. </para>
  387. </sect3>
  388. <sect3 id="zend.file.transfer.introduction.uploadprogress.manually">
  389. <title>Using getProgress() manually</title>
  390. <para>
  391. You can also work manually with <methodname>getProgress()</methodname> without the
  392. usage of <classname>Zend_ProgressBar</classname>.
  393. </para>
  394. <para>
  395. Call <methodname>getProgress()</methodname> without settings. It will return you an
  396. array with several keys. They differ according to the used <acronym>PHP</acronym>
  397. extension. But the following keys are given independently of the extension:
  398. </para>
  399. <itemizedlist>
  400. <listitem>
  401. <para>
  402. <emphasis>id</emphasis>: The ID of this upload. This ID identifies the
  403. upload within the extension. It is filled automatically. You should never
  404. change or give this value yourself.
  405. </para>
  406. </listitem>
  407. <listitem>
  408. <para>
  409. <emphasis>total</emphasis>: The total filesize of the uploaded files in
  410. bytes as integer.
  411. </para>
  412. </listitem>
  413. <listitem>
  414. <para>
  415. <emphasis>current</emphasis>: The current uploaded filesize in bytes
  416. as integer.
  417. </para>
  418. </listitem>
  419. <listitem>
  420. <para>
  421. <emphasis>rate</emphasis>: The average upload speed in bytes per second
  422. as integer.
  423. </para>
  424. </listitem>
  425. <listitem>
  426. <para>
  427. <emphasis>done</emphasis>: Returns <constant>TRUE</constant> when the upload
  428. is finished and <constant>FALSE</constant> otherwise.
  429. </para>
  430. </listitem>
  431. <listitem>
  432. <para>
  433. <emphasis>message</emphasis>: The actual message. Either the progress as
  434. text in the form <emphasis>10kB / 200kB</emphasis>, or a helpful message
  435. in the case of a problem. Problems could be, that there is no upload in
  436. progress, that there was a failure while retrieving the data for the
  437. progress, or that the upload has been canceled.
  438. </para>
  439. </listitem>
  440. <listitem>
  441. <para>
  442. <emphasis>progress</emphasis>: This optional key takes a instance of
  443. <classname>Zend_ProgressBar_Adapter</classname> or
  444. <classname>Zend_ProgressBar</classname> and allows to get the actual upload
  445. state within a progressbar.
  446. </para>
  447. </listitem>
  448. <listitem>
  449. <para>
  450. <emphasis>session</emphasis>: This optional key takes the name of a session
  451. namespace which will be used within <classname>Zend_ProgressBar</classname>.
  452. When this key is not given it defaults to
  453. <classname>Zend_File_Transfer_Adapter_Http_ProgressBar</classname>.
  454. </para>
  455. </listitem>
  456. </itemizedlist>
  457. <para>
  458. All other returned keys are provided directly from the extensions and will not be
  459. checked.
  460. </para>
  461. <para>
  462. The following example shows a possible manual usage:
  463. </para>
  464. <example id="zend.file.transfer.introduction.uploadprogress.manually.example1">
  465. <title>Manual usage of the file progress</title>
  466. <programlisting language="php"><![CDATA[
  467. $upload = Zend_File_Transfer_Adapter_Http::getProgress();
  468. while (!$upload['done']) {
  469. $upload = Zend_File_Transfer_Adapter_Http:getProgress($upload);
  470. print "\nActual progress:".$upload['message'];
  471. // do whatever you need
  472. }
  473. ]]></programlisting>
  474. </example>
  475. </sect3>
  476. </sect2>
  477. </sect1>
  478. <!--
  479. vim:se ts=4 sw=4 tw=80 et:
  480. -->