PHP页面加载,但也在chrome浏览器中显示原始代码

时间:2013-03-29 13:52:50

标签: php web

我是PHP新手,我遇到了一些问题。

我在Netbeans 7.3中编码的.php文件在我通过IDE的编译器运行时运行正常。但是当我在谷歌Chrome浏览器中运行时,相同的网页会显示原始代码

以下是示例代码 -

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title> Add Your Highscore! </title>
    </head>
    <body>
        <h2> Guitar Wars - Add you highscore!</h2>
        <?php
        //echo "successful!!";


        if(isset($_POST['submit'])){
        $name=$_POST['name'];
        $score=$_POST['score'];
        $screenshot=$_FILES['screenshot']['name'];
        $screenshot_type=$_FILES['screenshot']['type'];
        $screenshot_size=$_FILES['screenshot']['size'];

        $output=false;
        if(!empty($name)&&!!empty($score)&&!empty($screenshot))
       {  

            if ((($screenshot_type == 'image/gif') || ($screenshot_type == 'image/jpeg') || ($screenshot_type == 'image/pjpeg') || ($screenshot_type == 'image/png'))
        && ($screenshot_size > 0) && ($screenshot_size <= GW_MAXFILESIZE)) {
        if ($_FILES['screenshot']['error'] == 0) {
          // Move the file to the target upload folder
          $target = 'images' . $screenshot;
          if (move_uploaded_file($_FILES['screenshot']['tmp_name'], $target)) 
            // Connect to the database
         {
            $db=  mysqli_connect('localhost','root', 'akash123','gwdb') 

                  or die('Error in connecting to the database');
        //echo "</br> successful connection";
        $query= "insert into guitar wars values(0,NOW(),$name, $score, $screenshot ";
        //echo "</br> successful query";
        $result= mysqli_query($db,$query) ;

       mysqli_close($db,$query);
       echo "</br> Successful!";
       $name='';
       $score='';
       $screenshot='';

        mysqli_close($db);
        }
        else
        {echo "There was a problem uploading the image";
        }

        }  
        else
        {echo "There was a problem uploading the image";

        $output=true;
        }
        }


        else{echo '<p class="error">The screen shot must be a GIF, JPEG, or PNG image file no greater than ' . (GW_MAXFILESIZE / 1024) . ' KB in size.</p>';
        $output=true;
        }}

        else{echo "* Enter all the information to add your highscore";
        $output=true;
        }

        }

        else
        { 
            $output=true;
            $name='';
            $score='';
        }

            if ($output){ 
            ?>

<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="hidden" name="MAX_FILE_SIZE" value="65536"/>
<label for="name">Full Name:</label>
<input type="text" id="name"  name="name" value="<?php echo $name ; ?>" /></br>
<label for="score">Your Highscore :</label>
<input type="text" id="score"  name="score" value="<?php echo $score ; ?>" /></br>
<label for="screenshot"> Screenshot of your Highscore: </label>
<input type="file" id="screenshot" name="screenshot" /></br>
<input type="submit" name="submit" value="Add"/>

</form>
    <?php

        }


        ?>
    </body>
</html>

Internet Explorer无法下载,因为它试图下载.php文件。

这个结果的原因是什么?

2 个答案:

答案 0 :(得分:1)

您的网络服务器可能没有将文件传递给PHP解释器。检查您的网络服务器配置。

答案 1 :(得分:1)

你不能在浏览器中“打开”一个php文件,你必须“运行”它。 netbeans将运行文件(注意它们不会像java编译的那样编译)

如果您在本地计算机上进行测试,则需要运行php(例如xampp)的apache安装。

如果您使用的是xampp:

打开xampp文件夹中的htdocs文件夹。

为您的网络应用创建一个新文件夹。

将您的php文件放在该文件夹中。

在你的网页浏览器中,NAVIGATE到你的php页面(在地址栏中输入运行xampp的机器的IP地址,或者输入localhost,或者你已经配置了它,例如mylocalhostip / mywebapp / myphpfile.php)。

那么它应该可以正常工作