/    Sign up×
Community /Pin to ProfileBookmark

Webpage errors when hosted on Apache Web Server

I have completed a draft of the website I wanted to create and here is the code from my index.php file:

[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”>
<br><br>
<br><br>
Submit:
<input type=”submit” value=”Upload Image” name=”submit”>
</form>

<?php
$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.”;
}
}
?>

</body>
</html>

[/code]

Whenever I access my website I get 4 notices underneath my actual contents:

[quote]

Notice: Undefined index: fileToUpload in /opt/lampp/htdocs/index.php on line 16

Notice: Trying to access array offset on value of type null in /opt/lampp/htdocs/index.php on line 16

Notice: Undefined index: fileToUpload in /opt/lampp/htdocs/index.php on line 39

Notice: Trying to access array offset on value of type null in /opt/lampp/htdocs/index.php on line 39
Sorry, only JPG, JPEG, PNG & GIF files are allowed.Sorry, your file was not uploaded.

[/quote]

What are these? Are they error messages? How to fix this and is there anything wrong with my code

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 30.2020 — The main problem is that in this code...
<i>
</i>&lt;?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

...$_FILES["fileToUpload"] won't be set unless the form has been submitted -- so it probably belongs inside of the following isset() block (though you might want to test that it exists before you try to use it, just in case?).

You _could_ change the error_reporting() setting to not include notice-level errors (which is probably why you didn't see these in local testing?), but it's better to test with all error enabled and get rid of them.
Copy linkTweet thisAlerts:
@Tiger50authorJun 30.2020 — 
You could change the error_reporting() setting to not include notice-level errors (which is probably why you didn't see these in local testing?), but it's better to test with all error enabled and get rid of them.
[/quote]


How do I do this?
Copy linkTweet thisAlerts:
@daveyerwinJun 30.2020 — 
you really should put the php in a file named upload.php

I put the code you posted in a file named upload.php

in the same directory as index.htm and created a folder

in the same directory named uploads

all was well
Copy linkTweet thisAlerts:
@NogDogJun 30.2020 — Without copying/pasting everything, the general idea is:
<i>
</i>&lt;html&gt;
&lt;!-- your HTML stuff here, including the form, then... --&gt;

&lt;?php
if(isset($_POST['submit'])) {
if(!empty($_FILES['fileToUpload'])) {
// do everything you want to do with the uploaded file here
}
else {
// do whatever you want to do if no file was actually uploaded
}
}
?&gt;
&lt;!-- end of the HTML stuff --&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@daveyerwinJun 30.2020 — I have posted an example here ...

https://www.drivebyjavascript.com/tiger50/

it is based on the code found here ...

https://www.w3schools.com/php/php_file_upload.asp
×

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 4.26,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...