douliang1891 2015-01-11 07:47
浏览 73

PHP文件在浏览器中打开而不是运行[重复]

This question already has an answer here:

I need a bit of PHP in a website I am building for a school project. I need it to upload .swf files. But when I press the "upload" button, it opens up the php file in the browser rather than running it, or displaying an error if there is one.

This is my HTML code:

<!DOCTYPE html>
<html>
<head>
<title>New Smallcut</title>
</head>
<body>
<font face="verdana" size="2">
<div style="text-align:center">
This is a webpage<br>
But no matter what, I am King
<p><a href=http://www.kiaye.org/>KIAYEorg</a></p>
    <br>
    <img src=KingSn0w.png width="100" height="100" alt=“King Sn0wCh1ld’s logo”>


    <form action="upload.php" method="post" enctype="multipart/form-data">
        Select game to upload:
        <input type="file" name="fileToUpload" id="fileToUpload">
        <input type="submit" value="Upload Image" name="submit">
    </form>
</div>
</font>

It is supposed to put a bit of text above an image and the form, on an otherwise plain webpage.

Next is the PHP, which I got from W3Schools.

<?php
$target_dir = "uploads";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
} else {
    echo "File is not an image.";
    $uploadOk = 0;
}
}

// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}

// Allow certain file formats
if($imageFileType != "swf") {
echo "Sorry, only .swf files are allowed.";
$uploadOk = 0;
}
?>

I have absolutely no clue why it's not working, I am just getting back into HTML after 5 years of not using it (I was 9 when I last used it), and I really need this bit of PHP (which I do not understand, for the record), to work. I will later need a bit more PHP to work for a site-wide search, and I assume, the search results and stuff, and it would be comforting if I could get this to work.

Thanks in advance!

</div>
  • 写回答

3条回答 默认 最新

  • douwu0882 2015-01-11 07:56
    关注

    Try adding this to .htaccess file where your PHP file is located

    AddHandler application/x-httpd-php .php
    

    If it does not work, that mean you do not have PHP installed on your apache server. Contact your hosting provider.

    EDIT:

    You need to have apache server with PHP module installed in order to run PHP files.

    You can install a bundle like XAMPP and then you will be able to run php files. You can read more here: https://www.apachefriends.org/index.html

    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?