无法从html表单调用php

时间:2022-10-16 10:49:02

I want to call 'phpControls.php' from home.html to upload the browsed image in to a desired folder. I inspected the page in Chrome, it shows that the Upload button is not calling the php file.

我想从home.html调用'phpControls.php'将浏览过的图像上传到所需的文件夹。我在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.

请建议。提前致谢。

2 个解决方案

#1


0  

Your PHP is looking for the wrong value. You have your HTML button set to name="submitBtn", the name attribute is what you're selecting in PHP when you use $_POST["submit"].

您的PHP正在寻找错误的值。您将HTML按钮设置为name =“submitBtn”,当您使用$ _POST [“submit”]时,name属性是您在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. :)

不确定这是否是唯一的问题,但至少应该让你的代码运行。 :)

#2


0  

Most probably the only reason is the wrong value being checked by isset function. Replace your phpControls.php code to the following.

最可能的唯一原因是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";
?>

#1


0  

Your PHP is looking for the wrong value. You have your HTML button set to name="submitBtn", the name attribute is what you're selecting in PHP when you use $_POST["submit"].

您的PHP正在寻找错误的值。您将HTML按钮设置为name =“submitBtn”,当您使用$ _POST [“submit”]时,name属性是您在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. :)

不确定这是否是唯一的问题,但至少应该让你的代码运行。 :)

#2


0  

Most probably the only reason is the wrong value being checked by isset function. Replace your phpControls.php code to the following.

最可能的唯一原因是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";
?>