Click here to Skip to main content
15,886,110 members
Home / Discussions / Linux, Apache, MySQL, PHP
   

Linux, Apache, MySQL, PHP

 
AnswerRe: Linux Distribution Pin
Eddy Vluggen14-Nov-11 9:01
professionalEddy Vluggen14-Nov-11 9:01 
GeneralRe: Linux Distribution Pin
Albert Holguin17-Nov-11 9:54
professionalAlbert Holguin17-Nov-11 9:54 
SuggestionRe: Linux Distribution Pin
Mohibur Rashid1-Dec-11 19:44
professionalMohibur Rashid1-Dec-11 19:44 
AnswerRe: Linux Distribution Pin
DigiOz Multimedia5-Dec-11 4:39
DigiOz Multimedia5-Dec-11 4:39 
GeneralRe: Linux Distribution Pin
Albert Holguin5-Dec-11 4:51
professionalAlbert Holguin5-Dec-11 4:51 
AnswerRe: Linux Distribution Pin
loctrice29-Dec-11 17:16
professionalloctrice29-Dec-11 17:16 
QuestionImport Google contacts using Authsub(Secure connection) Pin
Paresh14621-Nov-11 3:31
Paresh14621-Nov-11 3:31 
QuestionHow to debug PHP Memory Leak Pin
Logan Black30-Oct-11 14:17
Logan Black30-Oct-11 14:17 
Hi guys,

using MAMP v2.0 on Mac ____ Apache/2.0.64 (Unix) -- PHP/5.3.5 -- DAV/2 mod_ssl/2.0.64 -- OpenSSL/0.9.7l -- MySQL 5.5.9

I have a script I am trying to run and It appears to be giving me major memory leaks, which I have attempted to debug and cannot work out how to fix.

Basically, the script is part of a file manager module. It processes the download of a file when given an ID.

The entire file is stored in a database table, as a BLOB, in 64kb chunks (per record), and is streamed down to the client on request.

Database: file_management

Tables: file_details, file_data

file_details:
FileID - int(10) AUTO_INCREMENT
FileTypeID - int(10)
FileType - varchar(60)
FileName - varchar(255)
FileDescription - varchar(255)
FileSize - bigint(20)
FileUploadDate - datetime
FileUploadBy - int(5)

file_details:
FileDataID	 - int(10) AUTO_INCREMENT
FileID - int(10)
FileData - BLOB


The error I am actually getting is this one (from php error log):

[31-Oct-2011 09:47:39] PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 63326173 bytes) in /root/htdocs/file_manager/file_manager_download.php on line 150

Now, the actual function of downloading works if the file is small enough, in this case, less than 40mb, however once it goes over that, like the 60mb file in the error above, it fails. All it does is download a 0kb file.

Obviously, 134217728 bytes is more than 63326173 bytes (128mb vs 60mb).

the Allowed memory size of 134217728 bytes is the directive in php.ini: "memory_limit = 128M ; Maximum amount of memory a script may consume"

If I set this to 256M, it allows me to download that 60mb file, as well as up to about an 80mb file.

Also, if I set this to 1024M it allows me to download a 260mb file and possibly bigger.

So you can see that the problem is a leak somewhere in the script that is eating up all the memory.


Here is the download script:

PHP
<?php

/* Processing for streaming files to the client */

ini_set('display_errors',1);
error_reporting(E_ALL & ~E_NOTICE);

$strDB=mysql_connect("localhost","username","password")or die ("Error connecting to mysql.. Error: (" . mysql_errno() . ") " . mysql_error());
$database=mysql_select_db("file_management",$strDB);

