问题
I have a script which reads a mp3 file with readfile() (I have also tried fread() with a loop). And on one particular file I am getting a 500 error. All other files seem to read just fine.
When I review the error log on my server I notice I am getting a PHP memory error when ever this particular file is attempted to be read.
Can anyone give me some pointers as to what might be going on?
回答1:
You already pointed out the problem - PHP is running out of memory. Maximum memory usage in PHP is limited by an ini setting.
You can set it at runtime at the top of your script with ini_set:
ini_set('memory_limit', '64M');
Or you can set it in your php.ini file so it takes effect for all scripts:
memory_limit = 64M;
Finally, you can see the memory used by the currently executing script with memory_get_usage()
回答2:
The mp3 file is of a larger filesize than memory_limit
. You'll need to increase the amount of memory PHP can access if you want to read from this mp3 file.
If you have access to the php.ini file, you can find memory_limit = 16M ;
and replace the value with however much memory you need.
If you don't have php.ini access and you do have .htaccess access, add:
php_value memory_limit 16M
and replace the value.
If you have neither, try compressing the mp3 file or otherwise reducing the amount of memory it will take for you to perform this action. Try clearing variables which are unused and take up large amounts of memory.
回答3:
Well, php is probably running out of memory before completing the script. You can just increase the memory php is allowed to run by changing the memory_limit option in your php.ini.
回答4:
Try increasing the execution time: set_time_limit(300); //300 seconds ini_set('memory_limit','16M'); //increase to some value like 16 MB
回答5:
Most likely it exceeds maximum memory. This can be adjusted in the php.ini, check here: http://www.php.net/manual/en/ini.core.php#ini.memory-limit
来源:https://stackoverflow.com/questions/8220650/reading-a-file-with-php-gives-500-error