[英]Unable to call php from html form
我想从home.html调用“ phpControls.php”,将浏览的图像上传到所需的文件夹中。 我在Chrome浏览器中检查了该页面,该页面显示“上传”按钮未调用php文件。
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代码如下:
<?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";
?>
php.ini中的file_upload = on。
我没有弄错什么。
请提出建议。 提前致谢。
您的PHP正在寻找错误的值。 您已将HTML按钮设置为name="submitBtn"
,name属性是使用$_POST["submit"]
时在PHP中选择的属性。
因此,您需要更改以下内容: if(isset($_POST["submit"])) {
为此: if(isset($_POST["submitBtn"])) {
不知道这是否是唯一的问题,但至少应该使您的代码运行。 :)
最可能的唯一原因是isset函数检查了错误的值。 将您的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.