Zend_File_Transfer-Introduction.xml 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. <para>
  260. <methodname>getHash()</methodname> accepts the name of a hash algorithm as first
  261. parameter. For a list of known algorithms refer to
  262. <ulink url="http://php.net/hash_algos">PHP's hash_algos method</ulink>. If you don't
  263. specify an algorithm, the <code>crc32</code> algorithm will be used by default.
  264. </para>
  265. <example id="zend.file.transfer.introduction.informations.example2">
  266. <title>Getting the hash of a file</title>
  267. <programlisting language="php"><![CDATA[
  268. $upload = new Zend_File_Transfer();
  269. $upload->receive();
  270. // Returns the hashes from all files as array if more than one file was uploaded
  271. $hash = $upload->getHash('md5');
  272. // Returns the hash for the 'foo' form element
  273. $names = $upload->getHash('crc32', 'foo');
  274. ]]></programlisting>
  275. </example>
  276. <note>
  277. <para>
  278. Note that if the given file or form name contains more than one file, the returned
  279. value will be an array.
  280. </para>
  281. </note>
  282. <para>
  283. <methodname>getMimeType()</methodname> returns the mimetype of a file. If more than one
  284. file was uploaded it returns an array, otherwise a string.
  285. </para>
  286. <example id="zend.file.transfer.introduction.informations.getmimetype">
  287. <title>Getting the mimetype of a file</title>
  288. <programlisting language="php"><![CDATA[
  289. $upload = new Zend_File_Transfer();
  290. $upload->receive();
  291. $mime = $upload->getMimeType();
  292. // Returns the mimetype for the 'foo' form element
  293. $names = $upload->getMimeType('foo');
  294. ]]></programlisting>
  295. </example>
  296. <note>
  297. <para>
  298. Note that this method uses the fileinfo extension if it is available. If this
  299. extension can not be found, it uses the mimemagic extension. When no extension was
  300. found it uses the mimetype given by the fileserver when the file was uploaded.
  301. </para>
  302. </note>
  303. </sect2>
  304. <sect2 id="zend.file.transfer.introduction.uploadprogress">
  305. <title>Progress for file uploads</title>
  306. <para>
  307. <classname>Zend_File_Transfer</classname> can give you the actual state of a fileupload
  308. in progress. To use this feature you need either the <acronym>APC</acronym> extension
  309. which is provided with most default <acronym>PHP</acronym> installations, or the
  310. <code>uploadprogress</code> extension. Both extensions are detected and used
  311. automatically. To be able to get the progress you need to meet some prerequisites.
  312. </para>
  313. <para>
  314. First, you need to have either <acronym>APC</acronym> or <code>uploadprogress</code> to
  315. be enabled. Note that you can disable this feature of <acronym>APC</acronym> within your
  316. php.ini.
  317. </para>
  318. <para>
  319. Second, you need to have the proper hidden fields added in the form which sends the
  320. files. When you use <classname>Zend_Form_Element_File</classname> this hidden fields are
  321. automatically added by <classname>Zend_Form</classname>.
  322. </para>
  323. <para>
  324. When the above two points are provided then you are able to get the actual progress of
  325. the file upload by using the <code>getProgress</code> method. Actually there are 2
  326. official ways to handle this.
  327. </para>
  328. <sect3 id="zend.file.transfer.introduction.uploadprogress.progressadapter">
  329. <title>Using a progressbar adapter</title>
  330. <para>
  331. You can use the convinient <emphasis>Zend_ProgressBar</emphasis> to get the actual
  332. progress and can display it in a simple manner to your user.
  333. </para>
  334. <para>
  335. To archive this, you have to add the wished
  336. <emphasis>Zend_ProgressBar_Adapter</emphasis> to
  337. <methodname>getProgress()</methodname> when you are calling it the first time. For
  338. details about the right adapter to use, look into the chapter <link
  339. linkend="zend.progressbar.adapters">Zend_ProgressBar Standard Adapters</link>.
  340. </para>
  341. <example id="zend.file.transfer.introduction.uploadprogress.progressadapter.example1">
  342. <title>Using the progressbar adapter to retrieve the actual state</title>
  343. <programlisting language="php"><![CDATA[
  344. $adapter = new Zend_ProgressBar_Adapter_Console();
  345. $upload = Zend_File_Transfer_Adapter_Http::getProgress($adapter);
  346. $upload = null;
  347. while (!$upload['done']) {
  348. $upload = Zend_File_Transfer_Adapter_Http:getProgress($upload);
  349. }
  350. ]]></programlisting>
  351. </example>
  352. <para>
  353. The complete handling is done by <methodname>getProgress()</methodname> for you in
  354. the background.
  355. </para>
  356. </sect3>
  357. <sect3 id="zend.file.transfer.introduction.uploadprogress.manually">
  358. <title>Using getProgress() manually</title>
  359. <para>
  360. You can also work manually with <methodname>getProgress()</methodname> without the
  361. usage of <classname>Zend_ProgressBar</classname>.
  362. </para>
  363. <para>
  364. Call <methodname>getProgress()</methodname> without settings. It will return you an
  365. array with several keys. They differ according to the used <acronym>PHP</acronym>
  366. extension. But the following keys are given independently of the extension:
  367. </para>
  368. <itemizedlist>
  369. <listitem>
  370. <para>
  371. <emphasis>id</emphasis>: The ID of this upload. This ID identifies the
  372. upload within the extension. It is filled automatically. You should never
  373. change or give this value yourself.
  374. </para>
  375. </listitem>
  376. <listitem>
  377. <para>
  378. <emphasis>total</emphasis>: The total filesize of the uploaded files in
  379. bytes as integer.
  380. </para>
  381. </listitem>
  382. <listitem>
  383. <para>
  384. <emphasis>current</emphasis>: The current uploaded filesize in bytes
  385. as integer.
  386. </para>
  387. </listitem>
  388. <listitem>
  389. <para>
  390. <emphasis>rate</emphasis>: The average upload speed in bytes per second
  391. as integer.
  392. </para>
  393. </listitem>
  394. <listitem>
  395. <para>
  396. <emphasis>done</emphasis>: Returns <constant>TRUE</constant> when the upload
  397. is finished and <constant>FALSE</constant> otherwise.
  398. </para>
  399. </listitem>
  400. <listitem>
  401. <para>
  402. <emphasis>message</emphasis>: The actual message. Either the progress as
  403. text in the form <emphasis>10kB / 200kB</emphasis>, or a helpful message
  404. in the case of a problem. Problems could be, that there is no upload in
  405. progress, that there was a failure while retrieving the data for the
  406. progress, or that the upload has been canceled.
  407. </para>
  408. </listitem>
  409. <listitem>
  410. <para>
  411. <emphasis>progress</emphasis>: This optional key takes a instance of
  412. <classname>Zend_ProgressBar_Adapter</classname> or
  413. <classname>Zend_ProgressBar</classname> and allows to get the actual upload
  414. state within a progressbar.
  415. </para>
  416. </listitem>
  417. <listitem>
  418. <para>
  419. <emphasis>session</emphasis>: This optional key takes the name of a session
  420. namespace which will be used within <classname>Zend_ProgressBar</classname>.
  421. When this key is not given it defaults to
  422. <classname>Zend_File_Transfer_Adapter_Http_ProgressBar</classname>.
  423. </para>
  424. </listitem>
  425. </itemizedlist>
  426. <para>
  427. All other returned keys are provided directly from the extensions and will not be
  428. checked.
  429. </para>
  430. <para>
  431. The following example shows a possible manual usage:
  432. </para>
  433. <example id="zend.file.transfer.introduction.uploadprogress.manually.example1">
  434. <title>Manual usage of the file progress</title>
  435. <programlisting language="php"><![CDATA[
  436. $upload = Zend_File_Transfer_Adapter_Http::getProgress();
  437. while (!$upload['done']) {
  438. $upload = Zend_File_Transfer_Adapter_Http:getProgress($upload);
  439. print "\nActual progress:".$upload['message'];
  440. // do whatever you need
  441. }
  442. ]]></programlisting>
  443. </example>
  444. </sect3>
  445. </sect2>
  446. </sect1>
  447. <!--
  448. vim:se ts=4 sw=4 tw=80 et:
  449. -->