javascript - 图像文件上传-无文件上传错误

标签 javascript php html file-upload

我正在尝试将图像文件上传到我的 php 服务器。我使用 JavaScript 作为选择按钮。选择按钮用于文件浏览,然后显示选定的图像,当我尝试将图像上传到服务器时,它会抛出错误 PHP 错误:

Array
(
    [image] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )

)

html页面代码

<div style="height:0px;overflow:hidden"><form id="myForm" action="http://192.168.2.4/digiid/webapi/capturehtml.php" method="post" enctype="multipart/form-data">
                    <input type="file" name="image" id="file"/>

            </div>
                    <a class="button1" id="browseButton" onclick="chooseFile()"  style="width:12%;height: 30px; text-decoration:none;"><font color="white" size="5px">Select ID</font></a>
        <br/>
    <img src='' id='imgscreen'  width='200' height='200'>
                    <div>
    <a class="button1"onclick="myFunction()" style= " width:12%;height: 30px; text-decoration:none;"><font color="white" size="5px">Save ID</font></a></form>

                      </div>    <script>
                    function myFunction() {
                        document.getElementById("myForm").submit();
                    }
                </script>
    <script>
    var imgScreen = document.getElementById("imgscreen");
    var audioPlayBtn = document.getElementById("playaudioButton");
    //var inputField = document.getElementById("audioURL");
    var browseButton = document.getElementById("browseButton");
    var x;

    var fileSelector = document.createElement('input')

    fileSelector.setAttribute("type", "file");
    fileSelector.setAttribute("accept","image/*");


    browseButton.onclick = function () {
        fileSelector.click();
        return false;
    };
    function chooseFile() {
     x=  document.getElementById("file").click();
        fileSelector(x);
    };

    fileSelector.onchange = function (x) {
        x = window.URL.createObjectURL(this.files[0]);
        if (x!=null) {
            //  only load a video file when the text field changes

                imgScreen.src = x;
           /* var fu1 = document.getElementById("FileUpload1");
            alert("You selected " + fu1.value);*/


            var path = x;
           var fileName = path.match(/[^\/\\]+$/);
          //  alert("  "+fileName);
            document.getElementById("audioURL").innerHTML=filename.value;


            imgScreen.load();
        }
    };</script>

php代码

echo "<pre>";
print_r($_FILES);
//error_reporting(9);
$filePath = $_FILES['image']['tmp_name'];
$fileName = $_FILES['image']['name'];
$userid=32;//$_SESSION["userid"];
if(!empty($filePath)){
$data = array('userid' => $userid, 'file' => "@$filePath", 'fileName' =>$fileName,'filename'=>$fileName);
    $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://192.168.2.4/digiid/api/addid.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 4);

if(curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 4)){

//file_get_contents('php://input');
    $json =curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if($httpcode==200){
        if($json!=null){
            $decoded=json_decode($json,true);
            $code= $decoded["code"];
            //    $userid=$decoded["userid"];
            if($code==0){
                header( 'Location: ./list.php' );

            }else{
                header( 'Location: ./capture.html' );
            }
        }
    }else{
        header( 'Location: ./notfound1.html' );

    }
}else{
    header( 'Location: ./notfound1.html' );

}
    curl_close($ch);
}

最佳答案

你能做一个 var_dump($_POST); 吗?在 php 脚本中查看发布到服务器的内容。

关于javascript - 图像文件上传-无文件上传错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25991189/

相关文章:

javascript - 如果我在用户受限页面上使用 Google Analytics(分析)会出现任何问题吗?

javascript - 在 Meteor 中使用自动表单将 postId 添加到评论中

php script.php 打印脚本,而不是在 linux 上执行它?

html - CSS动画绘制垂直线然后向左转

html - 传单图例如何通过 typescript 代码在div中设置样式css

javascript - JS - Canvas Image 在 Chrome 中无法正常工作(或如何等待图像正确加载)

javascript - 我怎样才能为结束播放声音创造一个 promise ?

javascript - 统计 HTML 表中唯一值的实例

php - 通过 API 发送(使用 Python)和接收(使用 PHP)文件

javascript - 在排行榜中回响