SitePoint Sponsor

User Tag List

Results 1 to 22 of 22
  1. #1
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I've been looking through a number of PHP books, and haven't come across this subject. I want to know if a function exists in PHP that will GET the contents of a page and save it the server.

    For example in Cold Fusion it's done like so:

    In this CF tag, Cold Fusion goes to the page that I've specified. The entire page is saves as the variable CFHTTP.FileContent.

    <CFHTTP METHOD="get" URL="http://www.onlinehiphopawards.com/sohh/templates/thecore/create_inc_right_sidebar_op.cfm"></CFHTTP>

    In the following CF tag, it takes the variable it was stored as, and saves it as the attribute in "FILE".

    <CFFILE ACTION="write" FILE="D:\online_hip_hop_awards\sohh\templates\opening_page\sohh_style.html" OUTPUT="#CFHTTP.FileContent#">

    If anyone has any ideas on how to accomplish this, let me know. I'd be interested to see how it's done.


    <///////~

    www.sohh.com

  2. #2
    psycho
    Join Date
    May 2000
    Location
    London
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    check out http://www.php.net/fopen

    and

    http://www.php.net/fputs

    for example, to write to a file :

    //OPENS A FILE. IF THE FILE DOESN'T EXIST, PHP CREATES IT
    $file = fopen($filename, "w");

    //INSERTS DATA INTO FILE
    fputs($file,$contentoffile);

    //CLOSES CONNECTION TO FILE
    fclose($file);

    J

  3. #3
    SitePoint Evangelist
    Join Date
    May 2000
    Location
    Canada
    Posts
    533
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, you would want to fopen() .. it accepts urls as a file location ... loop an fgets() and store all of the fopen into a variable... fclose() .... fopen() a local file... fwrite() fclose() the local file

    flat files are horrible at speed compared to mySQL, and are generally not supported very well, thats why you dont see too much information on them

    NOTE when writing the file to the hard drive, permissions MUST be valid... i dont like letting directories be chmod 777, so on my servers i have dual installs php, one being a cgi, secured by suexec, where the script runs as php, under the userid you are, letting you write files in your own filespace ... otherwise if the server runs php as a module, it will run under the web process, aka apache ...

    -----------------------------
    myPHPhost.com: we've thought of everything
    Kryogen Technologies: new year sale

  4. #4
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm pretty new to PHP, so some of the stuff that you put makes sense but I'm lost on other areas. Can you rewrite your example the with my variables?

    Also, Vinay. It's true, flat files are slower than MYSQL but I'm not using it for that purpose. My purpose is to write certain sections of my site to plain HTML files. For example, this link

    http://www.onlinehiphopawards.com/so...sidebar_op.cfm

    will generate my right sidebar with information from my SQL server. Using CF I am able to save this file, on my hard drive as

    http://www.online_hip_hop_awards\soh...ohh_style.html

    This way, instead of the webserver have to hit the database server to generate that one piece of content, that only changes once a week, the webserver will just this the webserver for an HTML file.
    <///////~

    www.sohh.com

  5. #5
    SitePoint Evangelist
    Join Date
    May 2000
    Location
    Canada
    Posts
    533
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    aah, fare enough

    here's code for that in php

    Code:
    <?
    
    $fp = fopen("http://www.onlinehiphopawards.com/sohh/templates/thecore/create_inc_right_sidebar_op.cfm","r") OR DIE("cant open file");
    
    while(!feof($fp))
    {
    	$tempstring .= fgets($fp,4096);
    }
    
    fclose($fp);
    
    $fp = fopen("some_file.html","w");
    fwrite($fp,$tempstring);
    
    fclose($fp);
    
    echo "all done, simple and sweet..";
    
    ?>
    message updated, updated code in red.. my mistake

    Disclaimer: there are more checks i could have added , but hey, its a script on a message board, i'll go nuts when i'm really coding
    <Edited by Vinay on 01-09-2001 at 01:09 AM>
    cogito, ergo sum

  6. #6
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I ran the script is this is what I got

    Warning: 1 is not a valid File-Handle resource in /usr/local/www/docs/sohh.com/pub2000/login.php on line 13

    Warning: 1 is not a valid File-Handle resource in /usr/local/www/docs/sohh.com/pub2000/login.php on line 15
    <///////~

    www.sohh.com

  7. #7
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I attempted to try something:

    <?php
    // set the name of the filename to get
    $getfile = "http://www.onlinehiphopawards.com/sohh/templates/thecore/create_inc_right_sidebar_op.cfm";

    // set the name of the filename to save
    $savefile = "some_file.html";

    // get the file and save the contents to getfilecontents
    $getfilecontents = readfile($getfile, "r") OR die("cant open file");

    // show the results of getfilecontents
    echo $getfilecontents;

    // save the results of getfilecontents to the filename specified in the savefile variable
    fwrite($savefile, $getfilecontents);

    // close the file
    fclose($savefile);

    echo "Done!";

    exit();
    ?>

    Sad thing it shows me the results of getfilecontents and after that it doesn't work. But a file does get saved to my server. So it's just not saving all of the content to the file because the file is at 0 bytes.
    <///////~

    www.sohh.com

  8. #8
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    because you need to open the file for writing before you ca nwrite anything to it.
    Like vinay did
    fopen("some_file.html","w");
    You might need to put the full system path to this file so it will open in it a folder that is writable by the web server.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  9. #9
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, Freddie, so I added the fopen() to my code:

    <?php

    $getfile = "http://www.onlinehiphopawards.com/sohh/templates/thecore/create_inc_right_sidebar_op.cfm";

    $savefile = "/usr/local/www/docs/sohh.com/pub2000/some_file.html";

    $getfilecontents = readfile($getfile, "r") OR die("cant open file");

    echo $getfilecontents;

    fopen($savefile,"w");

    fwrite($savefile, $getfilecontents);

    fclose($savefile);

    echo "Done!";

    exit();
    ?>

    But still getting errors.
    <///////~

    www.sohh.com

  10. #10
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What error?

    Also this part is wrong

    fopen($savefile,"w");

    fwrite($savefile, $getfilecontents);

    fclose($savefile);

    Should be

    $fp = fopen($savefile,"w");

    fwrite($fp, $getfilecontents);

    fclose($fp);
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  11. #11
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Freddy,

    That worked well. But it didn't save the actual $getfilecontents. It saved how many characters were in the file.

    http://www.sohh.com/pub2000/some_file.html

    Anyway to make it save the actual content?

    Steven
    <///////~

    www.sohh.com

  12. #12
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, Freddie, so I added the fopen() to my code:

    <?php

    $getfile = "http://www.onlinehiphopawards.com/sohh/templates/thecore/create_inc_right_sidebar_op.cfm";

    $savefile = "/usr/local/www/docs/sohh.com/pub2000/some_file.html";

    $getfilecontents = file($getfile) OR die("cant open file");

    echo $getfilecontents;

    fopen($savefile,"w");
    for($i=0;$i<count($getfilecontents);$i++) {
    fwrite($savefile, $getfilecontents[$i]."\n");
    }
    fclose($savefile);

    echo "Done!";

    exit();
    ?>



    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  13. #13
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nah, that didn't do it.

    I got the following:
    Array
    Warning: Supplied argument is not a valid File-Handle resource in /usr/local/www/docs/sohh.com/pub2000/login.php on line 14 (50+ times)

    Warning: Supplied argument is not a valid File-Handle resource in /usr/local/www/docs/sohh.com/pub2000/login.php on line 17
    Done!

    The script is at:
    http://www.sohh.com/pub2000/login.php in case you want to see it.
    <///////~

    www.sohh.com

  14. #14
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well it seems that you don't have permission to write to some_file.html
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  15. #15
    SitePoint Evangelist
    Join Date
    May 2000
    Location
    Canada
    Posts
    533
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and that is the case mentioned in the note on my first post ... php runs as the webserver, and you have permissions of the webserver, which is NO write capability in user directories... i've encountered this in the past and use a work around which works very well
    cogito, ergo sum

  16. #16
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ibeblunt, if I may be blunt, why not just open both files with a text editor and then copy and paste the stuff you want? or if its dynamic content open the pag in a browser view the source then copy the source to the file you want.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  17. #17
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have the pub2000 set to chmod 777 and the file set to chmod 777 and still no go.

    I don't have time to cut and paste code. I'm the only tech guy on staff. The way my Cold Fusion site works, it's less work 'cause it pushes content to the site based on time, and also automatically writes the HTML files everyday, week or month - depending on the setting of the file.

    Dayum, I didn't realize that PHP probably doesn't have a scheduling manager. I'll have to figure something out... Cold Fusion has a scheduling manager that will run .cfm files at different times of the day.

    Steven
    <///////~

    www.sohh.com

  18. #18
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well if you would move to a unix platform you could use cron to do the same thing. Basically you set up cron scripts to run at any given time. As Vinay said the user your web server runs as probaly doesn't have the permissions to write to files, this is why while PHP does run on NT it doesn't run that well. Its great for testing scripts but unix is where its at!
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  19. #19
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm...I'm on Linux. I'm moving away from NT.

    Are there any settings in PHP that I should be looking for? And Vinay, I noticed that you rewrote that PHP script 'cause you forgot something. Should I try it again?


  20. #20
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Duh I just noticed the unix file path above, I must be on another planet. Sorry about that. pub2000 is the user the web server runs under? Seems like a weird problem.

    Okay so one more time this should do the trick I am pretty sure

    <?php

    $getfile = "http://www.onlinehiphopawards.com/sohh/templates/thecore/create_inc_right_sidebar_op.cfm";

    $savefile = "/usr/local/www/docs/sohh.com/pub2000/some_file.html";

    $getfilecontents = file($getfile) OR die("cant open file");



    $fp = fopen($savefile,"w");
    for($i=0;$i<count($getfilecontents);$i++) {
    fwrite($fp, $getfilecontents[$i]."\n");
    }
    fclose($fp);

    echo "Done!";

    exit();
    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  21. #21
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whoa!

    It worked!

    http://www.sohh.com/pub2000/some_file.html

    FYI - If you're looking at the page, the images won't load because the paths are incorrect.

    But it works. Less work for me.
    <///////~

    www.sohh.com

  22. #22
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It seems you had some of the parts sometimes and other s other times but never all the correct parts together.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •