How can I write file into a folder?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
oztech
Forum Newbie
Posts: 11
Joined: Thu Dec 04, 2008 6:58 pm

How can I write file into a folder?

Post 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!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How can I write file into a folder?

Post 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
(#10850)
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: How can I write file into a folder?

Post 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);
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How can I write file into a folder?

Post 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.
(#10850)
Post Reply