dtby67541 2013-03-29 13:52
浏览 177
已采纳

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

I am new to PHP, and I am having a bit of an issue.

My .php files, which I code in Netbeans 7.3, work fine when I run them through the IDE's compiler. But the same web pages show the raw code, along when I run them in my Google Chrome browser.

Here is a sample code-

<!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 is hopeless, as it tries to downloads the .php file.

What is the reason of this outcome?

  • 写回答

2条回答 默认 最新

  • dseigqk7443 2013-03-29 14:10
    关注

    you cannot "open" a php file in a browser, you have to "run" it. netbeans will run files (note they don't get compiled in the way things like java get compiled)

    if you are testing on your local machine, you need an apache installation which runs php (xampp for example).

    if you are using xampp:

    open the htdocs folder inside the xampp folder.

    create a new folder for your webapp.

    put your php file in that folder.

    in your web browser, NAVIGATE to your php page (in the address bar, type in the IP address of the machine running xampp, or localhost, or however you have it configured, mylocalhostip/mywebapp/myphpfile.php for example.

    then it should work fine

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来