Jump to content

Help tying code together...


anon

Recommended Posts

This is a basic snippet for getting data from a web page (right?)

 

$text = file_get_contents($url);
file_put_contents($save_location, $text);

 

Could i make $url a function?

As in $url= "this will change as I go through a list of URL's in a database, then apply the file_get_contents?" (i'm a fat n00b)

 

Then obviously $save_location would be the database I want to save it to?

Link to comment
https://forums.phpfreaks.com/topic/83168-help-tying-code-together/
Share on other sites

What exactly are you trying to do here? By making $url a function, do you mean, get functions from external pages and load them/save them into the php page? Or do you just want to make a function that makes it easier for you to download pages? (i.e. having something like download($page);)

Maybe something like this:

 

<?php
//assuming you're connected to db
$getlist=mysql_query("SELECT url FROM url_table");
while($row=mysql_fetch_array($getlist)){
   $text = file_get_contents($row[0]);
   file_put_contents($save_location, $row[0]);
}
?>

<?php

$dbhost = 'localhost';

$dbuser = 'root';

$dbpass = 'password';

 

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

 

$dbname = 'url';

mysql_select_db($dbname);

//would i still need to connect to table?

 

$getlist=mysql_query("SELECT url FROM url_table");

 

while($row=mysql_fetch_array($getlist)){

  $text = file_get_contents($row[0]);

  file_put_contents($save_location, $row[0]);

}

?>

 

Would this work

This should work for you.  I don't know how you go about naming the file you store the data in so if you need help with that just explain how you want it to work.

 

<?php
define ('DB_USER', 'username');
define ('DB_PASSWORD', 'password');
define ('DB_HOST', 'host');
define ('DB_NAME', 'database name');

$dbc=@mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect: ' . mysql_error());
@mysql_select_db(DB_NAME) OR die ('Could not select database: ' . mysql_error());

$getlist=mysql_query("SELECT url FROM url_table");
while($row=mysql_fetch_array($getlist)){
   $save_location="filename.txt";  //change this to what you need it to be
   $text = file_get_contents($row[0]);
   file_put_contents($save_location, $text);
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.