简体   繁体   中英

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. I inspected the page in Chrome, it shows that the Upload button is not calling the php file.

The HTML code is as follows:

            <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:

<?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.

I am not getting what's the mistake.

Please suggest. Thanks in advance.

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"] .

So you need to change this: if(isset($_POST["submit"])) {

To this: 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. Replace your phpControls.php code to the following.

<?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";
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM