上传时出现错误时,页脚div不会显示

时间:2014-03-28 23:23:39

标签: php html

页脚包含在索引文件中,其中包含通向此页面的包含代码,如此

 <?php include "footer.php" ?>

 <center><div id="content"><br>
 <form method="post" enctype="multipart/form-data">
 <input type="file" class="file" name="userfile"/><br>
 <br>
 <button name="upload" type="submit" value="Submit" class="upload">Upload</button>
 </form>
 <?php
 if(isset($_POST['upload'])) {

 $allowed_filetypes = array('.jpg','.jpeg','.png','.gif');
 $max_filesize = 10485760;
 $upload_path = 'useruploads/';
 $description = $_POST['imgdesc'];

 $filename = $_FILES['userfile']['name'];
 $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);

 if(!in_array($ext,$allowed_filetypes))
 die('<h4>The file you attempted to upload is not allowed.');

 if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
 die('<h4>The file you attempted to upload is too large.');

 if(!is_writable($upload_path))
 die('<h4>You cannot upload to the specified directory, please CHMOD it to 777.');

 if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename)) {
 $query = "INSERT INTO uploads (name, description) VALUES ($filename, $description)"; 
 mysql_query($query);

 echo '<h5>Your file upload was successful!';


 } else {
 echo '<h4>There was an error during the file upload.  Please try again.';
 }
 }
 ?>
 </div>

如果您想亲眼看到我自己,我已暂时将此脚本上传到我的网站,这样您就可以看到错误,并自行尝试上传非图像文件的内容,您将会看到错误,我看到上传完成后是否可以添加按钮以将其链接到图像

Website Link

这里也是页脚代码

<center><div id="footer">
<p>Copyright &copy; OSPICTUREVAU 2014</p>
</div>

1 个答案:

答案 0 :(得分:1)

您正在使用die输出错误,这将在die语句后停止执行所有代码。这意味着您的页脚代码永远不会被执行。

Redirecton错误:

if (!in_array($ext, $allowed_filetypes))
    {
    header("Location: errorpage.php");
    exit;
    }

您可以将错误代码传递给errorpage.php,以便为用户提供有意义的错误消息。