Zend_File_Transfer-Introduction.xml 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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 true, all
  110. validators will ignore files that have not been uploaded by the form. The
  111. default value is false which results in an error if no files were specified.
  112. </para>
  113. </listitem>
  114. </itemizedlist>
  115. </sect2>
  116. <sect2 id="zend.file.transfer.introduction.checking">
  117. <title>Checking Files</title>
  118. <para>
  119. <classname>Zend_File_Transfer</classname> has several methods that check for various
  120. states of the specified file. These are useful if you must process files after they have
  121. been uploaded. These methods include:
  122. </para>
  123. <itemizedlist>
  124. <listitem>
  125. <para>
  126. <emphasis>isValid($files = null)</emphasis>: This method will check if the
  127. given files are valid, based on the validators that are attached to the files.
  128. If no files are specified, all files will be checked. You can call
  129. <methodname>isValid()</methodname> before calling
  130. <methodname>receive()</methodname>; in this case,
  131. <methodname>receive()</methodname> will not call <code>isValid</code> internally
  132. again when receiving the file.
  133. </para>
  134. </listitem>
  135. <listitem>
  136. <para>
  137. <emphasis>isUploaded($files = null)</emphasis>: This method will check if the
  138. specified files have been uploaded by the user. This is useful when you have
  139. defined one or more optional files. When no files are specified, all files will
  140. be checked.
  141. </para>
  142. </listitem>
  143. <listitem>
  144. <para>
  145. <emphasis>isReceived($files = null)</emphasis>: This method will check if the
  146. given files have already been received. When no files are specified, all files
  147. will be checked.
  148. </para>
  149. </listitem>
  150. </itemizedlist>
  151. <example id="zend.file.transfer.introduction.checking.example">
  152. <title>Checking Files</title>
  153. <programlisting language="php"><![CDATA[
  154. $upload = new Zend_File_Transfer();
  155. // Returns all known internal file information
  156. $files = $upload->getFileInfo();
  157. foreach ($files as $file => $info) {
  158. // file uploaded ?
  159. if (!$upload->isUploaded($file)) {
  160. print "Why havn't you uploaded the file ?";
  161. continue;
  162. }
  163. // validators are ok ?
  164. if (!$upload->isValid($file)) {
  165. print "Sorry but $file is not what we wanted";
  166. continue;
  167. }
  168. }
  169. $upload->receive();
  170. ]]></programlisting>
  171. </example>
  172. </sect2>
  173. <sect2 id="zend.file.transfer.introduction.informations">
  174. <title>Additional File Informations</title>
  175. <para>
  176. <classname>Zend_File_Transfer</classname> can return additional information on files.
  177. The following methods are available:
  178. </para>
  179. <itemizedlist>
  180. <listitem>
  181. <para>
  182. <emphasis>getFileName($file = null, $path = true)</emphasis>: This method
  183. will return the real file name of a transferred file.
  184. </para>
  185. </listitem>
  186. <listitem>
  187. <para>
  188. <emphasis>getFileInfo($file = null)</emphasis>: This method will return all
  189. internal information for the given file.
  190. </para>
  191. </listitem>
  192. <listitem>
  193. <para>
  194. <emphasis>getFileSize($file = null)</emphasis>: This method will return the
  195. real filesize for the given file.
  196. </para>
  197. </listitem>
  198. <listitem>
  199. <para>
  200. <emphasis>getHash($hash = 'crc32', $files = null)</emphasis>: This method
  201. returns a hash of the content of a given transferred file.
  202. </para>
  203. </listitem>
  204. <listitem>
  205. <para>
  206. <emphasis>getMimeType($files = null)</emphasis>: This method returns the
  207. mimetype of a given transferred file.
  208. </para>
  209. </listitem>
  210. </itemizedlist>
  211. <para>
  212. <methodname>getFileName()</methodname> accepts the name of the element as first
  213. parameter. If no name is given, all known filenames will be returned in an array. If the
  214. file is a multifile, you will also get an array. If there is only a single file a string
  215. will be returned.
  216. </para>
  217. <para>
  218. By default file names will be returned with the complete path. If you only need the file
  219. name without path, you can set the second parameter, <varname>$path</varname>, which
  220. will truncate the file path when set to false.
  221. </para>
  222. <example id="zend.file.transfer.introduction.informations.example1">
  223. <title>Getting the Filename</title>
  224. <programlisting language="php"><![CDATA[
  225. $upload = new Zend_File_Transfer();
  226. $upload->receive();
  227. // Returns the file names from all files
  228. $names = $upload->getFileName();
  229. // Returns the file names from the 'foo' form element
  230. $names = $upload->getFileName('foo');
  231. ]]></programlisting>
  232. </example>
  233. <note>
  234. <para>
  235. Note that the file name can change after you receive the file, because all filters
  236. will be applied once the file is received. So you should always call
  237. <methodname>getFileName()</methodname> after the files have been received.
  238. </para>
  239. </note>
  240. <para>
  241. <methodname>getFileSize()</methodname> returns per default the real filesize in SI
  242. notation which means you will get <code>2kB</code> instead of <code>2048</code>. If you
  243. need only the plain size set the <code>useByteString</code> option to false.
  244. </para>
  245. <example id="zend.file.transfer.introduction.informations.example.getfilesize">
  246. <title>Getting the size of a file</title>
  247. <programlisting language="php"><![CDATA[
  248. $upload = new Zend_File_Transfer();
  249. $upload->receive();
  250. // Returns the sizes from all files as array if more than one file was uploaded
  251. $size = $upload->getFileSize();
  252. // Switches of the SI notation to return plain numbers
  253. $upload->setOption(array('useByteString' => false));
  254. $size = $upload->getFileSize();
  255. ]]></programlisting>
  256. </example>
  257. <para>
  258. <methodname>getHash()</methodname> accepts the name of a hash algorithm as first
  259. parameter. For a list of known algorithms refer to
  260. <ulink url="http://php.net/hash_algos">PHP's hash_algos method</ulink>. If you don't
  261. specify an algorithm, the <code>crc32</code> algorithm will be used by default.
  262. </para>
  263. <example id="zend.file.transfer.introduction.informations.example2">
  264. <title>Getting the hash of a file</title>
  265. <programlisting language="php"><![CDATA[
  266. $upload = new Zend_File_Transfer();
  267. $upload->receive();
  268. // Returns the hashes from all files as array if more than one file was uploaded
  269. $hash = $upload->getHash('md5');
  270. // Returns the hash for the 'foo' form element
  271. $names = $upload->getHash('crc32', 'foo');
  272. ]]></programlisting>
  273. </example>
  274. <note>
  275. <para>
  276. Note that if the given file or form name contains more than one file, the returned
  277. value will be an array.
  278. </para>
  279. </note>
  280. <para>
  281. <methodname>getMimeType()</methodname> returns the mimetype of a file. If more than one
  282. file was uploaded it returns an array, otherwise a string.
  283. </para>
  284. <example id="zend.file.transfer.introduction.informations.getmimetype">
  285. <title>Getting the mimetype of a file</title>
  286. <programlisting language="php"><![CDATA[
  287. $upload = new Zend_File_Transfer();
  288. $upload->receive();
  289. $mime = $upload->getMimeType();
  290. // Returns the mimetype for the 'foo' form element
  291. $names = $upload->getMimeType('foo');
  292. ]]></programlisting>
  293. </example>
  294. <note>
  295. <para>
  296. Note that this method uses the fileinfo extension if it is available. If this
  297. extension can not be found, it uses the mimemagic extension. When no extension was
  298. found it uses the mimetype given by the fileserver when the file was uploaded.
  299. </para>
  300. </note>
  301. </sect2>
  302. <sect2 id="zend.file.transfer.introduction.uploadprogress">
  303. <title>Progress for file uploads</title>
  304. <para>
  305. <classname>Zend_File_Transfer</classname> can give you the actual state of a fileupload
  306. in progress. To use this feature you need either the <acronym>APC</acronym> extension
  307. which is provided with most default <acronym>PHP</acronym> installations, or the
  308. <code>uploadprogress</code> extension. Both extensions are detected and used
  309. automatically. To be able to get the progress you need to meet some prerequisites.
  310. </para>
  311. <para>
  312. First, you need to have either <acronym>APC</acronym> or <code>uploadprogress</code> to
  313. be enabled. Note that you can disable this feature of <acronym>APC</acronym> within your
  314. php.ini.
  315. </para>
  316. <para>
  317. Second, you need to have the proper hidden fields added in the form which sends the
  318. files. When you use <classname>Zend_Form_Element_File</classname> this hidden fields are
  319. automatically added by <classname>Zend_Form</classname>.
  320. </para>
  321. <para>
  322. When the above two points are provided then you are able to get the actual progress of
  323. the file upload by using the <code>getProgress</code> method. Actually there are 2
  324. official ways to handle this.
  325. </para>
  326. <sect3 id="zend.file.transfer.introduction.uploadprogress.progressadapter">
  327. <title>Using a progressbar adapter</title>
  328. <para>
  329. You can use the convinient <emphasis>Zend_ProgressBar</emphasis> to get the actual
  330. progress and can display it in a simple manner to your user.
  331. </para>
  332. <para>
  333. To archive this, you have to add the wished
  334. <emphasis>Zend_ProgressBar_Adapter</emphasis> to
  335. <methodname>getProgress()</methodname> when you are calling it the first time. For
  336. details about the right adapter to use, look into the chapter <link
  337. linkend="zend.progressbar.adapters">Zend_ProgressBar Standard Adapters</link>.
  338. </para>
  339. <example id="zend.file.transfer.introduction.uploadprogress.progressadapter.example1">
  340. <title>Using the progressbar adapter to retrieve the actual state</title>
  341. <programlisting language="php"><![CDATA[
  342. $adapter = new Zend_ProgressBar_Adapter_Console();
  343. $upload = Zend_File_Transfer_Adapter_Http::getProgress($adapter);
  344. $upload = null;
  345. while (!$upload['done']) {
  346. $upload = Zend_File_Transfer_Adapter_Http:getProgress($upload);
  347. }
  348. ]]></programlisting>
  349. </example>
  350. <para>
  351. The complete handling is done by <methodname>getProgress()</methodname> for you in
  352. the background.
  353. </para>
  354. </sect3>
  355. <sect3 id="zend.file.transfer.introduction.uploadprogress.manually">
  356. <title>Using getProgress() manually</title>
  357. <para>
  358. You can also work manually with <methodname>getProgress()</methodname> without the
  359. usage of <classname>Zend_ProgressBar</classname>.
  360. </para>
  361. <para>
  362. Call <methodname>getProgress()</methodname> without settings. It will return you an
  363. array with several keys. They differ according to the used <acronym>PHP</acronym>
  364. extension. But the following keys are given independently of the extension:
  365. </para>
  366. <itemizedlist>
  367. <listitem>
  368. <para>
  369. <emphasis>id</emphasis>: The ID of this upload. This ID identifies the
  370. upload within the extension. It is filled automatically. You should never
  371. change or give this value yourself.
  372. </para>
  373. </listitem>
  374. <listitem>
  375. <para>
  376. <emphasis>total</emphasis>: The total filesize of the uploaded files in
  377. bytes as integer.
  378. </para>
  379. </listitem>
  380. <listitem>
  381. <para>
  382. <emphasis>current</emphasis>: The current uploaded filesize in bytes
  383. as integer.
  384. </para>
  385. </listitem>
  386. <listitem>
  387. <para>
  388. <emphasis>rate</emphasis>: The average upload speed in bytes per second
  389. as integer.
  390. </para>
  391. </listitem>
  392. <listitem>
  393. <para>
  394. <emphasis>done</emphasis>: Returns true when the upload is finished and
  395. false otherwise.
  396. </para>
  397. </listitem>
  398. <listitem>
  399. <para>
  400. <emphasis>message</emphasis>: The actual message. Either the progress as
  401. text in the form <emphasis>10kB / 200kB</emphasis>, or a helpful message
  402. in the case of a problem. Problems could be, that there is no upload in
  403. progress, that there was a failure while retrieving the data for the
  404. progress, or that the upload has been canceled.
  405. </para>
  406. </listitem>
  407. <listitem>
  408. <para>
  409. <emphasis>progress</emphasis>: This optional key takes a instance of
  410. <classname>Zend_ProgressBar_Adapter</classname> or
  411. <classname>Zend_ProgressBar</classname> and allows to get the actual upload
  412. state within a progressbar.
  413. </para>
  414. </listitem>
  415. <listitem>
  416. <para>
  417. <emphasis>session</emphasis>: This optional key takes the name of a session
  418. namespace which will be used within <classname>Zend_ProgressBar</classname>.
  419. When this key is not given it defaults to
  420. <classname>Zend_File_Transfer_Adapter_Http_ProgressBar</classname>.
  421. </para>
  422. </listitem>
  423. </itemizedlist>
  424. <para>
  425. All other returned keys are provided directly from the extensions and will not be
  426. checked.
  427. </para>
  428. <para>
  429. The following example shows a possible manual usage:
  430. </para>
  431. <example id="zend.file.transfer.introduction.uploadprogress.manually.example1">
  432. <title>Manual usage of the file progress</title>
  433. <programlisting language="php"><![CDATA[
  434. $upload = Zend_File_Transfer_Adapter_Http::getProgress();
  435. while (!$upload['done']) {
  436. $upload = Zend_File_Transfer_Adapter_Http:getProgress($upload);
  437. print "\nActual progress:".$upload['message'];
  438. // do whatever you need
  439. }
  440. ]]></programlisting>
  441. </example>
  442. </sect3>
  443. </sect2>
  444. </sect1>
  445. <!--
  446. vim:se ts=4 sw=4 tw=80 et:
  447. -->