0

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!!

Edited by giddyhank: Formatting

3
Contributors
4
Replies
5
Views
4 Years
Discussion Span
Last Post by code739
0

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

0

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

0

@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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.