if (isset($_GET["id"])) {

	// List of nodes representing each 64kb chunk
	$nodelist = array();
		
	// Pull file meta-data
	$sql_GetFileDetails = "
	SELECT 
	FileID,
	FileTypeID,
	FileType,
	FileName,
	FileDescription,
	FileSize,
	FileUploadDate,
	FileUploadBy
	FROM file_details WHERE FileID = '".$_GET["id"]."';";
	
	$result_GetFileDetails = mysql_query($sql_GetFileDetails) or die ("No results for this FileID.<br>Your Query: " . $sql_GetFileDetails . "<br> Error: (" . mysql_errno() . ") " . mysql_error());
	
	if (mysql_num_rows($result_GetFileDetails) != 1) { die ("A MySQL error has occurred.<br>Your Query: " . $sql_GetFileDetails . "<br> Error: (" . mysql_errno() . ") " . mysql_error()); }
	
	// Set the file object to get details from
	$FileDetailsArray = mysql_fetch_assoc($result_GetFileDetails);
	
	// Pull the list of file inodes
	$sql_GetFileDataNodeIDs = "SELECT FileDataID FROM file_data WHERE FileID = ".$_GET["id"]." order by FileDataID";
	
	if (!$result_GetFileDataNodeIDs = mysql_query($sql_GetFileDataNodeIDs)) { die("Failure to retrive list of file inodes<br />Your Query: " . $sql_GetFileDataNodeIDs . "<br /> Error: (" . mysql_errno() . ") " . mysql_error()); }
	
	while ($row_GetFileDataNodeIDs = mysql_fetch_assoc($result_GetFileDataNodeIDs)) {
		$nodelist[] = $row_GetFileDataNodeIDs["FileDataID"];
	}
	
	
	$FileExtension = explode(".",$FileDetailsArray["FileName"]);
	$FileExtension = strtolower($FileExtension[1]);
	
	// Determine Content Type 
	switch ($FileExtension) { 
	
		case "mp3":		$ctype="audio/mp3"; break;
		case "wav":		$ctype="audio/wav"; break;
		case "pdf":		$ctype="application/pdf"; break;
		//case "exe":		$ctype="application/octet-stream"; break;
		case "zip":		$ctype="application/zip"; break;
		case "doc":		$ctype="application/msword"; break;
		case "xls":		$ctype="application/vnd.ms-excel"; break;
		case "ppt":		$ctype="application/vnd.ms-powerpoint"; break;
		case "gif":		$ctype="application/force-download"; break; // This forces download, instead of viewing in browser.
		case "png":		$ctype="application/force-download"; break; // This forces download, instead of viewing in browser.
		case "jpeg":	$ctype="application/force-download"; break; // This forces download, instead of viewing in browser.
		case "jpg":		$ctype="application/force-download"; break; // This forces download, instead of viewing in browser.
		default: 		$ctype="application/force-download"; 		// This forces download, instead of viewing in browser.
	} 
	
	// Send down the header to the client
	

	header("Date: ".gmdate("D, j M Y H:i:s e", time()));
	header("Cache-Control: max-age=2592000");
	//header("Last-Modified: ".gmdate("D, j M Y H:i:s e", $info['mtime']));
	//header("Etag: ".sprintf("\"%x-%x-%x\"", $info['ino'], $info['size'], $info['mtime']));
	header("Accept-Ranges: bytes");
	//header("Cache-Control: Expires ".gmdate("D, j M Y H:i:s e", $info['mtime']+2592000));
	header("Pragma: public"); // required
	header("Expires: 0");
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
	header("Cache-Control: private",false); // required for certain browsers
	header("Content-Description: File Transfer");
	header("Content-Disposition: attachment; filename=\"".$FileDetailsArray["FileName"]."\"");
	header("Content-Transfer-Encoding: binary");
	header("Content-Type: ".$FileDetailsArray["FileSize"]);

	ob_end_clean();
	ob_start();
	ob_start("ob_gzhandler");
	
		$sql_GetFileDataBlobs = "SELECT FileData FROM file_data WHERE FileID = ".$_GET["id"]." ORDER BY FileDataID ASC;";
		
		if (!$result_GetFileDataBlobs = mysql_query($sql_GetFileDataBlobs)) { die("Failure to retrive list of file inodes<br />Your Query: " . $sql_GetFileDataBlobs . "<br /> Error: (" . mysql_errno() . ") " . mysql_error()); }
		
		while ($row_GetFileDataBlobs = mysql_fetch_array($result_GetFileDataBlobs)) {
			echo $row_GetFileDataBlobs["FileData"];
		}

	
	ob_end_flush();
	header('Content-Length: '.ob_get_length());
	ob_end_flush();
		
}
?>


I have used Xdebug and output the results for peak memory usage, but nothing appears to be going anywhere near the limits, in total the peak memory usage was something like 900kb for the page.

So I am thinking it is aggregating the file chunks into memory and not letting them go, or something similar, but the file chunks are the only thing that would reach that amount of memory, causing the script to fail.

I can provide the script to upload a file to the database so you can test my script if you like, just let me know

Cheers for any help!

Mick

modified 30-Oct-11 21:03pm.

AnswerRe: How to debug PHP Memory Leak Pin
Logan Black30-Oct-11 16:00
Logan Black30-Oct-11 16:00 
AnswerRESOLVED: How to debug PHP Memory Leak Pin
Logan Black30-Oct-11 17:10
Logan Black30-Oct-11 17:10 
GeneralRe: RESOLVED: How to debug PHP Memory Leak Pin
Mohibur Rashid31-Oct-11 18:01
professionalMohibur Rashid31-Oct-11 18:01 
QuestionProblem receiving arabic emails Pin
Jassim Rahma27-Oct-11 7:38
Jassim Rahma27-Oct-11 7:38 
AnswerRe: Problem receiving arabic emails PinPopular
Graham Breach27-Oct-11 9:26
Graham Breach27-Oct-11 9:26 
QuestionHow to make a Cloud text from database Pin
Jassim Rahma26-Oct-11 10:58
Jassim Rahma26-Oct-11 10:58 
AnswerRe: How to make a Cloud text from database Pin
User 171649226-Oct-11 11:39
professionalUser 171649226-Oct-11 11:39 
QuestionFacebook-Like modal box Pin
Jassim Rahma25-Oct-11 6:26
Jassim Rahma25-Oct-11 6:26 
AnswerRe: Facebook-Like modal box Pin
Mohibur Rashid25-Oct-11 15:40
professionalMohibur Rashid25-Oct-11 15:40 
Questionsignature_invalid in Google API Pin
Paresh146225-Oct-11 2:36
Paresh146225-Oct-11 2:36 
AnswerRe: signature_invalid in Google API Pin
Niall Barr26-Oct-11 3:21
professionalNiall Barr26-Oct-11 3:21 
Questiondata was not displayed in the application in the correct format. Pin
Honeyboy_2025-Oct-11 1:01
Honeyboy_2025-Oct-11 1:01 
AnswerRe: data was not displayed in the application in the correct format. Pin
cjoki4-Nov-11 10:30
cjoki4-Nov-11 10:30 
QuestionBest Practices - mysql calls Pin
Dwayner7920-Oct-11 13:59
Dwayner7920-Oct-11 13:59 
AnswerRe: Best Practices - mysql calls Pin
Mohibur Rashid21-Oct-11 0:02
professionalMohibur Rashid21-Oct-11 0:02 
GeneralRe: Best Practices - mysql calls Pin
Dwayner7923-Oct-11 7:40
Dwayner7923-Oct-11 7:40 
GeneralRe: Best Practices - mysql calls Pin
Mohibur Rashid23-Oct-11 12:05
professionalMohibur Rashid23-Oct-11 12:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.