Jump to content

Recommended Posts

Hey friends,

 

I have this problem. I have written a script where users can upload MS Word files. So far so good but I also wanted to make them download MS Word files. So I wrote the PHP script that allows users to download files as well, here it is:

 

header("Content-Type: application/vnd.ms-word");
//open/save dialog box
header('Content-Disposition: attachment; filename="syllabus.docx"');
//read from server and write to buffer
readfile('uploaded_files/syllabuses/syllabus.docx');

 

But when the user the redirected to that page to download the file, everything goes fine until he/she tries to open in the their MS Word program and this error appears:

 

First error

 

And when I click OK, the second error appears:

 

Second error

 

 

And when I click OK on the second errors, the word documents works just file and everything is OK.

 

But these errors are really annoying and may cause trouble to other people downloading this file.

 

Could you please tell me how to fix them?

 

Thank you!

 

Best Regards

Stefany

Sounds like it could be a charset issue. Though, without seeing the actual data, the rest of the relevant code, and testing it myself, it's hard to say for sure.

In short: We're only guessing at this point, as you've provided us with too little information. Please post the rest of the code, plus anything else you think might be related to the handling of the files.

Sounds like it could be a charset issue. Though, without seeing the actual data, the rest of the relevant code, and testing it myself, it's hard to say for sure.

In short: We're only guessing at this point, as you've provided us with too little information. Please post the rest of the code, plus anything else you think might be related to the handling of the files.

 

Sorry about that!

 

Here is the code that I have written for uploading the file I am trying to basically download.

 

<?php	
include 'core/init.php';
include 'includes/overall/overallheader.php'; 
?>


<form action="" method="post" enctype="multipart/form-data">

<h2>Upload a syllabus</h2>
<hr></hr>
<input type="file" name="syllabus"/>
<input type="submit" value="upload" />
</form>


<?php

if(isset($_FILES['syllabus'])){
$allowed_exts = array('asd','cnv','doc','docm','docx','dot','dotm','dotx',' wbk','wll','svd');
$name = $_FILES['syllabus']['name'];
 @$ext = strtolower(end(explode('.',$_FILES['syllabus']['name'])));
$size = $_FILES['syllabus']['size'];
 $tmp = $_FILES['syllabus']['tmp_name'];
if(in_array($ext, $allowed_exts) === false){
	$errors[] = 'Extension not allowed!';

}

if($size > 10485760){
	$errors[] = 'Uploaded files must <= 10mb';

}

if(empty($errors)){
	if(move_uploaded_file($tmp, 'uploaded_files/syllabuses/'.$name)){
		echo 'Your file has been successfuly uploaded!';

	}

}else{
	foreach($errors as $error){
		echo 'Error: <strong>',$error,'<strong><br /> ';
		return false;


	}

}

}


include 'includes/overall/overallfooter.php'; ?>

Well, according MIME type files located on my local linux server in directory /etc/mime.types

The proper MIME type for .docx files is:

application/vnd.openxmlformats-officedocument.wordprocessingml.document

 

You can also create a hidden .htaccess file in your home directory adding this code

AddType  application/vnd.openxmlformats  .docx .pptx .xlsx .xltx . xltm .dotx .potx .ppsx

http://www.freetutorialssubmit.com/apache-mime-type-configuration-to-open-docx-pptx-xlsx-ms-office-files/1436

 

For beginning, try to change MIME type

header("Content-Type: application/vnd.ms-word"); 

to 

header("Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");

 

Is it a Bulgarian hosting server?

 

Afraid the .htaccess suggestion isn't going to work, unless you allow for direct downloading of the file from the server.

However, set the proper header for the document, and see if it solves the issue. If not, then I recommend looking at the actual headers sent by the server and compare them to the meta-data from the document itself.

Check for output buffers!

 

Since you don't seem to be adept in google-fu, Use this -

 

<?php

// Comment everything out to check for errors

//header("Content-Type: application/vnd.ms-word");
//open/save dialog box
//header('Content-Disposition: attachment; filename="syllabus.docx"');
//read from server and write to buffer
//readfile('uploaded_files/syllabuses/syllabus.docx');

if( ob_get_level() )
die("You've got buffering issues");

?>

I think, that should be work.

Try,

 

ob_start();
header("Content-Type: application/vnd.ms-word");
//open/save dialog box
header('Content-Disposition: attachment; filename="syllabus.docx"');
ob_clean();
flush();
//read from server and write to buffer
readfile('uploaded_files/syllabuses/syllabus.docx');

You're clearing what should be an empty output, which does nothing. If it's errors that are getting cleaned, you're suppressing something very important to proper script execution.

 

It's good that you're trying to help, but please understand what your code is doing before offering it.

Well, this is the script, that I've tested into my local server.

ob_start();
$file = 'syllabus.docx';
header('Content-Description: File Transfer');
header("Content-Type: application/vnd.openxmlformats");
//open/save dialog box
header('Content-Disposition: attachment; filename='.  basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
//read from server and write to buffer
readfile($file);

 

My output_buffering is Off in my php file. 

 

Where did you find a problem in my script above ?

 

 

My output_buffering is set to Off in my php.ini file.

Sorry about that 

PS, without buffering and header('Content-Length: ' . filesize($file));, I've got a error message, as Stefi, when I try to open a file with microsoft office word 2010.

 

And your code simply prevents the output of the error message, rather than FIXING THE ERROR.

 

Matt, I've tested the script to two different hosting, their output_buffering are set to off in php.ini files.

The code that I'm using is:

<?php
$file = 'syllabus.docx';
header('Content-Description: File Transfer');
header("Content-Type: application/vnd.openxmlformats");
header('Content-Disposition: attachment; filename='.  basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);

 

Godaddy (php -5.3.3) - http://kaneffbrookside.ca/home.php

Bulgarian hosting (php 4.3.) -  http://canada.lesno.net/home.php

 

Jazzman1: Regardless of the output buffering being enabled or not in the configuration file1, whenever you call ob_start () you are manually starting an output buffer. Which is what Xyph was saying "no" to, as you didn't just obscure the error but you actively threw it away.

The last code you posted, however, looks better. Though, you still haven't taken into consideration that any of the function calls could fail. Thus, ending up with the same problem again.

 

Stefany: What you should take home from this is that you should always do all of the processing before sending any output to the client. This means moving the function calls above the header () calls, saving the results into variables, and checking for errors. That way you won't corrupt the files if something happens, but you'll find yourself in a position to correctly handle the error condition instead.

 

1In fact, if this directive is turned on, PHP will automatically start an output buffer at every page load. Which would render the ob_start () call rather superfluous, at least in this case.

Thank you very much for the help everyone but the same error appears once again but not every time thank you very much for the help, you are awesome! So I decided to make if anyone wants to upload a docx document to archive it in a zip format first. Now the errors are gone. Thank you!

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.