Forum Moderators: coopster

Message Too Old, No Replies

PHP copy and paste

Is it possible

         

Lionor

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

10+ Year Member



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?

Anyango

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

WebmasterWorld Senior Member 10+ Year Member



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

Anyango

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

WebmasterWorld Senior Member 10+ Year Member



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";
}

?>

Lionor

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

10+ Year Member



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?

Anyango

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

WebmasterWorld Senior Member 10+ Year Member



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

Lionor

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

10+ Year Member



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!

Anyango

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

WebmasterWorld Senior Member 10+ Year Member



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.

Anyango

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

WebmasterWorld Senior Member 10+ Year Member



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

Lionor

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

10+ Year Member



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

Lionor

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

10+ Year Member



OK, done it!

Thanks A

Anyango

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

WebmasterWorld Senior Member 10+ Year Member



Hey

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

Cheeers