Page 1 of 1

get image according to date function.

Posted: Fri Aug 07, 2009 11:58 am
by mp04
Hey,
I have a script that was custom made for me that, grabs a image from another site (my own other site) the script is:

Code: Select all

 
<?
 
 
    $filename="URL To other site";
    $indirectory="Directory where the image is";
    $destinationpath="Path to where it should save it.";
 
 
 
    $contents = file_get_contents($filename);
    $url=dirname($filename);
    $keywords=0;
    $keywords1=0;
 
    $keywords = strpos($contents,"src");
    while ($keywords>0)
    {
        $filename=retbetween1($contents,"src=\"","\"",$keywords1);
 
        if (substr($filename,-4)==".jpg")
        {
            if (strpos($filename,$indirectory)>0)
            {
                echo "Writing file ";
                if (substr($filename,0,7)=="http://")
                {
                    $bname=basename($filename);
                    $contents1 = file_get_contents($filename);
                    echo $filename." to ";
                    file_put_contents($destinationpath,$contents1);
                    echo "$destinationpath. Success";
                }
                else
                {
                    $bname=basename($filename);
                    $contents1 = file_get_contents($url."/".$filename);
                    echo $url."/".$filename." to ";
                    file_put_contents($destinationpath,$contents1);
                    echo "$destinationpath. Success";
                }
            }
        }
    }
 
 
function retbetween($mainstring,$search,$search1,$cnt)
{
    global $sttable;
    global $keywords;
    global $keywords1;
 
    $keywords = strpos($mainstring,$search,$cnt);
 
    $keywords1 = strpos($mainstring,$search1,$keywords+strlen($search));
    $sl=strlen($search);
    $s2=strlen($search1);
    $strw=substr($mainstring,$keywords,($keywords1+$s2)-($keywords));
    $sttable=$keywords1+$s2;
    return $strw;
 
}
function retbetween1($mainstring,$search,$search1,$cnt)
{
    global $sttable;
    global $keywords;
    global $keywords1;
    $keywords = strpos($mainstring,$search,$cnt);
    if ($keywords>0)
    {
        $keywords1 = strpos($mainstring,$search1,$keywords+strlen($search));
        $sl=strlen($search);
        $s2=strlen($search1);
        $strw=substr($mainstring,$keywords+strlen($search),($keywords1)-($keywords+strlen($search)));
        $sttable=$keywords1+$s2;
        return $strw;
    }
    else
        return "";
 
}
 
?>
</form>
</body>
</html>
 

Now this has worked perfectly so far, since site would only show one image a day, and server would go there, and pick up that one image from that one folder.

Now, We have made some changes to the daily image feature on the web, where the user can go back a few days or months etc, BUT on the site it self it shows 5 pictures from the last five days now. SO now the server is confused as far as which ONE image it should grab. causeing the script to now stall.

So basically what we did I talked with the admin to the page and he said what he can do is name each image with a date, so today image would be: 08-07-09.jpg.

what I want to do, is maybe a addition to that script above so it can get the date from the server, and match it with the image and THEN transfer it.

any ideas? Please remember I am a newbie to PHP and still learning, so please keep that in mind.

Thanks.

Re: get image according to date function.

Posted: Fri Aug 07, 2009 9:22 pm
by aceconcepts
Well, if the image filename is now a date what you could do is explode() the filename and compare that string with a date.

e.g.

Code: Select all

 
//get date from filename
$exp = explode(".", $filename);
$date_from_filename = $exp[0];
 
Hope this makes sense.