View Full Version : Saving a webpage to the webserver

11-05-2007, 10:30 PM
Ok i have a page thats generated after a form is submitted. What code can i use to save a copy of that page to a folder on my webserver?

11-06-2007, 12:54 AM
$contents = file_get_contents("http://...");
$file_to_save = "text.txt";

$file_opened = fopen($file_to_save, 'a+');
fputs($fi_opened, $contents);

11-06-2007, 02:15 AM
Ok it saved a file named text.txt but there is no info in the document. Also the page generated this error message:

Warning: file_get_contents(http://www.domain.com/engr/servlet/scripts/printouts/newuser/printout.php?user=testuser2&password=password&num1=53359&num2=34009&num3=90024&num4=19092&num5=84117) [function.file-get-contents]: failed to open stream: HTTP request failed! in /home/ootaclan/public_html/engr/servlet/scripts/printouts/newuser/printout.php on line 79

11-06-2007, 02:26 AM
It requires some kind of trigger to be set on your localhost when page is submited. If that was on the same server it wouldn't be problem (i guess), but sending HTTP request to far away server and dynamicly uploading it is possible only with Ajax, or some super cool cgi or similar script which i would really like to see :) if anyone is following me, please answer what could it be possible to use instead of lousy javascript :D

11-06-2007, 03:12 AM
You could always

$content = "";
$content .="<html>";
$content .="<head>";

And then use fopen and write that output to a file and then print that output to the screen. You could also use ob_start and ob_get_flush.

11-06-2007, 04:52 AM

// put your page here

$content = ob_get_clean();

$return = file_put_contents('text.txt', $content); // or change to file_put_contents('text.txt', $content, FILE_APPEND); if you want to append the file

if($return == false)
die('Failed to write file!');

11-06-2007, 06:00 AM
ok it's writing the file perfectly, however the page no longer loads. it's just a blank white screen.

11-06-2007, 01:17 PM
print $return;

11-06-2007, 04:36 PM
No, print $content; or change ob_get_clean() to ob_get_flush().

11-06-2007, 05:21 PM
Oops :)

I had just woken up and picked the first variable I saw :)

11-06-2007, 05:30 PM
Ok i have it saving and loading properly. I tried to change the name of the file it saves but couldn't get it to work. I'm trying to set it equal to a variable.

Here's what i tried:

$file_to_save = "../../../archives/docs/account_details/$uic.txt";

also tried:

$file_to_save = "../../../archives/docs/account_details/" . $uic . ".txt";

both yield no file saved.

11-06-2007, 06:17 PM
it will not save in file with file_put_contents if file doesn't egzist.
you need to use fopen with 'a+', then file will be created it doesn't egzist.

this is not correct, like aerdin wrote in post below. sorry and thanks.

11-06-2007, 07:11 PM
it will not save in file with file_put_contents if file doesn't egzist.

This is wrong. The whole purpose of file_put_contents() is to have an easy method of writing to a text file without the worry of modes/existing files/etc.

Are you displaying errors? Are you sure the $uic variable contains a value?

11-06-2007, 11:29 PM
no errors are getting displayed. the variable $uic is declared

11-07-2007, 01:03 AM
yep just double checked by doing an echo $uic; it shows up with the correct info. just the file ain't saving anymore

just wondering if this could be it: Is there a max length for the name of a saved file. Because the variable $uic stands for user identification code which is a 25 digit number.

Apparantly not because i just tried it with a shorter variable length and still got no file saved

11-07-2007, 01:10 AM
i think that fat32 systems have limited file name lenght. but i'm wrong most of the day so don't get to serious

11-07-2007, 01:11 AM
great! just great.

11-07-2007, 01:34 AM
well considering it didn't work even when i shortened it i'd say thats not the problem.

it seems like its not reading the variable

11-08-2007, 12:33 AM
does anybody see anything that i'm doing wrong?

11-08-2007, 05:11 AM
Post the code you're using.

11-08-2007, 10:11 PM
$file_to_save = "../../../archives/docs/account_details/" . $uic . ".txt";

it saves the file if i put a name instead of a variable

like this:

$file_to_save = "../../../archives/docs/account_details/text.txt";

11-08-2007, 11:00 PM

11-08-2007, 11:49 PM
where should i put that at?

11-09-2007, 04:27 AM
ok i just did an echo $file_to_save; and here's what i got:


it should have been:


its making it a decimal and adding that E+24 to the end any ideas on how to prevent this?