HTML / php Parser错误

时间:2012-09-27 16:22:12

标签: php html

我正在浏览http://net.tutsplus.com/tutorials/php/online-file-storage-with-php/comment-page-2/#comments上的教程 它工作正常,直到:

if(strlen($message) > 0)
{
    $message = '<p class="error">' . $message . '</p>';
}

这行php在index.php中找到。当我在firefox中的页面很少时,看起来php解析器停在大于。我能逃脱这个角色吗?我需要吗?

编辑:所有的PHP代码:

<?php 
//Load the settings
require_once("settings.php");

$message = "";
//Has the user uploaded something?
if(isset($_FILES['file']))
{
    $target_path = Settings::$uploadFolder;
    $target_path = $target_path . time() . '_' . basename( $_FILES['file']['name']); 

    //Check the password to verify legal upload
    if($_POST['password'] != Settings::$password)
    {
        $message = "Invalid Password!";
    }
    else
    {
        //Try to move the uploaded file into the designated folder
        if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
            $message = "The file ".  basename( $_FILES['file']['name']). 
            " has been uploaded";
        } else{
            $message = "There was an error uploading the file, please try again!";
        }
        }

    //Clear the array
    unset($_FILES['file']);
}


if(strlen($message) > 0)
{
    $message = '<p class="error">' . $message . '</p>';
}    
?>
<html> ... </html> //my html code

4 个答案:

答案 0 :(得分:3)

>不会导致PHP解析器停止。

没有看到服务器的HTML输出,很难肯定,但由于>是文件中的第一个>,因此PHP解析器似乎永远不会启动,浏览器会将文件开头的<?phpstrlen($message) > 之间的所有内容视为标记

您需要通过安装了PHP并配置为处理该文件的Web服务器来访问PHP(通常通过为其提供.php文件扩展名来完成)。

答案 1 :(得分:1)

这个怎么样?

if(!empty($message)){
    $message = '<p class="error">'.$message.'</p>';
}

但是,为什么不直接将段落标记分配给错误消息,而不是先将错误消息分配给$message,然后再将段落标记分配?

答案 2 :(得分:0)

if条件没有任何错误,其工作正常

enter image description here

中可能出现的问题
  1. if(isset($_FILES['file']))
  2. if($_POST['password'] != Settings::$password)
  3. if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path))
  4. 如果你没有进入if体,则意味着

    中的问题
      if(isset($_FILES['file']))
    

    因为它比$message = "";

    更快

答案 3 :(得分:-1)

始终使用 Yoda条件并以(反)(d)顺序编写此类陈述(您通常习惯于:

if ( 0 !== strlen( $message ) )
{
    $message = 'Hello World!';
}

无论如何,您也可以查看! empty( $message )