2014-09-23 136 views
1

我amtrying上傳圖像文件到我的PHP服務器。我使用JavaScript用於文件瀏覽選擇button.the選擇按鈕,然後選擇圖像顯示,當我試圖上傳圖片到服務器時,它拋出錯誤 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); 
} 

回答

0

你可以做的var_dump($ _ POST);在php腳本中查看發佈到服務器的內容。