PDA

View Full Version : I Need a Batch Import Script


Useless
2005-09-02, 02:04 PM
As the title states, I'm looking for a batch import script (hopefully PHP) that I could modify and super glue into my link list script. I want to be able to add pipe delimited text into my database, as such:
Site 1 Title | URL | description | other shit
Site 2 Title | URL | description | other shit
Site 3 Title | URL | description | other shit
Site 4 Title | URL | description | other shit

I just checked my budget and I see that the script must cost somewhere between $00.00 and $3.95. :D

Halfdeck
2005-09-02, 03:01 PM
This is the code I use for batch importing. It takes a .txt file, parses it and inserts info into my MYSQL DB.

It needs to be rewritten (I just ripped it off one of my scripts), it'll create undeletable files on your server depending on server setting, its a bit clunky 'cause it doesn't use regexp... and you need an HTML with a form that takes files with BROWSE, but I'm about to hit the sack .. so I'll just post it.

$batchlinksdir = "mysql/batchfiles"; // directory where the .txt file's uploaded.

upload_file("batchlinks", $batchlinksdir);
$batchfilename = $_FILES['batchlinks']['name'];

$file = fopen("../batch/$batchfilename", "r");
while($fileline = fscanf($file, "%[a-zA-Z0-9@&=;_:./ '!?-]\n")) {

list($myline) = $fileline;

$myline = substr($myline, strpos($myline, "http:"), strlen($myline));

list($url) = sscanf($myline, "http://%[a-zA-Z0-9@&=_,:./!?-][ |\n]");

if($url=='') echo "EMPTY STRING<BR>";
else {
$url = "http://" .$url;
echo "PARSED URL: " .$url ."<BR>";
/*_ First, check if the url already's in the database __*/

$query = "SELECT * FROM hosted_gallery WHERE url=\"$url\"";
$result = mysql_query($query) or mysql_error();
if(mysql_num_rows($result)>0) echo "URL: [$url]::DUPLICATE ENTRY<BR>";
else {

$query = "INSERT INTO hosted_gallery (url,site_id,description,title, url_type_id, static,content_type,niche_id,disable, thumb, date_added,dvd_id) VALUES ('$url','$site_id',\"$description\",\"$title\", '$url_type_id', '$static','$content_type','$niche_id', '$disable', '$thumbname', '$date_added', '$dvd_id')";
$result = mysql_query($query) or error_msg($query ." modify-gallery.php::batch<BR>");
}
}
}
fclose($file);
exit;
}

rich06
2005-09-03, 10:11 AM
Here's around er... $2.15 worth of PHP code that should get you started...


<?
$sites = file('/path/to/my/text/file.txt');

if (is_array($sites)) {
foreach ($sites as $site) {
$bits = explode('|',$site);
$sql = 'insert into mytable (title,url,description,other_shit) ';
if (is_array($bits)) {
mysql_query($sql."('{$bits[0]}','{$bits[1]}','{$bits[2]}','{$bits[3]}')") or die('whatever');
}
}
}
?>


hth
rich

Useless
2005-09-03, 01:05 PM
I knew I should of up'd my budget to the full 5 bucks. :D

Thanks guys.