Hi, I have made a script that is supposed to check if an image exists on remote server, check if exists on local server, and decide weither or not to copy the image or not (It is poster images for a movie database of my own). However this is under a mysql query and is supposed to go through the entire table, but instead it only works for the first row and I recieve php error:

Fatal error: Cannot redeclare remoteFileExists() (previously declared in C:\wamp\ww ... etc.

Here is my code, I tried 2 different methods, first using curl:

<?php
    // Download Images
    $query = mysql_query('SELECT * FROM movies');
    while ($row = mysql_fetch_array($query))    
    {   
        $imdb = $row['IMDB'];
        $imdbsubstr = substr($imdb, 26, -1);
        $url = "http://www.clearplay.com/MovieBattle/Images/". $imdbsubstr .".jpg";
        function remoteFileExists($url) 
        {
            $curl = curl_init($url);

            //don't fetch the actual page, you only want to check the connection is ok
            curl_setopt($curl, CURLOPT_NOBODY, true);

            //do request
            $result = curl_exec($curl);

            $ret = false;

            //if request did not fail
            if ($result !== false) 
            {
                //if request was ok, check response code
                $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);  

                if ($statusCode == 200) 
                {
                    $ret = true;   
                }
            }

            curl_close($curl);
            return $ret;
        }

        $exists = remoteFileExists($url);
        if ($exists && !file_exists($_SERVER["DOCUMENT_ROOT"] ."/images/". $imdbsubstr .".jpg")) 
        {
            copy($url, $_SERVER["DOCUMENT_ROOT"] . "/images/". $imdbsubstr .".jpg");
            $posterimage = "<img alt=\"Error\" src=\"images/". $imdbsubstr .".jpg\">";
        } 
        elseif (file_exists($_SERVER["DOCUMENT_ROOT"] ."/images/". $imdbsubstr .".jpg"))
        {
            $posterimage = "<img alt=\"Error\" src=\"images/". $imdbsubstr .".jpg\">"; 
        }
        else
        {
            $posterimage = "Poster Coming Soon!";
        }
    }
?>

And Here is my other method:

<?php    
    // Download Images
    $query = mysql_query('SELECT * FROM movies');
    while ($row = mysql_fetch_array($query))    
    {   
        $imdb = $row['IMDB'];
        $imdbsubstr = substr($imdb, 26, -1);
        $url = "http://www.clearplay.com/MovieBattle/Images/". $imdbsubstr .".jpg";
        function url_exists($url)
        {
            $url = str_replace("http://", "", $url);
            if (strstr($url, "/")) 
            {
                $url = explode("/", $url, 2);
                $url[1] = "/".$url[1];
            } 
            else 
            {
                $url = array($url, "/");
            }
            $fh = fsockopen($url[0], 80);
            if ($fh) 
            {
                fputs($fh,"GET ".$url[1]." HTTP/1.1\nHost:".$url[0]."\n\n");
                if (fread($fh, 22) == "HTTP/1.1 404 Not Found" && !file_exists($_SERVER["DOCUMENT_ROOT"] ."/images/".$imdbsubstr.".jpg"))
                { 
                    $posterimage = "Poster Coming Soon!";
                    return FALSE;
                }
                elseif (!file_exists($_SERVER["DOCUMENT_ROOT"] ."images/". $imdbsubstr .".jpg"))
                {
                    copy("http://www.clearplay.com/MovieBattle/Images/". $imdbsubstr .".jpg", $_SERVER["DOCUMENT_ROOT"] . "/images/". $imdbsubstr .".jpg");
                    $posterimage = "<img alt=\"Error\" src=\"images/". $imdbsubstr .".jpg\">";
                    return TRUE;
                }
                else
                {
                    $posterimage = "<img alt=\"Error\" src=\"images/". $imdbsubstr .".jpg\">";
                    return TRUE;
                }
            } else
            {
                $posterimage = "Server Error";
                    return FALSE;
            }
        }
    }
?>  

I recieve ther same error with both ways, is there a way around this?

Thanks so much for your help in advance!!

can you make this out of the while loop
function remoteFileExists($url)
{ some codes here...........}

As far as i'm aware, no. This is why I post the question. Thanks for the reply though :)

Member Avatar for diafol

@gh
You HAVE TO take the function out of the loop. It's not an option. Just place it at the very top of your php code.

yah becuase you are rediclaring the function every loop..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.