Jump to content

.doc download problem


grejon04

Recommended Posts

I'm having a problem downloading doc files. The files download, but when opened, contain all sorts of gibberish. The files are fine on the server. PDF files downloaded and run from the same script work fine.

 

The site is run over SSL. Apache, MAC OSX server, connected to MySQL database.

 

Any help would be appreciated. I'm really stumped, here.

 

jonah

 

<?php

require_once 'includes/securitycheck.inc';
require_once 'includes/dbconnect.inc';
require_once "includes/manuscript_functions.inc";
require_once "includes/functions.inc";
$query = "SELECT title FROM `manuscript`
WHERE manuscript_id = " . $_GET['manuscript_id'];

$m_result = mysql_query($query) or catch_error($query, mysql_error(), $_SERVER['REQUEST_URI']);
$row = mysql_fetch_array($m_result);

while ($i <= 20){

$c_char = substr($row[title], $i, 1);

if (ereg("[a-zA-Z0-9]",$c_char))
$title .= $c_char;

if ($c_char == " ")
$title .= "_";

$i++;

}
//header("Content-type: Text");
//echo $title;

$go = true;
//echo "$title outside version<BR>";
if ($_GET['version'] == "original"){
$title .= "_original";

if ($_GET['type'] == 'doc' && file_exists("/u/staff/jrme/jrme_files/manuscripts/" . $_GET['manuscript_id'] . "o.doc")){
$file = "/u/staff/jrme/jrme_files/manuscripts/" . $_GET['manuscript_id'] . "o.doc";
}else{
$file = "/u/staff/jrme/jrme_files/manuscripts/" . $_GET['manuscript_id'] . "o.pdf";
$_GET['type'] = 'pdf';
}

$query = "SELECT * FROM `manuscript_authors`
WHERE manuscript_id = " . $_GET['manuscript_id'] . "
AND `person_id` = " . $_SESSION['person_id'];

$ma_result = mysql_query($query) or catch_error($query, mysql_error(), $_SERVER['REQUEST_URI']);
$is_rc = is_rc_man($_GET[manuscript_id]);

if ((mysql_num_rows($ma_result) > 0) || ($_SESSION[access_level] > 2) || (($_SESSION[access_level] == 2) && $is_rc))
$go = true;

else
$go = false;

}



elseif ($_GET['version'] == 'lc') {

$title .= "_letter_of_changes";

if ($_GET['type'] == 'doc')
$file = "/u/staff/jrme/jrme_files/manuscripts/" . $_GET['manuscript_id'] . "lc.doc";

elseif ($_GET['type'] == 'pdf')
$file = "/u/staff/jrme/jrme_files/manuscripts/" . $_GET['manuscript_id'] . "lc.pdf";

}



else {

$title .= "_blinded";

if ($_GET['type'] == 'doc')
$file = "/u/staff/jrme/jrme_files/manuscripts/" . $_GET['manuscript_id'] . "b.doc";

elseif ($_GET['type'] == 'pdf')
$file = "/u/staff/jrme/jrme_files/manuscripts/" . $_GET['manuscript_id'] . "b.pdf";

}
// echo $file;
//remove for normal file



if($go && file_exists($file)){

header('Content-Description: File Transfer');
header("Pragma: cache");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");



if ($_GET['type'] == 'doc')
header('Content-Type: application/msword');

if ($_GET['type'] == 'pdf')
header('Content-Type: application/pdf');

header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($file));

if ($_GET['type'] == 'doc')
header('Content-Disposition: attachment; filename=' . $title . ".doc");

elseif ($_GET['type'] == 'pdf')
header('Content-Disposition: attachment; filename=' . $title . ".pdf");
//old way of doing it
//readfile($file);
readfile_chunked($file,false);

exit();

}



elseif (!$go) {

echo "You do not have permission to access this file!";

}

else {

echo "File does not exist! ($file)";

}


?>

Link to comment
https://forums.phpfreaks.com/topic/63745-doc-download-problem/
Share on other sites

Narrowed down code some...

 

	if($go && file_exists($file)){

	header('Content-Description: File Transfer');
	header("Pragma: cache");
	header("Expires: 0");
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
	header("Cache-Control: public");

	session_cache_limiter("must-revalidate");


	if ($_GET['type'] == 'doc')
		header('Content-Type: application/msword');

	if ($_GET['type'] == 'pdf')
		header('Content-Type: application/pdf');

	header("Content-Transfer-Encoding: binary");
	header('Content-Length: ' . filesize($file));

	if ($_GET['type'] == 'doc')
		header('Content-Disposition: attachment; filename=' . $title . ".doc");

	elseif ($_GET['type'] == 'pdf')
		header('Content-Disposition: attachment; filename=' . $title . ".pdf");
	//old way of doing it
	//readfile($file);
		readfile_chunked($file,false);

	exit();

Link to comment
https://forums.phpfreaks.com/topic/63745-doc-download-problem/#findComment-317687
Share on other sites

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.