添加任何函数都不会;不允许加载页面


adding any function doesn't allow page to load

这是加载良好的代码,而不添加页面顶部的函数

 <?php

    if (!logged_in()) {
    header('Location: index.php');
    exit();
    } 

    ?>
    <h3>Upload image</h3>
    <?php
    if (isset($_FILES['image'], $_POST['image_n'], $_POST['image_description'])) {
    $image_name = $_FILES['image']['name'];
    $bytes = $_FILES['image']['size'];
    $image_temp = $_FILES['image']['tmp_name'];
    $image_n = $_POST['image_n'];
    $image_description = $_POST['image_description'];
    $allowed_ext = array('jpg', 'jpeg', 'png', 'gif', 'rar', 'pdf');
    //$image_ext = strtolower(end(explode('.', $image_name)));
    $image_ext = pathinfo($image_name, PATHINFO_EXTENSION);
    $album_id = $_SESSION['varname'];
    $errors = array();
    if (empty($image_name) || empty($album_id) || empty($image_n) ||     empty($image_description)) {
        $errors[] = 'Something is missing';
    } else {
    if (strlen($album_name) > 55 || strlen($album_description) > 255) {
            $errors[] = 'One or more fields contains too many characters';
        }
    if (in_array($image_ext, $allowed_ext) === false) {
        $errors[] = 'File type not allowed';
    }
    //if ($image_size > 2097152) {
    //  $errors[] = 'Maximum file size is 2mb';
    //}
    if (album_check($album_id) === false) {
        $errors[] = 'Couldn''t upload to that album';
    }
    }
    if (!empty($errors)) {
        foreach ($errors as $error) {
            echo $error, '<br />';
        }
    } else {
        $byte = formatSizeUnits($bytes);
        upload_image($image_temp, $image_ext, $album_id, $image_n,    $image_description, $byte);
        header('Location: view_album.php?album_id='.$album_id);
        exit();
    }
    }

    ?>
    <form action="" method="post" enctype="multipart/form-data">
    <div class="choose">
        <p>Choose a file:<br /><input type="file" name="image" /></p>
        </div>
            <div class="des">
            <p>Name*:<br /><input type="text" name="image_n" maxlength="55"/>  </p>
            <p>Description*:<br /><textarea name="image_description" rows="6" cols="35" maxlength="255"></textarea></p>

        <p><input type="submit" value="Upload" /></p>
        </div>
    </form>
<div class="foot">
<?php   
//include 'template/footer.php';    
?>
</div>

但是当我在php之后和if之前的顶部添加这样的函数时!已记录。。页面的不起作用

 function formatSizeUnits($bytes)
    {
        if ($bytes >= 1073741824)
        {
            $bytes = number_format($bytes / 1073741824, 2) . ' GB';
        }
        elseif ($bytes >= 1048576)
        {
            $bytes = number_format($bytes / 1048576, 2) . ' MB';
        }
        elseif ($bytes >= 1024)
        {
            $bytes = number_format($bytes / 1024, 2) . ' KB';
        }
        elseif ($bytes > 1)
        {
            $bytes = $bytes . ' bytes';
        }
        elseif ($bytes == 1)
        {
            $bytes = $bytes . ' byte';
        }
        else
        {
            $bytes = '0 bytes';
        }
        return $bytes;
    }

知道问题出在哪里以及如何解决吗?

它不起作用的原因是在调用header()之前不能有任何输出。不能这样做的原因是PHP会抛出一个错误,即标头已经发送。

if (!logged_in()) {
   header('Location: index.php');
   exit();
} 

在这个header()语句之前不能有任何输出,所以请将新代码放在它之后,以避免出现任何问题。此外,也要确保在打开php标记之前没有任何HTML。

我还建议删除打开PHP标记和第一个if语句之间的空行。