/    Sign up×
Community /Pin to ProfileBookmark

php does not run properly, shows up as text on web browser

I created an html form:

[code]
<!DOCTYPE html>
<html>
<body>

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

</body>
</html>
[/code]

That calls the following php file:

[code]

<?php
echo “Hello World”;

$target_dir = “uploads/”;
$target_file = $target_dir . basename($_FILES[“fileToUpload”][“name”]);
$uploadOk = 1;
$imageFileType = strtolower(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;
}

// Check file size
if ($_FILES[“fileToUpload”][“size”] > 500000) {
echo “Sorry, your file is too large.”;
$uploadOk = 0;
}

// Allow certain file formats
if($imageFileType != “jpg” && $imageFileType != “png” && $imageFileType != “jpeg”
&& $imageFileType != “gif” ) {
echo “Sorry, only JPG, JPEG, PNG & GIF files are allowed.”;
$uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo “Sorry, your file was not uploaded.”;
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES[“fileToUpload”][“tmp_name”], $target_file)) {
echo “The file “. basename( $_FILES[“fileToUpload”][“name”]). ” has been uploaded.”;
} else {
echo “Sorry, there was an error uploading your file.”;
}
}

?>
[/code]

But rather than running as intended, it just shows up on the browser as the original code on the web browser

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@Tiger50authorJul 15.2020 — This is being hosted on my laptop. All the files (php and html) are in the same folder.
Copy linkTweet thisAlerts:
@daveyerwinJul 15.2020 — What HTML Server do you have installed on your laptop ?
Copy linkTweet thisAlerts:
@daveyerwinJul 15.2020 — when i ran your code on my server

i got this result

Hello WorldFile is an image - image/jpeg.The file https___console.brax-cdn.com_creatives_4807de2e-a8f3-43b5-8d44-a63073961018_F7U-Cutlass-631_1000x600_904a027f55eba637ef2eeca405b9073a.jpg has been uploaded.
Copy linkTweet thisAlerts:
@Tiger50authorJul 15.2020 — None I think, I just clicked on my html directly and ran it on Google Chrome. Do I need an html server for this? I am new to this stuff
Copy linkTweet thisAlerts:
@daveyerwinJul 15.2020 — yes it needs an HTTP server and PHP installed also
Copy linkTweet thisAlerts:
@daveyerwinJul 15.2020 — 
you can install any one l many free http servers

on your own machine

google for ...

free http servers for windows

but my suggestion is get

some free web hosting

google for ...

free http hosting with php and mysq[

pick one

get an account

ask here for help

at some point you will want to

share your url with the world so

all can see your work
Copy linkTweet thisAlerts:
@NogDogJul 15.2020 — If on Windows, [Wampserver](https://www.wampserver.com/en/) is relatively easy to get up and running.

[Xampp](https://www.apachefriends.org/index.html) has versions for OSX, Linux, and Windows.
×

Success!

Help @Tiger50 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...