3

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!

JustMe
  • 306
  • 1
  • 4
  • 15
  • If you don't understand the code, what makes you think that you need to make it work? – Ja͢ck Jan 11 '15 at 07:51
  • In any case, this is a server configuration issue. Not exactly OT here. – Ja͢ck Jan 11 '15 at 07:52
  • Well, do you have PHP and a server installed? If not then that is your problem. For novices the easiest would be to install a server bundle. XAMPP has always worked very well in my experience, but there are [others out there](https://en.wikipedia.org/wiki/LAMP_(software_bundle)#Variants). – Sverri M. Olsen Jan 11 '15 at 08:16

3 Answers3

3

You are accessing the file through file://, and regardless of whether you have installed a webserver or not, if you open op the PHP file itself in the browser, the web server nas nothing to do with it; you are opening the file directly, without intervention of the webserver.

Your webserver is configured to run a local website, which is, I assume, accessible through your local IP address 127.0.0.1.

Type that in your browser and check if it works.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
1

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

Sh1d0w
  • 9,340
  • 3
  • 24
  • 35
  • I'm running it on my computer, through file:// – JustMe Jan 11 '15 at 07:57
  • Well you can not run php files like this. To execute PHP files you need apache server with PHP installed and configured. – Sh1d0w Jan 11 '15 at 07:58
  • JustMe, It seems to me you really need to read up on running PHP (or any other language for that matter) on the web. Your browser just serves static files, if you want to "run" your PHP, eg. get the output of your script in the browser, you will have to put an interpreter in between, which can be done via a web server (Apache, NginX, ...) for example. – Timusan Jan 11 '15 at 07:59
  • Also, what do I name the .htaccess file? Is it important? – JustMe Jan 11 '15 at 08:00
  • To use .htaccess file you will need to run the website through apache server, you can read on internet how to install and configure one. – Sh1d0w Jan 11 '15 at 08:01
  • @Timusan I am using OS X Yosemite, which comes with Apache installed. I followed a guide to turn on PHP in Terminal, but it didn't work. – JustMe Jan 11 '15 at 08:03
  • The Apache Web server would not start, so I moved it to a GitHub Pages at www.kiaye.org/games.html and when you attempt to upload a .swf, it gives a 413 error. Request Entity Too Large. – JustMe Jan 11 '15 at 17:02
  • 413 errors occur when the request body is larger than the server is configured to allow. Here’s how you can fix it - You want to try increasing PHP’s upload_max_filesize and post_max_size settings in php.ini or using the php_value directive in your .htaccess file if you’re using Apache (http://davidwalsh.name/php-values-htaccess) or you can contact your hosting provider for assistance. – Sh1d0w Jan 11 '15 at 20:36
0

You need a webserver like Nginx, Apache or the PHP server itself, if you do not then when you try to access the file through your browser (file://) then the browser will assume it is plain text.

The XAMPP and WAMP stacks are both recommended and very easy to install, they also come with MySQL (and Perl for XAMPP)

Of course you can just install the standalone web servers if all the goodies don't interest you, here are some instructions for installing Nginx and setting up PHP and here are the instructions for Apache.

Best of luck!