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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.