Zend_File_Transfer-Introduction.xml 21 KB

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