The written program is supposed to take a file from the user and if the user has uploaded a file, it will print the word ok, otherwise it will print the sentence, please select the desired file (that is, when the user did not upload any file)
In our opinion, the written program is correct, but unfortunately, the third if block is not considered.
In the html codes, we send the file with the post method, and in the php codes, we check that the file is sent with the post method, if the file is sent with this method, we check that the upload button is set (for the moment of clicking) in Finally, the third if block checks whether the user has uploaded a file or not. If a file has been uploaded, it prints the word ok, otherwise it prints this sentence, please select the desired file.
The problem starts here that the third if block is not considered, that is, if the user has uploaded the file, the sentence "Please select the desired file" will be printed (that is, the else block).
html code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.container{
max-width: 600px;
background: #fff;
position: relative;
height: 151px;
border-radius: 5px;
margin: 0 auto;
margin-top: 300px;
box-shadow: 0px 0px 6px 1px #efefef;
padding-top: 118px;
}
.msg{
position: absolute;
top: -14px;
background: #f9f9f9;
width: 100%;
padding: 14px 16px;
box-sizing: border-box;
color: #000;
text-align: right;
width: 85%;
left: 0;
transform: translate(8%,50%);
border-right: 3px solid #00ceff;
}
form{
justify-content: center;
align-items: center;
flex-direction: column;
margin: 0 40px;
}
div.upload-wrapper{
color: white;
font-weight: bold;
display: flex;
}
input[type="file"] {
position: absolute;
left: -9999px;
}
input[type="submit"]{
color: white;
background: #33bb30;
margin: 10px 0;
border-radius: 5px;
font-weight: bold;
padding: 9px 30px;
cursor: pointer;
border: none;
}
input[type="submit"]:hover{
background: #555;
}
label[for="file-upload"]{
padding: 0.7rem;
display: inline-block;
background: #5d9eff;
cursor: pointer;
border-radius: 0 5px 5px 0;
border-left: 0;
}
label[for="file-upload"]:hover{
background: #ca3103;
}
span.file-name{
padding:0.7rem 14rem 0.7rem 0.7rem;
white-space: nowrap;
overflow: hidden;
background: #ececec;
color: black;
border: 1px solid #ececec;
border-radius: 5px 0 0 5px;
border-right: 0;
width: 100%;
}
</style>
</head>
<body>
<div class="container">
<form method="POST" action="upload.php" enctype="multipart/form-data">
<div class="upload-wrapper">
<span class="file-name">Choose a file...</span>
<label for="file-upload">Browse</label>
<input type="file" id="file-upload" name="uploadedFile">
</div>
<input type="submit" name="uploadBtn" value="Upload"/>
</form>
</div>
</body>
</html>
php code:
<?php
session_start();
$msg = null;
if ($_SERVER["REQUEST_METHOD"]=="POST"){
if(isset($_POST["uploadBtn"]) && $_POST["uploadBtn"]=="Upload"){
if(isset($_POST["uploadedFile"]) && !empty($_POST["uploadedFile"])){
echo $msg="Ok";
}else{
echo $msg="Please select the desired file!";
}
}
}
In our opinion, the word ok should be printed when the user uploads a file, but unfortunately this does not happen when the user uploads the file.
In addition, we do not want to save the file somewhere or add a new feature, and we just want these two sentences to be printed.
2
Answers
Data about files will be put under the $_FILES variable, not the $_POST variable For more in depth information about the $_FILES variable, refer here. The corect php code:
Data about files will be put under the
$_FILES
variable, not the$_POST
variable.For more in depth information about the
$_FILES
variable, refer here.