If-Else语句不执行所有代码

时间:2015-08-20 03:24:02

标签: php forms if-statement

在表单处理脚本结束时,如果move_uploaded_file()成功,则echo "Successful"

else
    {
        move_uploaded_file($_FILES["looks"]["tmp_name"][$i], "user_pictures/$username/" . $newfilename); 

        echo "Successful";  
    }
} // for $i loop ends

但是,即使move_uploaded_file()有效(文件上传到我的本地目录),脚本也根本不会显示我的echo "Successful"

我不明白为什么会这样。这是我的完整表单处理代码。

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

session_start();

$username = $_SESSION['username'];
$title = $_SESSION['title'];

$file_dir = "user_pictures/$username" ;

mkdir($file_dir, 0777, true);

$looks_array = array();

$numberoflooks = count($_FILES['looks']['tmp_name']);

for ($i=0; $i < $numberoflooks; $i++) 

    {

        // $_FILES["hello"]["name'] is the file that is uploaded by her

        $filePath = $_FILES["looks"]["tmp_name"][$i];
        $image = imagecreatefrompng($filePath);
        $bg = imagecreatetruecolor(imagesx($image), imagesy($image));
        imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
        imagealphablending($bg, TRUE);
        imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
        imagedestroy($image);
        $quality = 100; // 0 = worst / smaller file, 100 = better / bigger file 
        imagejpeg($bg, $filePath . ".jpg", $quality);
        imagedestroy($bg);

        $newfilename = $title . "_" . $i . ".jpg";

        $imageFileType = pathinfo($newfilename, PATHINFO_EXTENSION);

        if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
        && $imageFileType != "gif" ) 

            {
                echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
                $uploadOk = 0;
            }

    else

            {
                $uploadOk = 1;

            }


    if ($uploadOk == 0) 

    {
        echo "";

    }

    else

    {

        move_uploaded_file($_FILES["looks"]["tmp_name"][$i], "user_pictures/$username/" . $newfilename); 


        echo "Successful";  

    }


} // for $i loop ends


?>

2 个答案:

答案 0 :(得分:0)

我无法评论你的帖子。尝试移动回声或在 move_uploaded_file 上方创建另一个回声以查看它是否有效。

答案 1 :(得分:0)

你可以尝试:

if (move_uploaded_file($_FILES["looks"]["tmp_name"][$i], "user_pictures/$username/" . $newfilename))
{
    echo "Successful";  
}
else
{
    die("failed);
}