...

View Full Version : Saving a webpage to the webserver



Doom87
11-05-2007, 09: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?

matak
11-05-2007, 11:54 PM
$contents = file_get_contents("http://...");
$file_to_save = "text.txt";

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

Doom87
11-06-2007, 01: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

matak
11-06-2007, 01: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

toddandrae
11-06-2007, 02: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.

Inigoesdr
11-06-2007, 03:52 AM
<?php
ob_start();

// 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!');

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

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

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

toddandrae
11-06-2007, 04:21 PM
Oops :)

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

Doom87
11-06-2007, 04: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.

matak
11-06-2007, 05: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.

aedrin
11-06-2007, 06: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?

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

Doom87
11-07-2007, 12: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

matak
11-07-2007, 12: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

matak
11-07-2007, 12:11 AM
great! just great.

Doom87
11-07-2007, 12: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

Doom87
11-07-2007, 11:33 PM
does anybody see anything that i'm doing wrong?

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

Doom87
11-08-2007, 09: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";

Inigoesdr
11-08-2007, 10:00 PM
var_dump($uic);

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

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

../../../archives/docs/account_details/9.53468745698E+24.txt

it should have been:

../../../archives/docs/account_details/9534687456982314621578632.txt

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum