0

I want to edit a file which extension is .doc This file contains some keywords such as:

<<Customer>>
<<DateStart>>
ecc.

Now I want to read the content of the file, edit it and then put it in new word.

I try in this way:

header ("Content-Type: application/msword; ");
header ("Content-Disposition: attachment; filename=new.doc");

$filename='a.doc';
$key = "«Customer»";
$fc=file($filename);

$f=fopen("'C:\Users\Ciro\Desktop\new.doc","w");
foreach($fc as $line){
  if (strpos($line,$key))
    $line=str_replace($key,"some new text",$line);
  file_put_contents($f,$line); 
}
fclose($f);
fclose($fc);

How can I fix it?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
CiroRa
  • 492
  • 3
  • 13

1 Answers1

0

The following line was wrong, it had a misplaced '

f=fopen("C:\Users\Ciro\Desktop\new.doc","w");

Also, DOC files aren't simple TXT files because its content is encoded, if possible, convert it to a TXT file.

This is how a DOC file looks like to PHP

enter image description here

Basically, PHP cannot see what you see and you won't be able to achieve what you want, unless you convert the DOC file to a RAW format, like TXT.

UPDATE:

Try catdoc, which converts any .doc file into plain text. See the catdoc homepage

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268