-2

I'm writing a code to upload a file in PHP. But there is an unknown and strange problem in it's IF statement. It does operations in both true and false condition! Look at the code below please:

if (is_uploaded_file($_FILES['catalogue']['tmp_name']))
{
    $ext = find_extension('catalogue');
    $ext_array = array('pdf');

    if (!in_array($ext,$ext_array))
    {
        // echo something for error message.
    }
    else
    {
        echo ' Step1 ';
        @unlink ('../../catalogues/'.$id.'.pdf');
        if(@move_uploaded_file($_FILES['catalogue']['tmp_name'],"../../catalogues/".$id.'.pdf'))
        {
            @chmod ("../../catalogues/".$id.".pdf",'644');
            $sql = "UPDATE tbl_products SET catalogue = ? WHERE id = ?";
            $q = $db->prepare($sql);
            $query = $q->execute(array($id.'.pdf',$id));
        }
    }
}
else
{
    echo ' Step2 ';
    @unlink ('../../catalogues/'.$id.'.pdf');
    $sql = "UPDATE tbl_products SET catalogue = ? WHERE id = ?";
    $q = $db->prepare($sql);
    $query = $q->execute(array('',$id));
}

And the result is Step1 Step2 !
So when the file is uploaded successfully, new file will be uploaded and set to database, and then it will removed in step2 and its field in database will be empty. :(
This is very strange for me. Please help me.

Note: find_extension() function and $id are defined before these lines of code.

Mohammad Saberi
  • 12,864
  • 27
  • 75
  • 127
  • You have placed step 2 in the wrong place. It will run every time a file is uploaded. – SDZ May 23 '13 at 15:20
  • Aren't you by any chance double-clicking the submit button of your form ? – Laurent S. May 23 '13 at 15:20
  • @Bartdude - That wouldn't matter. Only the second request would load, so you would only see `Step 2`. – nickb May 23 '13 at 15:20
  • Hmmm, sure it isn't running multiple times? First time it runs, step one takes place, second time step 2? Echo start before the if and end below it – We0 May 23 '13 at 15:21
  • @nickb : believe it or not, I've seen cases (in old ASP, not in PhP though) where that kind of double-click did in fact send 2 times the same request... Now that was not exactly the same case as here, but still I prefer to check first :-) – Laurent S. May 23 '13 at 15:23
  • @SamiDzHamida, why else section must be done when `is_uploaded_file()` is TRUE and its codes runs successfully? – Mohammad Saberi May 23 '13 at 15:24
  • @MohammadBagherSaberi So step 2 should always run? – SDZ May 23 '13 at 15:25
  • No. It must run just when no file has been uploaded. – Mohammad Saberi May 23 '13 at 15:26
  • 1
    The amount of error suppression on this script is unreal. Those are some bad practices. – Daryl Gill May 23 '13 at 15:26
  • @MohammadBagherSaberi Could you explain to me exactly what should be happening in this script? – SDZ May 23 '13 at 15:27
  • You likely have same echo statement somewhere else in your code. rename these two to "step I" and "step II" and see output – Ranty May 23 '13 at 15:27
  • My second guess is that this code is executed twice. Write random echo statement right before the first `if` to check for that. – Ranty May 23 '13 at 15:30
  • @Ranty it does not matter to put `Step1` or something else here ... – Mohammad Saberi May 23 '13 at 15:35
  • @MohammadBagherSaberi it does if there's another `echo 'Step1';` somewhere else in your code that you forgot about. – Ranty May 23 '13 at 15:40
  • @Ranty there is not any other `echo 'Step1'` in the code. – Mohammad Saberi May 23 '13 at 15:54
  • Oops. I found the mistake. It was about one `for` loop that was not closed before these codes. Thank you all for your attention to my problem friends – Mohammad Saberi May 23 '13 at 16:30

1 Answers1

0

Try this:

if (is_uploaded_file($_FILES['catalogue']['tmp_name']))
{
    $ext = find_extension('catalogue');
    $ext_array = array('pdf');

    if (!in_array($ext,$ext_array))
    {
        // echo something for error message.
    }
    else
    {
        echo ' Step1 ';
        @unlink ('../../catalogues/'.$id.'.pdf');
        if(@move_uploaded_file($_FILES['catalogue']['tmp_name'],"../../catalogues/".$id.'.pdf'))
        {
            @chmod ("../../catalogues/".$id.".pdf",'644');
            $sql = "UPDATE tbl_products SET catalogue = ? WHERE id = ?";
            $q = $db->prepare($sql);
            $query = $q->execute(array($id.'.pdf',$id));
     }
}
else
{
    echo ' Step2 ';
    @unlink ('../../catalogues/'.$id.'.pdf');
    $sql = "UPDATE tbl_products SET catalogue = ? WHERE id = ?";
    $q = $db->prepare($sql);
    $query = $q->execute(array('',$id));
}

    }
SDZ
  • 726
  • 2
  • 8
  • 21
  • I think you have closed the if statements in the wrong places. Keep playing with them until you get the correct result. Trial and error is key! – SDZ May 23 '13 at 15:24
  • I realised, but that is his code. I think he needs to re-type it. – SDZ May 23 '13 at 15:44