Stefany93 Posted September 5, 2012 Share Posted September 5, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/268021-error-corrupted-word-file-after-downloading-with-php/ Share on other sites More sharing options...
darkfreaks Posted September 5, 2012 Share Posted September 5, 2012 can we see the full code Quote Link to comment https://forums.phpfreaks.com/topic/268021-error-corrupted-word-file-after-downloading-with-php/#findComment-1375484 Share on other sites More sharing options...
xyph Posted September 5, 2012 Share Posted September 5, 2012 You're probably outputting something along with the file. Check and make sure the output buffer isn't turned on. Quote Link to comment https://forums.phpfreaks.com/topic/268021-error-corrupted-word-file-after-downloading-with-php/#findComment-1375491 Share on other sites More sharing options...
Stefany93 Posted September 5, 2012 Author Share Posted September 5, 2012 ^^ Could you please tell me how to check that? Quote Link to comment https://forums.phpfreaks.com/topic/268021-error-corrupted-word-file-after-downloading-with-php/#findComment-1375499 Share on other sites More sharing options...
Christian F. Posted September 5, 2012 Share Posted September 5, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268021-error-corrupted-word-file-after-downloading-with-php/#findComment-1375505 Share on other sites More sharing options...
Stefany93 Posted September 5, 2012 Author Share Posted September 5, 2012 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'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/268021-error-corrupted-word-file-after-downloading-with-php/#findComment-1375521 Share on other sites More sharing options...
darkfreaks Posted September 5, 2012 Share Posted September 5, 2012 some error correction if($size > 12345) {} to: if(intval($size) > 12345) {} Quote Link to comment https://forums.phpfreaks.com/topic/268021-error-corrupted-word-file-after-downloading-with-php/#findComment-1375523 Share on other sites More sharing options...
darkfreaks Posted September 5, 2012 Share Posted September 5, 2012 might refer to this link as well upload images using array it uses the exact same code your using only with a forach loop and file types. Quote Link to comment https://forums.phpfreaks.com/topic/268021-error-corrupted-word-file-after-downloading-with-php/#findComment-1375525 Share on other sites More sharing options...
jazzman1 Posted September 5, 2012 Share Posted September 5, 2012 Stefi, attach a piece of the file to look its content. Quote Link to comment https://forums.phpfreaks.com/topic/268021-error-corrupted-word-file-after-downloading-with-php/#findComment-1375526 Share on other sites More sharing options...
Stefany93 Posted September 5, 2012 Author Share Posted September 5, 2012 Sorry the server here doesn't allow .docx word documents to be uploaded. I will try to convert the file Quote Link to comment https://forums.phpfreaks.com/topic/268021-error-corrupted-word-file-after-downloading-with-php/#findComment-1375530 Share on other sites More sharing options...
Stefany93 Posted September 5, 2012 Author Share Posted September 5, 2012 Here is the file! 18974_.zip Quote Link to comment https://forums.phpfreaks.com/topic/268021-error-corrupted-word-file-after-downloading-with-php/#findComment-1375543 Share on other sites More sharing options...
jazzman1 Posted September 5, 2012 Share Posted September 5, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/268021-error-corrupted-word-file-after-downloading-with-php/#findComment-1375578 Share on other sites More sharing options...
Christian F. Posted September 6, 2012 Share Posted September 6, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268021-error-corrupted-word-file-after-downloading-with-php/#findComment-1375673 Share on other sites More sharing options...
jazzman1 Posted September 6, 2012 Share Posted September 6, 2012 I don't have a problem following my post above: http://canada.lesno.net/home.php Quote Link to comment https://forums.phpfreaks.com/topic/268021-error-corrupted-word-file-after-downloading-with-php/#findComment-1375764 Share on other sites More sharing options...
xyph Posted September 6, 2012 Share Posted September 6, 2012 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"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/268021-error-corrupted-word-file-after-downloading-with-php/#findComment-1375782 Share on other sites More sharing options...
jazzman1 Posted September 6, 2012 Share Posted September 6, 2012 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'); Quote Link to comment https://forums.phpfreaks.com/topic/268021-error-corrupted-word-file-after-downloading-with-php/#findComment-1375786 Share on other sites More sharing options...
xyph Posted September 6, 2012 Share Posted September 6, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268021-error-corrupted-word-file-after-downloading-with-php/#findComment-1375789 Share on other sites More sharing options...
jazzman1 Posted September 6, 2012 Share Posted September 6, 2012 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/268021-error-corrupted-word-file-after-downloading-with-php/#findComment-1375795 Share on other sites More sharing options...
jazzman1 Posted September 6, 2012 Share Posted September 6, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268021-error-corrupted-word-file-after-downloading-with-php/#findComment-1375800 Share on other sites More sharing options...
xyph Posted September 6, 2012 Share Posted September 6, 2012 And your code simply prevents the output of the error message, rather than FIXING THE ERROR. Quote Link to comment https://forums.phpfreaks.com/topic/268021-error-corrupted-word-file-after-downloading-with-php/#findComment-1375821 Share on other sites More sharing options...
jazzman1 Posted September 7, 2012 Share Posted September 7, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/268021-error-corrupted-word-file-after-downloading-with-php/#findComment-1376092 Share on other sites More sharing options...
Christian F. Posted September 7, 2012 Share Posted September 7, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268021-error-corrupted-word-file-after-downloading-with-php/#findComment-1376195 Share on other sites More sharing options...
Stefany93 Posted September 8, 2012 Author Share Posted September 8, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/268021-error-corrupted-word-file-after-downloading-with-php/#findComment-1376269 Share on other sites More sharing options...
darkfreaks Posted September 8, 2012 Share Posted September 8, 2012 @OP: don't forget to mark topic as solved Quote Link to comment https://forums.phpfreaks.com/topic/268021-error-corrupted-word-file-after-downloading-with-php/#findComment-1376272 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.