[英]Unable to call php from html form
I want to call 'phpControls.php' from home.html to upload the browsed image in to a desired folder. 我想从home.html调用“ phpControls.php”,将浏览的图像上传到所需的文件夹中。 I inspected the page in Chrome, it shows that the Upload button is not calling the php file. 我在Chrome浏览器中检查了该页面,该页面显示“上传”按钮未调用php文件。
The HTML code is as follows: HTML代码如下:
<form method="post" enctype="multipart/form-data" action="phpControls.php">
<input type="file" name="browseFile" id="browseFile" accept="image/*" onchange="loadFile(event)"
style="width: 50%; margin-top: 1%"
class="btn btn-info btn-lg" > <!--style="opacity: 0"-->
<script>
var loadFile = function(event) {
var output = document.getElementById('preview');
output.src = URL.createObjectURL(event.target.files[0]);
};
</script>
<input type="submit" id="submitBtn" name="submitBtn" value="Upload" class="btn btn-info btn-lg"
style="width: 50%; margin-top: 1%">
</input>
</form>
phpControls.php code is as follows: phpControls.php代码如下:
<?php
echo "Enter php";
$target_dir = "SharedFolder/";
$target_file = $target_dir . basename($_FILES["browseFile"]["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["browseFile"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
echo "Exit php";
?>
file_upload =on in php.ini. php.ini中的file_upload = on。
I am not getting what's the mistake. 我没有弄错什么。
Please suggest. 请提出建议。 Thanks in advance. 提前致谢。
Your PHP is looking for the wrong value. 您的PHP正在寻找错误的值。 You have your HTML button set to name="submitBtn"
, the name attribute is what you're selecting in PHP when you use $_POST["submit"]
. 您已将HTML按钮设置为name="submitBtn"
,name属性是使用$_POST["submit"]
时在PHP中选择的属性。
So you need to change this: if(isset($_POST["submit"])) {
因此,您需要更改以下内容: if(isset($_POST["submit"])) {
To this: if(isset($_POST["submitBtn"])) {
为此: if(isset($_POST["submitBtn"])) {
Not sure if this is the only issue, but it should at least make your code run. 不知道这是否是唯一的问题,但至少应该使您的代码运行。 :) :)
Most probably the only reason is the wrong value being checked by isset function. 最可能的唯一原因是isset函数检查了错误的值。 Replace your phpControls.php code to the following. 将您的phpControls.php代码替换为以下代码。
<?php
echo "Enter php";
$target_dir = "SharedFolder/";
$target_file = $target_dir . basename($_FILES["browseFile"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submitBtn"])) {
$check = getimagesize($_FILES["browseFile"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
echo "Exit php";
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.