Page 1 of 1

How can I write file into a folder?

Posted: Tue Jul 21, 2009 3:46 pm
by oztech
Hi,

What I want is this. Read a word document and write the document as a new file into a folder.

$fp = fopen($filename, 'r');
$output = fread($fp, filesize($filename));

//and then write file into a folder as a new file

How can I do this??

Thanks a lot!

Re: How can I write file into a folder?

Posted: Tue Jul 21, 2009 3:58 pm
by Christopher
Have you looked in the manual at the filesystem functions? There are several different ways to copy a file.

http://www.php.net/manual/en/book.filesystem.php

Re: How can I write file into a folder?

Posted: Tue Jul 21, 2009 4:02 pm
by Darhazer
Is there a reason not to use copy?

OK, actually it's pretty simple:

Code: Select all

$in = fopen('filename', 'r');
$out = fopen('filename2', 'w+');
// TODO: check if fopens() were successful
while (!feof($in)) {
$data = fread($in, 1024)  
fwrite($out, $data);
} 
fclose($in);
fclose($data);

Re: How can I write file into a folder?

Posted: Tue Jul 21, 2009 4:06 pm
by Christopher
Or

Code: Select all

$data= file_get_contents('filename');
file_put_contents('filename2', $data);
I am sure there are several other ways to do it.