什么是exit()的替代方案;没有打破页面

时间:2014-08-31 17:03:39

标签: php upload exit-code

例如,现在当我使用die()exit()删除脚本时,我的页面看起来有一半被破坏,因为页面底部的html无法加载,因为它是使用{{{ 1}}功能。

那么有没有办法告诉PHP“除了网页的其余部分加载之外,不允许执行任何其他命令”?

我将其用于图片上传脚本。

前面:enter image description here

但是当我上传的文件类型错误或根本没有文件时,它会破坏我的网页,因为我设置了exit();

enter image description here enter image description here

我该怎么做才能解决这个问题?这是我的代码,你会注意到exit();的

include()

2 个答案:

答案 0 :(得分:1)

试试这个,我们的想法是你必须完成整个脚本,这样你才能添加页脚,这样页面看起来就完整了。因此,不要在找到错误时回显错误,然后退出脚本,将它们保存在变量中。然后,一旦你到达脚本的主要部分,即图像被加载,你想要调整它的大小,你决定是否有错误只输出错误而不是图像调整大小。如果没有找到错误,那么你摆弄图像,不要输出任何错误信息。

if (isset($_POST['submit']))
{
    $file_uniq_id = uniqid();

    // Access the $_FILES global variable for this specific file being uploaded
    // and create local PHP variables from the $_FILES array of information

    $fileName = $_FILES["uploaded_file"]["name"]; // The file name
    $fileTmpLoc = $_FILES["uploaded_file"]["tmp_name"]; // File in the PHP tmp folder
    $fileType = $_FILES["uploaded_file"]["type"]; // The type of file it is
    $fileSize = $_FILES["uploaded_file"]["size"]; // File size in bytes
    $fileErrorMsg = $_FILES["uploaded_file"]["error"]; // 0 for false... and 1 for true
    $kaboom = explode(".", $fileName); // Split file name into an array using the dot
    $fileExt = end($kaboom); // Now target the last array element to get the file extension

    // START PHP Image Upload Error Handling 

    $Err = NULL;

    if (!$fileTmpLoc)
    { // if file not chosen
        $Err = "ERROR: Please browse for a file before clicking the upload button.";
    }
    else
    if ($fileSize > 5242880)
    { // if file size is larger than 5 Megabytes
        $Err = "ERROR: Your file was larger than 5 Megabytes in size.";
        unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
    }
    else
    if (!preg_match("/.(gif|jpg|png)$/i", $fileName))
    {
        // This condition is only if you wish to allow uploading of specific file types

        $Err = "ERROR: Your image was not .gif, .jpg, or .png.";
        unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
    }
    else
    if ($fileErrorMsg == 1)
    { // if file upload error key is equal to 1
        $Err = "ERROR: An error occured while processing the file. Try again.";
    }

    // END PHP Image Upload Error Handling 
    // Place it into your "uploads" folder mow using the move_uploaded_file() function

    $moveResult = move_uploaded_file($fileTmpLoc, "uploads/$fileName");

    // Check to make sure the move result is true before continuing

    if ($moveResult != true) {
        $Err = "ERROR: File not uploaded. Try again.";
        // not needed PHP is supposed to do this
        // unlink($fileTmpLoc);
    }

    // ---------- Include Adams Universal Image Resizing Function --------

    if ( ! isset( $Err ) ) {    

        include_once ("libs/ak_php_img_lib_1.0.php");

        $target_file = "uploads/$fileName";
        $resized_file = "uploads/" . $file_uniq_id . "." . $fileExt;
        $resized_file_final = $file_uniq_id . "." . $fileExt;
        mysql_query("UPDATE users 
                     SET profile_image = '$resized_file_final' 
                     WHERE id = '$id' ");
        $wmax = 128;
        $hmax = 128;
        ak_img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);
        unlink($target_file);

        // ----------- End Adams Universal Image Resizing Function -----------
        // Display things to the page so you can see what is happening for testing purposes

        // as we had to process error in this if we better test again
        echo "The file named <strong>$fileName</strong> uploaded successfuly.<br /><br />";
        echo "It is <strong>$fileSize</strong> bytes in size.<br /><br />";
        echo "It is an <strong>$fileType</strong> type of file.<br /><br />";
        echo "The file extension is <strong>$fileExt</strong><br /><br />";
        echo "The Error Message output for this upload is: <strong>$fileErrorMsg</strong><br /><br />";
        echo "The new name for the file is: <strong>$resized_file_final</strong>";

    } else {
       echo $Err;
    }
}

答案 1 :(得分:0)

通常function用于将代码组合在一起。然后你可以用return;语句中途停止一个函数。函数使代码保持组织和可重用。下一步是将相关函数放在类中,然后将生成输出(echo-statements)和逻辑的代码分离到不同的类中,但这是一个新的范例供您掌握(查看面向对象的编程 - OOP。这很难,但如果你想要进步,这非常重要。

作为将代码分组为函数的示例,if语句中的代码可以放在uploadFile函数中,如下所示:

<?php

function uploadFile($id, $file) {
    $file_uniq_id = uniqid();

    // Access the $_FILES global variable for this specific file being uploaded
    // and create local PHP variables from the $_FILES array of information

    $fileName = $file["name"]; // The file name
    $fileTmpLoc = $file["tmp_name"]; // File in the PHP tmp folder
    $fileType = $file["type"]; // The type of file it is
    $fileSize = $file["size"]; // File size in bytes
    $fileErrorMsg = $file["error"]; // 0 for false... and 1 for true
    $kaboom = explode(".", $fileName); // Split file name into an array using the dot
    $fileExt = end($kaboom); // Now target the last array element to get the file extension

    // START PHP Image Upload Error Handling --------------------------------------------------

    if (!$fileTmpLoc)
    { // if file not chosen
        echo "ERROR: Please browse for a file before clicking the upload button.";
        return;
    }
    else
    if ($fileSize > 5242880)
    { // if file size is larger than 5 Megabytes
        echo "ERROR: Your file was larger than 5 Megabytes in size.";
        unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
        return;
    }
    else
    if (!preg_match("/.(gif|jpg|png)$/i", $fileName))
    {

        // This condition is only if you wish to allow uploading of specific file types

        echo "ERROR: Your image was not .gif, .jpg, or .png.";
        unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
        return;
    }
    else
    if ($fileErrorMsg == 1)
    { // if file upload error key is equal to 1
        echo "ERROR: An error occured while processing the file. Try again.";
        return;
    }

    // END PHP Image Upload Error Handling ----------------------------------------------------
    // Place it into your "uploads" folder mow using the move_uploaded_file() function

    $moveResult = move_uploaded_file($fileTmpLoc, "uploads/$fileName");

    // Check to make sure the move result is true before continuing

    if ($moveResult != true)
    {
        echo "ERROR: File not uploaded. Try again.";
        unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
        return;
    }

    // unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
    // ---------- Include Adams Universal Image Resizing Function --------

    include_once ("libs/ak_php_img_lib_1.0.php");

    $target_file = "uploads/$fileName";
    $resized_file = "uploads/" . $file_uniq_id . "." . $fileExt;
    $resized_file_final = $file_uniq_id . "." . $fileExt;
    mysql_query("UPDATE users SET profile_image = '$resized_file_final' WHERE id = '$id' ");
    $wmax = 128;
    $hmax = 128;
    ak_img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);
    unlink($target_file);

    // ----------- End Adams Universal Image Resizing Function -----------
    // Display things to the page so you can see what is happening for testing purposes

    echo "The file named <strong>$fileName</strong> uploaded successfuly.<br /><br />";
    echo "It is <strong>$fileSize</strong> bytes in size.<br /><br />";
    echo "It is an <strong>$fileType</strong> type of file.<br /><br />";
    echo "The file extension is <strong>$fileExt</strong><br /><br />";
    echo "The Error Message output for this upload is: <strong>$fileErrorMsg</strong><br /><br />";
    echo "The new name for the file is: <strong>$resized_file_final</strong>";
}

if (isset($_POST['submit']))
{
    uploadFile($id, $_FILES["uploaded_file"]);
}

虽然距离组织良好的代码还很远,但它通过将exit()替换为return;来解决您的问题。