Hi, 

I would like to implement some simple file upload / download
functionality.

I tried the following approach:

When uploading a file via the <input type="file" ...> tag the value
of $_FILES['file']['type'] is retrieved (application/octet-stream in
the case of a simple text file), stored in the database and later
reused when somebody tries to download the file to set the
'content-type' header :

     $request  = $this->getRequest();
     $response = $this->getResponse();
     [...]
     $this->_helper->viewRenderer->setNoRender();
     [...]
>>>  $response->setHeader('content-type',   $contentType);
     $response->setHeader('content-length', $contentLength);
     echo file_get_contents($filepath);

This seems to be the wrong approach: when trying to download the file
only an empty page is shown.

When uncommenting the line "$response->setHeader('content-type',
$mimetype);" and downloading a text file the content is shown
correctly.

The up/download of binary files on the other side (for example a
simple hello-world program) doesn't work.

Here my question:

  - should I use
  
     $response->setHeader('content-type',   $type);
     $response->setHeader('content-length', $size);
     echo file_get_contents($filepath);

    at all?

    Or should I simply skip the first two lines and just use 
   
     echo file_get_contents($filepath);

    ?
 
  - If the header has to be used, how can I determine the correct
    'content-type' to use for some uploaded file?

    Any kind of file - Images, binary files, text files etc. -
    might be up- and downloaded by the user.

Thanks for your help, Dietrich


Reply via email to