views:

20

answers:

0

Part of our web app has a little Ajax method that will load a page in an iFrame or allow you to download it.

We store a bunch of search results from search engines and we have script opens the file containing our info and the search html. We strip out the stuff we don't need from the top (our info) and then we serve that up either by echo'ing the $html variable or putting it in a temporary file and dishing it off to download.

The problem: I load the page in the iFrame and it's loaded in UTF-8 because everything else is. If I download the file manually it is fine and FF tells me the endoding is x-gbk.

I've tried using mb_convert_encoding to no avail. We are using PHP4 on this server.

Thoughts?

EDIT: Code that drives this

f(!isset($_GET['file']) || $_GET['file'] == '')
{
    header("location:index.php");
}
$download = false;

if(!isset($_GET['view']) || $_GET['view'] != 'true')
{
    $download = true;
}

$file = LOG_PATH . $_GET['file'];

$fileName = end(explode("/", $file));

$fh = fopen($file, "rb");

if(!$fh)
{
    echo "There was an error in processing this file. Please retry.";
    return;
}

// Open HTML file, rip out garbage at top, inject "http://google.com" before all "images/"
$html = fread($fh, filesize($file));
fclose($fh);

// Need to trim off our headers
$htmlArr = explode("<!", $html, 2);
$htmlArr[1] = "<!" . $htmlArr[1];   

if(strstr($file, "google"))
{
    $html = str_replace('src="/images/', 'src="http://google.com/images/', $htmlArr[1]);
    $html = str_replace('href="/', 'href="http://google.com/', $html);
}
else if(strstr($file, "/msn/"))
{
    $html = str_replace('src="/images/', 'src="http://bing.com/images/', $htmlArr[1]);
    $html = str_replace('href="/', 'href="http://www.bing.com/', $html);    
}
else
{
    $html = $htmlArr[1];
}

if(strstr($file, "baidu"))
{
    $html = mb_convert_encoding($html, 'utf-8'); // Does not work
}

if($download)
{   
    // Write to temporary file
    $fh = fopen("/tmp/" . $fileName, 'w+');
    fwrite($fh, $html);
    fclose($fh);

    $fh = fopen("/tmp/" . $fileName, "rb");
    header('Content-type: application/force-download;');
    header("Content-Type: text/html;");
    header('Content-Disposition: attachment; filename="' . $fileName . '"');
    fpassthru($fh);
    fclose($fh);
    unlink("/tmp/" . $fileName);
}
else // AJAX Call
{
    echo $html;
}