Welcome to WebmasterWorld Guest from 18.206.194.210

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP copy and paste

Is it possible

     
4:43 pm on Jul 16, 2005 (gmt 0)

New User

10+ Year Member

joined:July 12, 2005
posts:13
votes: 0


Hi,

I have a normal html file "index.html" and a folder called "Old" in the same folder as index.html.

I am trying to create a php file which will copy the contents of index.html when the php file is run and paste the contents into a new file it creates in the "old" folder (along with the date and time in the title)

The problem is, The index.html has an php run rss feed in it with server side php processing (through htaccess), so what I need the php file to do is to copy the output that a user would see, not the php in index.html! if that makes sense :)

Any ideas?

4:57 pm on July 16, 2005 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 24, 2005
posts:697
votes: 0


Thats Easy!

ob_start();
include "http://your-web-site/index.html";
$data=ob_get_contents();
ob_clean();

This code will execute any and every php code that is in your index.html as a normal user would see and then it would return the compiler page content in the variable $data;

Just use Filestream and create a file in "Old" Directory and put $data in your file.

Simple

5:04 pm on July 16, 2005 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 24, 2005
posts:697
votes: 0


put this below the code i pasted above.

once that code gets you content of that page in variable $data

the following code will put in any file as you define, provided that file has write access.
(I would not recommend to play with File Permissions unless you know what you are doing)

<?php

$filename = 'Old/test.html';

if (is_writable($filename)) {

if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}

if (fwrite($handle, $data) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}

echo "Success";

fclose($handle);

} else {
echo "The file $filename is not writable";
}

?>

5:52 pm on July 16, 2005 (gmt 0)

New User

10+ Year Member

joined:July 12, 2005
posts:13
votes: 0


WOW, is there anything PHP can't do! :)

OK, one last problem, how would I create the file in the old directory (test.html above) using the php code, and how to name it the date/time?

6:07 pm on July 16, 2005 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 24, 2005
posts:697
votes: 0


Hey! That File write code is boring enough, isnt it.?

what about this? ;)

ob_start();
include "http://your-web-site/index.html";
$data=ob_get_contents();
ob_clean();

$timestamp=date('H-i-m-d-Y');
$filename = "old/$timestamp.html";
$fp = fopen($filename, "w");
fwrite($fp, $data);
fclose($fp);

//after the execution of last line content grabbed from that url would have been copied and stored into your file curent hour-minute-month-day-year.html
for example

old/12-33-6-6-2005.html

Thats it ! cheers

6:20 pm on July 16, 2005 (gmt 0)

New User

10+ Year Member

joined:July 12, 2005
posts:13
votes: 0


9 lines of code! jeez dude...thats l337 ;)

I had it at 32...

Last one, i promise...is it possible to list (echo) all the files in the "old" folder (without the .html bit)using just php?

Cheers!

6:33 pm on July 16, 2005 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 24, 2005
posts:697
votes: 0


Yes

leme check my code for this on my server. give few mins

You need not to ask the Last Question, Ask as many as needed to get your work done.

7:12 pm on July 16, 2005 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 24, 2005
posts:697
votes: 0


Yo!

I couldnt find my code quickly so i picked up code from the following url for you.

[php.net...]

Just put in in a php file and give it path of your directory, it will print our list

( I didnt test it ) Please check the above url for some more codes for this too

<?php $myfiles=GetDirContents(dirname($_SERVER['DOCUMENT_ROOT']));
print_r ($myfiles);
?>

<?php

function GetDirContents($dir){
ini_set("max_execution_time",10);
if (!is_dir($dir)){die ("Fehler in Funktion GetDirContents: kein g?s Verzeichnis: $dir!");}
if ($root=@opendir($dir)){
while ($file=readdir($root)){
if($file=="." ¦¦ $file==".."){continue;}
if(is_dir($dir."/".$file)){
$files=array_merge($files,GetDirContents($dir."/".$file));
}else{
$files[]=$dir."/".$file;
}
}
}
return $files;
}

?>

Have Fun

7:59 pm on July 16, 2005 (gmt 0)

New User

10+ Year Member

joined:July 12, 2005
posts:13
votes: 0


Hmmm, can't get it working (error). Plus it seems like its full of filler and comments. I need the quickest code possible...;)

Could you please try to find the better code on your server please...

Ta

8:26 pm on July 16, 2005 (gmt 0)

New User

10+ Year Member

joined:July 12, 2005
posts:13
votes: 0


OK, done it!

Thanks A

11:22 pm on July 16, 2005 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 24, 2005
posts:697
votes: 0


Hey

Sorry i was out for a few hours , couldnt read your message. its cool you have done it!

Cheeers