...

View Full Version : Help with PHP needed (file reading, manipulation)



masky666
05-24-2011, 09:53 PM
Hi. I have text files in the directory that contain rapidshare.com links. Now what I need to do is take them one by one and check links inside them one by one using rapidshare API.

Everything works fine apart from one thing.

It reads files and deletes the ones containing dead links but the problem is that it only deletes the file if ALL links are dead, but not when only one link is dead or some of them.


<?php
function rs_check($url){
if (!$url) return false;
$files_pattern= '/\/files\/([^\/]*)\//';
$filename_pattern= '/\/files\/.*\/(.*)/';
preg_match($files_pattern, $url, $matches_id);
preg_match($filename_pattern, $url, $matches_name);
$res= file_get_contents("http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=checkfiles_v1&files={$matches_id[1]}&filenames={$matches_name[1]}");
list($fId,$fName,$fSize,$fServerId,$fStatus,$fShortHost,$fmd5) =explode(',',$res);
return ($fStatus==1 || $fStatus==2);
}
$directory = "download/";
$linksfiles = glob("" . $directory . "*.txt");
foreach($linksfiles as $linkfile)
{
$lf = file_get_contents($linkfile);
$purefilename = str_replace($directory, "" , $linkfile);
if($purefilename != "5100c00fdb545e12e8cb4f9c5f69ec21.txt"){
if(rs_check($lf) != 1){
unlink($directory . $purefilename);
echo("FILE DELETED <br />");
}
}
}
?>

gvre
05-24-2011, 10:24 PM
Try this

<?php
function rs_check($url)
{
if (!$url)
return false;
$files_pattern= '/\/files\/([^\/]*)\//';
$filename_pattern= '/\/files\/.*\/(.*)/';
preg_match($files_pattern, $url, $matches_id);
preg_match($filename_pattern, $url, $matches_name);
$res = file_get_contents("http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=checkfiles_v1&files={$matches_id[1]}&filenames={$matches_name[1]}");
list($fId,$fName,$fSize,$fServerId,$fStatus,$fShortHost,$fmd5) = explode(',',$res);
return $fStatus == 1 || $fStatus == 2;
}

$directory = "download/";
$linksfiles = glob("" . $directory . "*.txt");
foreach($linksfiles as $linkfile)
{
$lf = file($linkfile);
$purefilename = str_replace($directory, "" , $linkfile);
if ($purefilename != "5100c00fdb545e12e8cb4f9c5f69ec21.txt")
{
foreach($lf as $line)
{
if (rs_check($line) === false)
{
unlink($directory . $purefilename);
echo("FILE DELETED <br />");
break;
}
}
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum