Get Contents With 404

Discussion in 'PHP' started by bloard, Jul 18, 2007.

  1. #1
    I am running a script that loops through various URL's and does a $content=file_get_contents($url);

    How do I do an "If URL Not Found (404) Then Do Something, Else Run Rest Of Loop"

    Right now when there is a 404 error, the script stops and outputs a php Warning saying that the file_get_contents returned a 404. A 404 error is ok for this script... I want to move on to the next URL without stopping the script.

    Thanks
     
    bloard, Jul 18, 2007 IP
  2. Bartuc

    Bartuc Active Member

    Messages:
    120
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #2
    you can try;
    $content=@file_get_contents($url);

    @ prevents function to print errors on the page.
     
    Bartuc, Jul 18, 2007 IP
  3. mattjfrank

    mattjfrank Peon

    Messages:
    48
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Won't if(!$content) work?
     
    mattjfrank, Jul 18, 2007 IP
  4. mikemotorcade

    mikemotorcade Peon

    Messages:
    645
    Likes Received:
    49
    Best Answers:
    0
    Trophy Points:
    0
    #4
    an exclamation point just means if something is not true. Basically

    if($content)
    PHP:
    is the same as

    if($content == true)
    PHP:
    and

    if(!$content)
    PHP:
    is the same as

    if($content != true)
    PHP:
    and also

    if($content == false)
    PHP:
    (the operator "!=" means "not equal to")
     
    mikemotorcade, Jul 20, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    
    if (!$content = @file_get_contents($url))
    {
        continue;
    }
    
    PHP:
     
    nico_swd, Jul 21, 2007 IP