0

I am trying to upload a file, My front end application is in PHP and backend engine is in Java. They both communicate through PHP-Java_bridge.

My first action was, when a file is posted to PHP page, it will retrieve its content.

$filedata= file_get_contents($tmpUploadedLocation);

and then pass this information to Java EJB façade which accepts byte array saveFileContents(byte[] contents)

Here is how in PHP I converted the $filedata into byte array.

$bytearrayData = unpack("C*",$filedata);

and finally called the Java service (Java service object was retrieved using php-java-bridge)

$javaService->saveFileContents($bytearrayData);

This works fine if file size is less, but if the size increase 2.9 MB, I receive an error and hence file contents are not saved on to the disk.

Fatal error: Allowed memory size of 134217728 bytes exhausted //This is PHP side error due to unpack

I am not sure how to do this, Above method is not accurate, Please I have few limits.

  1. The engine(Java) is responsible for saving and retrieving the contents.
  2. PHP-HTML is the front end application, It could be any thing for now its just PHP
  3. PHP communicate with Java using PHP-Java-Bridge
  4. EJB's methods are accessed by PHP for saving and retrieving information.

Everything was working fine with above combination, but now its about upload and saving documents. It is EJB (Application Engine access point) that will be used for any front-end application (PHP or another java application through remote interface (lookups)).

My question is how File contents from PHP can be sent to Java, where it does not break any thing (Memory)?

PHP Avenger
  • 1,744
  • 6
  • 37
  • 66
  • There are often file upload limitations configured in PHP. That could be a cause too: http://stackoverflow.com/questions/2184513/php-change-the-maximum-upload-file-size – Joop Eggen Apr 01 '14 at 14:13
  • @JoopEggen above limitations are already covered, I am able to upload file but issue is to forward it to Java engine. – PHP Avenger Apr 02 '14 at 07:48

1 Answers1

0

Instead of converting a file into an array I'd try to pass it as string. Encode the string into base64 in PHP and decode it into array in Java.

Another option is to pass the file thru the filesystem. Some Linux systems have /dev/shm or /run/shm mounted to a tmpfs, which is often a good way to pass temporary data between programs without incurring a hard-drive overhead. A typical tmpfs algorithm is 1) create a folder; 2) remove old files from it (e.g. files older than a minute); 3) save the new file; 4) pass the file path to Java; 5) remove the file. Step 2 is important in order not to waste RAM if steps 3-5 are not completed for some reason.

ArtemGr
  • 11,684
  • 3
  • 52
  • 85
  • I have tried passing string, an empty string is received on Java side, however I haven't tried first converting it to the base64 (Though it will increase the size of the contents). – PHP Avenger Apr 02 '14 at 07:35
  • Sorry I think you are assuming both front end and back end application are on same server??? I apologize in advance if I miss understood. – PHP Avenger Apr 02 '14 at 07:45
  • Yeah, try packing with base64. I've assumed the same server, yes. – ArtemGr Apr 02 '14 at 08:58