Php output xml (excel export) data is truncated when it is too long-PHP source code

Source: Internet
Author: User
Tags apc configuration settings
If the file data generated in the system is too long and then automatically truncated, what is the cause? This small editor has encountered problems caused by the loss of post data, let's take a look. If the file data generated in the system is too long and then automatically truncated, what is the cause? This small editor has encountered problems caused by the loss of post data, let's take a look.

Script ec (2); script


When using xml to export excel, excel can be exported Normally when a small number (less than 1 K) is found, but when the export quantity is increased (several K), it is found that the exported excel data is incomplete.

(It's good on my local computer. If the server runs online, there is a problem ...)
Check the exported xml data and find that the content is truncated. Sometimes the xml tags are incomplete.

Php-xml-excel

It is suspected that the output content is too long in bytes, which exceeds the upper limit of the Environment configuration. Check the output_buffering and nginx fastcgi_buffer settings. Like my local value, the modification has no effect.

Then Google went down and found a similar situation, which was caused by permission issues, so I checked the nginx error log error. log.
The following error records are found during the export operation:

09:47:53 [crit] 19027 #0: * 967389 mkdir () "/tmp/fastcgi_temp/xxx" failed (13: Permission denied) while reading upstream, client: xxx. xxx. xx. xxx

It seems that the permission is correct. During the export, nginx needs to create a directory of/tmp/fastcgi_temp/xxx, but fails.
Run to/tmp and check that there is no fastcgi_temp directory !! Then, manually create the directory/tmp/fastcgi_temp/and set the owner www and permission 744.

Run the export again. The xml truncation problem has been solved.

PHPExcel export of a large amount of data timeout and memory error Solution

PHPExcel is a powerful PHP open-source class for processing Excel, but a major problem is that it occupies too much memory. From 1.7.3, PHPExcel supports cell caching, however, we recommend that you use the stable version 1.7.6, because previous versions have bugs of different degrees. The following is the official documentation:

PHPExcel1.7.6 official documentation

PHPExcel uses an average of about 1 k/cell in your worksheets, so large workbooks can quickly use up available memory. cell caching provides a mechanisms that allows PHPExcel to maintain the cell objects in a smaller size of memory, on disk, or in APC, memcache or Wincache, rather than in PHP memory. this allows you to reduce the memory usage for large workbooks, although at a cost of speed to access cell data.

PHPExcel uses 1 k/Cell Memory on average. Therefore, large files may cause high memory consumption. The cell cache mechanism allows PHPExcel to cache small cell objects in memory in disks, APC, memcache, or Wincache, although it takes some time to read data, but it can help you reduce memory consumption.

PHPExcel1.76. official documentation

By default, PHPExcel still holds all cell objects in memory, but you can specify alternatives. to enable cell caching, you must call the PHPExcel_Settings: setCacheStorageMethod () method, passing in the caching method that you wish to use.

By default, PHPExcel still saves cell objects in memory, but you can customize them. You can use the PHPExcel_Settings: setCacheStorageMethod () method to pass the cache method as a parameter to this method to set the cache method.

Php code:

$ CacheMethod = PHPExcel_CachedObjectStorageFactory: cache_in_memory;

PHPExcel_Settings: setCacheStorageMethod ($ cacheMethod );

PHPExcel1.7.6 official documentation

SetCacheStorageMethod () will return a boolean true on success, false on failure (for example if trying to cache to APC when APC is not enabled ).

The setCacheStorageMethod () method returns a Boolean variable to indicate whether the setting is successful. (for example, if APC cannot be used, if you set to use APC cache, false is returned)

PHPExcel1.7.6 official documentation

A separate cache is maintained for each inpidual worksheet, and is automatically created when the worksheet is instantiated based on the caching method and settings that you have configured. you cannot change the configuration settings once you have started to read a workbook, or have created your first worksheet.

Each worksheet has an independent cache. When a worksheet is instantiated, it is automatically created based on the configured or configured cache mode. Once you start to read a file or you have created the first worksheet, you cannot change the caching method.

PHPExcel1.7.6 official documentation

Currently, the following caching methods are available.

Currently, the following cache methods are available:

Php code:

PHPExcel_CachedObjectStorageFactory: cache_in_memory;

PHPExcel1.7.6 official documentation

The default. If you don't initialise any caching method, then this is the method that PHPExcel will use. Cell objects are maintained in PHP memory as at present.

By default, if you do not initialize any caching method, PHPExcel uses the memory cache method.

========================================================== ========

Php code:

PHPExcel_CachedObjectStorageFactory: cache_in_memory_serialized;

PHPExcle1.7.6 official documentation

Using this caching method, cells are held in PHP memory as an array of serialized objects, which has CES the memory footprint with minimal performance overhead.

Using this cache method, cells are stored in the memory in a serialized manner, which is a solution to reduce memory usage performance.

========================================================== ========

Php code:

PHPExcel_CachedObjectStorageFactory: cache_in_memory_gzip;

PHPExcel1.7.6 official documentation

Like cache_in_memory_serialized, this method holds cells in PHP memory as an array of serialized objects, but-gzipped to reduce the memory usage still further, although access to read or write a cell is slightly slower.

Similar to the serialization method, this method is compressed by gzip and then put into the memory after serialization. This method further reduces memory usage, but reading and writing will be slow.

========================================================== ==============================

Php code:

PHPExcel_CachedObjectStorageFactory: cache_to_discISAM;

PHPExcel1.7.6 official documentation

When using cache_to_discISAM all cells are held in a temporary disk file, with only an index to their location in that file maintained in PHP memory. this is slower than any of the cache_in_memory methods, but significantly CES the memory footprint. the temporary disk file is automatically deleted when your script terminates.

When cache_to_discISAM is used, all cells will be saved in a temporary disk file, and only their location in the file will be saved in the PHP memory, this is slower than any caching method in the memory, but can significantly reduce the memory usage. Temporary disk files are automatically deleted after the script is run.

========================================================== ==============================

Php code:

PHPExcel_CachedObjectStorageFactory: cache_to_phpTemp;

PHPExcel1.7.6 official documentation

Like cache_to_discISAM, when using cache_to_phpTemp all cells are held in the php: // temp I/O stream, with only an index to their location maintained in PHP memory. in PHP, the php: // memory wrapper stores data in the memory: php: // temp behaves similarly, but uses a temporary file for storing the data when a certain memory limit is reached. the default is 1 MB, but you can change this when initialising cache_to_phpTemp.

Similar to cache_to_discISAM. When cache_to_phpTemp is used, all cells will still exist in the php: // temp I/O Stream and only store their locations in the PHP memory. PHP: // memory package of php stores data in the memory. php: // temp performs similar operations. However, when the size of the stored data exceeds the memory limit, the data will be stored in the temporary file. The default size is 1 MB, but you can modify it during initialization:

Php code:

$ CacheMethod = PHPExcel_CachedObjectStorageFactory: cache_to_phpTemp;

$ CacheSettings = array ('memorycachesize' => '8mb ');

PHPExcel_Settings: setCacheStorageMethod ($ cacheMethod, $ cachesetd );

PHPExcel1.7.6 official documentation

The php: // temp file is automatically deleted when your script terminates.

Php: // The temp file will be automatically deleted after the script ends.

========================================================== ==============================

Php code:

PHPExcel_CachedObjectStorageFactory: cache_to_apc;

PHPExcle1.7.6 official documentation

When using cache_to_apc, cell objects are maintained in APC with only an index maintained in PHP memory to identify that the cell exists. by default, an APC cache timeout of 600 seconds is used, which shocould be enough for most applications: although it is possible to change this when initialising cache_to_APC.

When cach_to_apc is used, the cell is saved in APC and only the index is saved in memory. By default, the APC cache timeout time is 600 seconds, which is sufficient for most applications. You can also modify the value during initialization:

Php code:

$ CacheMethod = PHPExcel_CachedObjectStorageFactory: cache_to_APC;

$ Cachesetask= array ('cachetime' => 600 );

PHPExcel_Settings: setCacheStorageMethod ($ cacheMethod, $ cachesetd );

PHPExcel1.7.6 official documentation

When your script terminates all entries will be cleared from APC, regardless of the cacheTime value, so it cannot be used for persistent storage using this mechanic.

When the script stops running, all the data will be clear from the APC (ignore Cache Time). This mechanism cannot be used as a persistent cache.

========================================================== ==============================

Php code:

PHPExcel_CachedObjectStorageFactory: cache_to_memcache

PHPExcel1.7.6 official documentation

When using cache_to_memcache, cell objects are maintained in memcache with only an index maintained in PHP memory to identify that the cell exists.

By default, PHPExcel looks for a memcache server on localhost at port 11211. it also sets a memcache timeout limit of 600 seconds. if you are running memcache on a different server or port, then you can change these defaults when you initialise cache_to_memcache:

When cache_to_memory is used, the Cell Object is saved in memcache and only the index is saved in memory. By default, PHPExcel searches for the memcache service on localhost and port 11211. The timeout time is 600 seconds. If you run the memcache service on another server or port, you can modify it during initialization:

Php code:

$ CacheMethod = PHPExcel_CachedObjectStorageFactory: cache_to_memcache;

$ CacheSettings = array ('memcacheserver '=> 'localhost ',

'Memcacheport '=> 11211,

& Apos; cacheTime & apos; = & apos; 600 & apos;

);

PHPExcel_Settings: setCacheStorageMethod ($ cacheMethod, $ cachesetd );

In terms of initialization settings, MS does not support multiple memcache servers polling.

PHPExcel1.7.6 official documentation

When your script terminates all entries will be cleared from memcache, regardless of the cacheTime value, so it cannot be used for persistent storage using this mechanic.

When the script ends, all data is cleared from memcache (the cache time is ignored). This mechanism cannot be used for persistent storage.

========================================================== ==============================

Php code:

PHPExcel_CachedObjectStorageFactory: cache_to_wincache;

PHPExcel1.7.6 official documentation

When using cache_to_wincache, cell objects are maintained in Wincache with only an index maintained in PHP memory to identify that the cell exists. by default, a Wincache cache timeout of 600 seconds is used, which shoshould be enough for most applications: although it is possible to change this when initialising cache_to_wincache.

When cache_towincache is used, cell objects are saved in Wincache and only indexes are saved in memory. By default, the Wincache expiration time is 600 seconds, which is sufficient for most applications, of course, you can also modify it during initialization:

Php code:

$ CacheMethod = PHPExcel_CachedObjectStorageFactory: cache_to_wincache;

$ Cachesetask= array ('cachetime' => 600 );

PHPExcel_Settings: setCacheStorageMethod ($ cacheMethod, $ cachesetd );

PHPExcel official document 1.7.6

When your script terminates all entries will be cleared from Wincache, regardless of the cacheTime value, so it cannot be used for persistent storage using this mechanic.

PHPExcel is still relatively powerful, and the biggest problem is memory usage. When can PHPExcel produce a lightweight version that does not require so many fancy features, just export the version of the most common data!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.