Jump to content

ob_start() and fwrite() problem... not writing to file...?


zeeshan_haider000

Recommended Posts

Hi there,

I am trying to create a file using fwrite function, but for something reason all I get is a blank file with no content inside.

Here is the code, go to the bottom where the fwrite function is called:

<?php //Get Nohay Albums function
function GetAlbums($cattype){
    if ((isset($_GET['year']))||(isset($_GET['reciter']))){ //nothing displayed 
    }
    
    else{
        
    $query = "SELECT COUNT(Topic), SUM(Hits) AS totalhits, FileFormat, Speaker, Language, Year, FileType, Cat FROM nauhey WHERE Cat='".addslashes($cattype)."' AND Year='".date('Y')."' GROUP By Speaker, Year";
    $result = mysql_query($query) or die (mysql_error());

//<!------------ Create Cache -------------!>
//folder where cache is saved
$path = "cache/";
//Page name format: FunctionName_Category_Year.html
$pagename = $path."getalbums_".$cattype.".html";
$cacheFile = fopen($pagename, "w");
if ($cacheFile == FALSE){ 
    die ("Unable to create cache file"); }
    
//Get HTML in a variable
$obstart = ob_start();
if ($obstart == FALSE){ 
    die ("Unable to buffer cache content!"); }
//<!------------ Cache closed -------------!>

if ($cattype = strtolower('nauhey')){
    while ($row = mysql_fetch_array($result)){
    echo ("<div class='container'>");
    echo ("<img src=\"http://localhost/HAQMOLAAALI/backed up/haqmolaali/images/".$row['FileFormat'].".gif\">");

    echo ("<b style=\"font-size:10px; color:#000066; position:absolute; margin-left: 500px;\">DOWNLOADS: ".$row['totalhits']."</b>");
    echo (" <a href=\"nohay.php?reciter=".urlencode($row['Speaker'])."&year=".urlencode($row['Year'])."\">".$row['Speaker']." Vol ".$row['Year']."</a> <b style=\"font-size:10px; color:#000000;\">(".strtoupper($row['Language']).")</b>"."\n");
echo ("<div class='small'><b>Year: ".$row['Year']." | Language: ".ucfirst($row['Language'])." | Tracks: ".$row['COUNT(Topic)']."</b></div>");
    echo ("</div>");
    }//while loop closed
        
        }
else {
    while ($row = mysql_fetch_array($result)){
    echo ("<div class='container'>");
    echo ("<img src=\"http://localhost/HAQMOLAAALI/backed up/haqmolaali/images/".$row['FileFormat'].".gif\">");
echo ("<b style=\"font-size:10px; color:#000066; position:absolute; margin-left: 500px;\">DOWNLOADS: ".$row['totalhits']."</b>");
echo (" <a href=\"majlis.php?reciter=".urlencode($row['Speaker'])."&year=".urlencode($row['Year'])."\">".$row['Speaker']." Vol ".$row['Year']."</a> <b style=\"font-size:10px; color:#000000;\">(".strtoupper($row['Language']).")</b>");
echo ("<div class='small'><b>Year: ".$row['Year']." | Language: ".ucfirst($row['Language'])." | Lectures in series: ".$row['COUNT(Topic)']."</b></div>");
    echo ("</div>");
    
    }
        }
        
//<!------------ Cache Code -------------!>
$cacheContent = ob_get_contents();
//Write to cache file
fwrite ($cacheFile, $cacheContent) or die ("Cannot write cache content to file!");
fclose ($cacheFile);
ob_flush();
ob_end_clean();
//<!------------ Cache Closed-------------!>
    
    }
}

GetAlbums("nauhey");
?>

 

When I execute the function, I get my self-made error "Cannot write cache content to file!". Help would be much appreciated. Thank you.

You need to use mysql_num_rows() to find out if your query matched any rows.

 

You should only process the results of the query if there are any rows and if you are getting zero rows, but you think your query should return something, you would need to troubleshoot why your query is not matching any rows.

Okay, I finally found what the problem was, it was a stupid spelling mistake that I made when calling the function. GetAlbums("nauhey") should have been GetAlbums("nohay"). Now it's working properly. Spent so much time on this small silly mistake.

Thanks for replying @ PFMaBiSmAd.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.