Zend_File_Transfer-Introduction.xml 24 KB

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