Jump to content

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);
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.