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>
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
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!
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.