简体   繁体   中英

Upload Image using javascript in php

------------------- uploadimage.php file ------------

<!DOCTYPE html>
<html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

    <title>Upload Images</title>

<script>

/////This Function Show The Details OF Selected File

function fileselected()
        {

            var file=document.getElementById("photo").files[0];            
            document.getElementById("filename").innerHTML=file.name;
            document.getElementById("filesize").innerHTML=file.size;
            document.getElementById("filetype").innerHTML=file.type;
        }

////////When Upload the image upload.php file is call but $_FILES is Empty             

function uploadImage()
        {

            var fd=new FormData();
            fd.append("photo",document.getElementById("photo").files[0]);

            var xmlhttp = new XMLHttpRequest();
            xmlhttp.upload.addEventListener("progress", uploadProgress, false);                
            var url = "http://localhost/JSONProgram/upload.php";

            xmlhttp.onreadystatechange=function() {

                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("result").innerHTML=xmlhttp.responseText;

                }
            }

            xmlhttp.open("POST", url);
            xmlhttp.setRequestHeader('Content-Type', 'image/png');
            xmlhttp.send(fd);

        }

function uploadProgress(evt) {

        if (evt.lengthComputable) {
                var percentComplete = Math.round(evt.loaded * 100 / evt.total);
                document.getElementById('progressNumber').innerHTML = percentComplete.toString() + '%';
                document.getElementById('prog').value = percentComplete;
            }
            else {
                document.getElementById('progressNumber').innerHTML = 'unable to compute';
            }
        }
    </script>
    </head>

    <body>
        <form>
            <div class="col-md-8">
                <div id="result">Result Here</div>
                <label for="photo">Select Image</label>
                <input type="file" id="photo" name="photo" onchange="fileselected()"/>
                <input type="button" value="Upload" onclick="uploadImage()" value="Upload"/>
                <div id="filename"></div>
                <div id="filesize"></div>
                <div id="filetype"></div>

                <div id="progressNumber">
                </div>
                <progress id="prog" value="0" max="100.0"></progress>
            </div>
        </form> 

    </body>
</html>

but in upload.php has empty array of $_FILES

<?php
print_r($_FILES);
?>

How To get File for Upload? Please Help

Add the enctype attribute to your form tag like this:

<form enctype="multipart/form-data">

See this link for more information about the enctype.

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