簡體   English   中英

無法從html表單調用php

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM