PHP gets file as byte array using Zend_Form

We use Zend_Form inside a PHP application to create an input file html element. We can set the "destination" of this element, and when we call the receive () method, the file will be saved in the specified location.

We want to be able to not save the file to disk at all, but grab the file as a byte array and do something else with it.

Is it possible? If this is not possible with Zend_Form (), can it be done in any other way?

EDIT: The reason we can't write to disk is because the app is running on Azure and it doesn't seem to have write permissions anywhere, not even the temp folder. We get an exception from Zend saying "this destination could not be written."

+3


source to share


2 answers


All PHP uploads are written to the filesystem whether Zend is used or not (see upload_tmp_dir and Uploading POST Method ).

The files will be stored on the default temporary server by default, unless a different location has been provided using upload_tmp_dir

in php.ini

.

Instead of using it receive

to handle the download, try accessing it directly using an array $_FILES

, which will allow you to read the file in line using file_get_contents () or similar functions. However, you can use Zend_Form

to create and manipulate the form in general.



You can configure shared memory upload_tmp_dir

to map the file system to memory where downloaded files are stored. Be careful with this, as if someone is trying to download a very large file, it will end up in memory, which can affect performance or maintenance cost.

It ultimately Zend_File_Transfer_Adapter_Http::receive()

calls move_uploaded_file () to move the file from its temporary location to a permanent location. Also, it makes sure the download is valid and filters it and marks it as received so it can't be moved again (since it didn't work).

+1


source


The only thing that seems viable is to save the file using the protocol php://memory

.



I never had a reason to implement, but it looks as simple as setting the save location of the file to php://memory

here is the link to the PHP I / O Wrappers man page .

+2


source







All Articles