这是加载良好的代码,无需在页面顶部添加功能
<?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;
}
知道问题是什么以及如何解决吗?