Altaf Hussain
Altaf Hussain

Reputation: 5202

Large file sizes generated in php

I am not sure if this will be the correct forum to ask this question or i should post it on another stack exchange site, please advice.

I am generating translations files in PHP and translations are stored as below

$lang['fileName_md5_hashed_word'] = 'translation in arabic' 

The lang array has the index of file name appended with underscore md5 hashed word, and the value of that index is the translation for that specific word in that specific file. I generate this file as below :

$fh = fopen($file, 'w');

 //create the array to be stored in the language file
 .....
 .....

fwrite($fh, $translations) //write translations to the file, while $translations is the associative array of the words in the above mentioned format.

This works perfectly for other translations and file size is in KBs like 70 KB, (like normal theme translations, admin translations and modules translations) but for mobile theme translations, the file size is 16 MB (yes 16 MB :( ) and the number of lines of that file is 356 , less than other translation files. I am not sure what is the problem and why its size is that much high.

Can someone please advice what may be the issue?

Thank you

Upvotes: 0

Views: 99

Answers (1)

EmpathicSage
EmpathicSage

Reputation: 1323

This sounds suspicious. UTF-8 and UTF-16 files can take up more space than ASCII text files for non-ASCII characters due to the increase in character byte size, but not that much!

Is there some way you can post a link to the file? I'm going to assume you are running a Linux based operating system for some of my suggestions.

Things to Try

First verify it is really 16 MB. Assuming your file is 'translation.php':

ls -lh translations.php

How long are the lines that are translated? It may only be 356+ lines, but if a lot of those lines are missing carriage returns, they might be very long. Try adding carriage returns after each output and see if that makes a difference.

$myContent += "\n";

You might even try buffering the entire file to a string before writing it to a file. You can then output the string when it gets to a certain size and examine your loop.

You can try sorting the file and processing duplicate lines. Assuming your file is named 'translations.php', you can process this quickly on the command line:

sort translations.php | uniq > translations.php

There is also the possibility you need to remove "gremlins" or hidden control characters.

preg_replace('/[\x00-\x1F\x7F]/', '', $input);

There is also the possibility that there is something wrong with your loop condition that outputs the file. Are you able to provide more code?

Upvotes: 1

Related Questions