move_uploaded_file不适用于ng-file-upload

时间:2016-06-10 09:11:48

标签: php angularjs rest ng-file-upload

我尝试将图片上传到服务器,并将路径插入数据库。插入到数据库工作正常,在客户端上传(内置角度)后,文件似乎在$ _FILES中找到。但是move_uploaded_file函数返回false并且文件没有移动到任何地方。可能是什么问题呢?

VIEW.html

<div class="insert col-lg-3 col-sm-12">
 <input type="file" name="file" ngf-select="uploadFiles($file)" ngf-pattern="'image/*'"  />
</div>

CONTROLLER.js

 $scope.uploadFiles = function(file) {
        console.log(file);
        Upload.upload({
          url: '../backend/index.php/user/uploadfile?id=' + 1,
          method: 'POST',
          file: file,
          sendFieldsAs: 'form'
        }).then(function successCallback (response)  {
              console.log(response.data);
        });
      };

user.php的

 /**
 * @param $id
 * @url uploadfile
 * @return bool
 */

public function postUploadfile($id){

    $mysqli = $this->db-> getConnection();  

    if(isset($_FILES)) {
      $this->pic = new Pictures($mysqli);
      $picture = $this->pic->upload($_FILES['file'], "", "book", $id);
    }    
}

PICTURES.CLASS.php

namespace BC; 

class pictures {

  private $img_path = 'test/';
  private $m_conn;

  function __construct(&$mysqli_conn) {
    $this->m_conn = $mysqli_conn;
  }

  /*
   * Finishing the upload of an image and creates the nessecary rows in the
   * database.
   *
   * @param array $FILE Contains the element image_file which is an uploaded file
   * @param string $description Description of the image
   * @param string $type Which type should this belong to? Eg. periodical
   * @param int $foreign_key The index of the row this image belongs to
   *
   * @return boolean True if everything went fine, otherwise false.
   */

  public function upload($FILE, $description, $type, $foreign_key) {
    // Insert into database
    $SQL = 'INSERT INTO pictures (description) VALUE ("' . $description . '")';


    if(!$this->m_conn->query($SQL)) {
      return false;
    }

    // Get the id
    $id = $this->m_conn->insert_id;
    move_uploaded_file($_FILES['file']['tmp_name'], $this->img_path . $id . '.jpg');
    return $this->img_path . $id . '.jpg';


    // Create entry for it
    $SQL = 'INSERT INTO picture_connections (picture_id, type, foreign_key) VALUES (' . $id . ', "' . $type . '", ' . $foreign_key . ')';
    if(!$this->m_conn->query($SQL)) {
      return false;
    }
    return true;
  }

}

文件夹测试与pictures.class.php位于同一级别,并具有正确的权限。似乎无法想出这个。

1 个答案:

答案 0 :(得分:1)

这是路径问题!我通过更改private $img_path = 'test/';

解决了这个问题

到私人$img_path = '/Applications/MAMP/htdocs/project/test/';