Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to do file uploads with PHP and the Zend Framework?
    primarykey
    data
    text
    <p>I am using Zend Framework 1.9.6. I think I've got it pretty much figured out except for the end. This is what I have so far:</p> <h2>Form:</h2> <pre><code>&lt;?php class Default_Form_UploadFile extends Zend_Form { public function init() { $this-&gt;setAttrib('enctype', 'multipart/form-data'); $this-&gt;setMethod('post'); $description = new Zend_Form_Element_Text('description'); $description-&gt;setLabel('Description') -&gt;setRequired(true) -&gt;addValidator('NotEmpty'); $this-&gt;addElement($description); $file = new Zend_Form_Element_File('file'); $file-&gt;setLabel('File to upload:') -&gt;setRequired(true) -&gt;addValidator('NotEmpty') -&gt;addValidator('Count', false, 1); $this-&gt;addElement($file); $this-&gt;addElement('submit', 'submit', array( 'label' =&gt; 'Upload', 'ignore' =&gt; true )); } } </code></pre> <h2>Controller:</h2> <pre><code>public function uploadfileAction() { $form = new Default_Form_UploadFile(); $form-&gt;setAction($this-&gt;view-&gt;url()); $request = $this-&gt;getRequest(); if (!$request-&gt;isPost()) { $this-&gt;view-&gt;form = $form; return; } if (!$form-&gt;isValid($request-&gt;getPost())) { $this-&gt;view-&gt;form = $form; return; } try { $form-&gt;file-&gt;receive(); //upload complete! //...what now? $location = $form-&gt;file-&gt;getFileName(); var_dump($form-&gt;file-&gt;getFileInfo()); } catch (Exception $exception) { //error uploading file $this-&gt;view-&gt;form = $form; } } </code></pre> <p>Now what do I do with the file? It has been uploaded to my <code>/tmp</code> directory by default. Obviously that's not where I want to keep it. I want users of my application to be able to download it. So, I'm thinking that means I need to move the uploaded file to the public directory of my application and store the file name in the database so I can display it as a url.</p> <p>Or set this as the upload directory in the first place (though I was running into errors while trying to do that earlier).</p> <p>Have you worked with uploaded files before? What is the next step I should take?</p> <h2>Solution:</h2> <p>I decided to put the uploaded files into <code>data/uploads</code> (which is a sym link to a directory outside of my application, in order to make it accessible to all versions of my application).</p> <pre><code># /public/index.php # Define path to uploads directory defined('APPLICATION_UPLOADS_DIR') || define('APPLICATION_UPLOADS_DIR', realpath(dirname(__FILE__) . '/../data/uploads')); # /application/forms/UploadFile.php # Set the file destination on the element in the form $file = new Zend_Form_Element_File('file'); $file-&gt;setDestination(APPLICATION_UPLOADS_DIR); # /application/controllers/MyController.php # After the form has been validated... # Rename the file to something unique so it cannot be overwritten with a file of the same name $originalFilename = pathinfo($form-&gt;file-&gt;getFileName()); $newFilename = 'file-' . uniqid() . '.' . $originalFilename['extension']; $form-&gt;file-&gt;addFilter('Rename', $newFilename); try { $form-&gt;file-&gt;receive(); //upload complete! # Save a display filename (the original) and the actual filename, so it can be retrieved later $file = new Default_Model_File(); $file-&gt;setDisplayFilename($originalFilename['basename']) -&gt;setActualFilename($newFilename) -&gt;setMimeType($form-&gt;file-&gt;getMimeType()) -&gt;setDescription($form-&gt;description-&gt;getValue()); $file-&gt;save(); } catch (Exception $e) { //error } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload