Zend_File_Transfer-Introduction.xml 21 KB

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