fahad shaikh
fahad shaikh

Reputation: 631

move_uploaded_file returns true but does not execute if statement

I am using PHPMailer for sending mail with attachment. I am saving the file on the server first and for this, I'm using PHP move_uploaded_file function.

The problem is when I debug my code, move_uploaded_file function returns true but it does not execute any statement inside if the condition and jump inside the else condition.

I don't understand this behaviour if the condition is true then if statement should be executed. Please let me know the problem.

Here is my code

        $targetfolder = "docs/cv/";
        $new_file_name = md5($_FILES['cv']['tmp_name']);

        //$targetfolder = $targetfolder . basename($_FILES['cv']['tmp_name']);
        $targetfolder = $targetfolder . $new_file_name . '.png';
        $ext = pathinfo($_FILES['cv']['name'], PATHINFO_EXTENSION);

        //if (move_uploaded_file($_FILES['cv']['tmp_name'], $targetfolder))
        if (move_uploaded_file($_FILES['cv']['tmp_name'], $targetfolder))
        {
           echo "Uploaded";
        }
        else {
           echo "Not Uploaded";
        }

Upvotes: 0

Views: 116

Answers (1)

Dipak Mewada
Dipak Mewada

Reputation: 365

Try this:

    $targetfolder = "docs/cv/";
    $new_file_name = md5($_FILES['cv']['tmp_name']);

    //$targetfolder = $targetfolder . basename($_FILES['cv']['tmp_name']);
    $targetfolder = $targetfolder . $new_file_name . '.png';
    $ext = pathinfo($_FILES['cv']['name'], PATHINFO_EXTENSION);

    //if (move_uploaded_file($_FILES['cv']['tmp_name'], $targetfolder))
    if (move_uploaded_file($_FILES['cv']['tmp_name'], $targetfolder) === true)
    {
       echo "Uploaded";
    }
    else {
       echo "Not Uploaded";
    }

Upvotes: 1

Related Questions