图像文件上传 - 没有文件上传错误

时间:2014-09-23 09:11:24

标签: 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 code

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);
}

1 个答案:

答案 0 :(得分:0)

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