File upload of .DOC... Interesting


Well, heres one for you all… Need to upload a file, and reprint it into a textbox. Ok thats no problem at all. The real issue at hand is when uploading .DOC’s… it seems to do it in BINARY format… any ideas on converting or what to do?


	if ($_POST['submit_upload_file']) {
	$Temporary_File = $_FILES['upload_file']['tmp_name'];
	$File_Name = $_FILES['upload_file']['name'];
	move_uploaded_file($Temporary_File, "temp/$File_Name");
	$Read_File = file_get_contents("temp/$File_Name");
	$_POST['event_description'] = $Read_File;



My guess is that you’re trying to send a Microsoft Word or Wordpad document. It is a binary format.


so pretty much im screwed. lol I guess im going to have to tell my client to use a different editor.


There’s no reason PHP can’t upload and download a .doc file. The link points out many reasons why you should use something else, but it has absolutely nothing to do with PHP.

My guess is that file_get_contents() is your mistake – use an fread() loop instead, and use the ‘b’ option when you fopen() the file. That gets at the data at a slightly lower, rawer level.


reading and opening the file is no problem at all, its how it translates it. Its in binary format rather then ASCII and thus shows up with a bunch of symbols and stuff…

thanks for your help though :)


Look at your php.ini-type settings next; there may be something there that is affecting the file translation. Also make sure you have the correct flags for fopen() – there is a binary flag – and make sure the html form that’s being submitted with the document has the right content-type.


alright. Lets see what we can do… how exactly do you read the file into a string using fopen… i am a little unfamiliar with it.

I do not have access to my server right now but here is my try where im at right now. let me know if their is something that should be changed… Thanks :slight_smile:
if ($_POST[‘submit_upload_file’]) {
$Temporary_File = $_FILES[‘upload_file’][‘tmp_name’];
$File_Name = $_FILES[‘upload_file’][‘name’];
move_uploaded_file($Temporary_File, “temp/$File_Name”);
$File_Open = @fopen(“temp/$File_Name”, “b”) or DIE (“Error Opening File”);
$File_Read = @fread(“temp/$File_Name”) or DIE (“Error reading the file”);
$File_Close = @fclose(“temp/$File_Name”) or DIE (“Error Closing File”);
$File_Delete = unlink(“temp/$File_Name”) or DIE (“Error Deleteing The File”);
$_POST[‘event_description’] = $File_Read;


with that, Should $_POST[‘event_description’] now contain the contents to the file?


You can get the contents of the file however you want. However, you will probably run into problems putting this raw data into a textbox.

If the text of the document is important, it will need to be saved as some text format (MS-DOS text or rtf). If the document itself is the important part, you’ll probably have to store it in a database as a binary format. To retrieve the document, you’ll have to send the correct headers then stream the file.


Aye, I didn’ t even think about all the “extra” stuff it will put in their for alignment and such… I will need to find out more information