1

这是加载良好的代码,无需在页面顶部添加功能

 <?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 之后和之前的顶部添加这样的函数时,如果页面的 !logged .. 它不起作用

 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;
    }

知道问题是什么以及如何解决吗?

4

1 回答 1

3

它不起作用的原因是因为在调用header(). 你不能的原因是因为 PHP 会抛出一个错误,标头已经发送。

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

您不能在此header()语句之前有任何输出,因此请将您的新代码放在它之后以避免任何问题。另外,请确保在开始的 php 标记之前没有任何 HTML。

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

于 2013-08-29T20:11:12.973 回答