如何将文件从javascript上传到php服务器?

时间:2017-08-19 06:40:44

标签: javascript php html css

如何将图像或其他文件从javascript上传到php服务器??? 我正在研究php api如何发送和接收 请帮我 我正在使用

获取josn数据
$login = file_get_contents('php://input');

我想使用js上传图像并使用php接收并上传到服务器磁盘 这是我的login.php文件

  <?php
session_start();
//server accpet response in forllowing format 
//{"username":"administrator","password":"administrator","remind":"on"}
// iuncluding function file
require_once "../includes/system.php";
    if(isset($_COOKIE['COOKIE_KEEP_ME_NAME1shpingcart'])){
            $_SESSION['login_usershoping'] =  $_COOKIE['COOKIE_KEEP_ME_NAME1shpingcart'];
        }   
   if(isset($_SESSION['login_usershoping'])){
    $success['alreadylogin'] = "true";
    echo $successlogin = malik_encode($success );
    die();
}
// get value
$login = file_get_contents('php://input');
    if(empty($login)){
        $error['error'] = "Username & password never be empty";
        echo $errorlogin = malik_encode($error );
        die();
    }
//decode and convert into assos array by using custom function
$login_decode = malik_decode($login,true);
    $username =  $login_decode['username'];
    $password =  $login_decode['password'];
    $remind = $login_decode['remind'];

    $db = malik_database_connection();
    $connected = null;   
    $username = $db->quote($username);
        $select = $db->query("SELECT * FROM users WHERE username=$username");
        if($select->rowCount() > 0){
            $user = $select->fetch();
            $ban = $user['ban'];
              if($ban === 'yes'){
                 $error['error'] = "Your account is banned by our admistrator";
                 echo $banerror = malik_encode($error );
              }         
            if (password_verify($password, $user["password"])){
                if(isset($remind) && $remind === 'on'){
                //generate for storing cookies
                setcookie('COOKIE_KEEP_ME_NAME1shpingcart',$user['salts'] ,time() + (86400 * 30 * 12), '/',false,true);
                //generate for storing cookies
                setcookie('COOKIE_KEEP_ME_NAME2',malik_ramdomstring_generator(60) ,time() + (86400 * 30 * 12), '/',false,true);
                //generate for storing cookies
                setcookie('COOKIE_KEEP_ME_NAME3',malik_ramdomstring_generator(50) ,time() + (86400 * 30 * 12), '/',false,true);
                }   
                $se= $_SESSION['login_usershoping'] =  $user['salts'];
                 $success['success'] = "true";
                 echo $loginsuccess = malik_encode($success );
                exit();

            }else {
                $connected = false;
            }
            }else{
            $connected = false;
        }
           if ($connected === false): 
           $error['error'] = "Username and password wrong";
            echo $loginerror = malik_encode($error );
         $connected = null; 
       endif; 

1 个答案:

答案 0 :(得分:1)

upload.html:

StatusList = new SelectList(Discountdata),

upload_file.php:

    <!DOCTYPE html>
<html lang="en">
    <head>
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    </head>
    <body>
        <form id="uploadform">
            <input type="file" name="file"/><br/>
            <button type="submit">
                Upload!
            </button>
        </form>
    </body>
    <script src="https://code.jquery.com/jquery-3.2.1.min.js" type="text/javascript"></script>
    <script>

        $("form#uploadform").submit(function () {
            var formData = new FormData(this);

            $.ajax({
                url: '/upload_file.php',
                type: 'POST',
                data: formData,
                async: false,
                success: function (data) {
                    alert(data)
                },
                error:function(data){
                    alert(data);
                },
                cache: false,
                contentType: false,
                processData: false
            });

            return false;
        });
    </script>
</html>