用于上传文件的REst API

时间:2018-01-31 15:01:12

标签: php api

任何人都可以帮我下面如何使用此功能...此功能将文件上传到我的存储,但不幸的是我不知道如何使用该代码。我已经尝试了很多次但仍然没有用。这段代码不是我的,我从github得到它并尝试将它用于我的项目。

public function upload(){

  // Check that the path is a directory
  if(is_file($this->path)){ 
    $this->status = "error";
    $this->message = "Path Not A Directory";
  }else{
    // Handle upload
    $target = $this->path  . "/" . basename($_FILES['upload']['name']); 
    if(move_uploaded_file($_FILES['upload']['tmp_name'], $target)) {
      $this->status = "success";    
    }else{
      $this->status = "error";
      $this->message = "Upload Error";
    }          
  }
  $this->respond();        
}
<form action="file.php" method="get">
  <input type="hidden" name="action" value="upload" readonly>
  <input type="hidden" name="path" value="class/" readonly>
  <input type="file" name="upload" readonly>
  <!--  <input type="submit" name="create" class="btn btnsuccess btn-
                     sm" value="create dir/file ">
              <input type="submit" name="open" class="btn btn-success btn-sm" 
                      value="Open a file">
              -->
  <input type="submit" class="btn btn-success btn-sm" value="upload to specific dir">
  <!--  <input type="submit" name="modify" class="btn btn-success 
                      btn-sm" value="modify file or dir">
              <input type="submit" name="delete" class="btn btn-success btn-sm" 
                     value="delete file">
              <input type="submit" name="index" class="btn btn-success btn-sm" 
                     value="list dir">
              <input type="submit" name="dublicate" class="btn btn-success btn-
                    sm" value="duplicate"> -->
</form>

url:http://localhost/api/archiving%20system/file.php?action=upload&path=class%2F&upload=myfile.txt

输出:{"status":"error","message":"Upload Error"}

1 个答案:

答案 0 :(得分:4)

您需要将enctype="multipart/form-data"添加到<form>标记中 - 该方法可能应为POST而不是GET,最大文件大小...

<form action="file.php" method="POST" enctype="multipart/form-data" >
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
     <input type="hidden" name="action"  value="upload" readonly>
     <input type="hidden" name="path"  value="class/" readonly>
     <input type="file" name="upload" readonly>
     <input type="submit"  class="btn btn-success btn-sm" value="upload to specific dir">
</form>