无法从html表单调用php

Unable to call php from html form

本文关键字:调用 php 表单 html      更新时间:2023-09-26

我想从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";
?>

file_upload =on in php.ini.

我不明白错在哪里。

请建议。提前感谢。

您的PHP正在寻找错误的值。你有你的HTML按钮设置为name="submitBtn",名称属性是你在PHP中选择当你使用$_POST["submit"]

所以你需要改变这个: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";
?>