John Quest
John Quest

Reputation: 85

PHP receive file as byte array using Zend_Form

We are using Zend_Form inside a PHP application for building an input file html element. We can set the 'destination' of this element, and when calling receive() the file will be saved to the specified location.

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

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

EDIT: The reason why we cannot write to disc is because the application runs on Azure, and it seems that it does not have write access rights anywhere, not even in the temp folder. We get an exception from Zend saying that 'The given destination is not writeable'.

Upvotes: 3

Views: 672

Answers (2)

drew010
drew010

Reputation: 69967

All PHP uploads are written to the file system regardless of using Zend or not (see upload_tmp_dir and POST method uploads).

Files will, by default be stored in the server's default temporary directory, unless another location has been given with the upload_tmp_dir directive in php.ini.

Instead of using receive to process the upload, try accessing it directly using the $_FILES array which would let you read the file into a string using file_get_contents() or similar functions. You can however, still use Zend_Form to create and handle the form in general.

You could set up shared memory upload_tmp_dir to map a filesystem to memory where uploaded files are held. Be cautious with this as if someone attempts to upload a very large file, it will go into memory which could affect performance or your cost of service.

Ultimately, Zend_File_Transfer_Adapter_Http::receive() calls move_uploaded_file() to move the file from its temporary location to the permanent location. In addition it makes sure the upload is valid and filters it, and marks it as received so it cannot be moved again (as that would fail).

Upvotes: 1

RockyFord
RockyFord

Reputation: 8519

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

I've never had reason to implement but it looks a simple as setting the save location of the file to php://memory here is the link to the manual page PHP I/O Wrappers.

Upvotes: 2

Related Questions