move_uploaded_file не будет работать с ng-file-upload

Я пытаюсь загрузить изображения на сервер, а также вставить путь к базе данных. Вставка в базу данных работает нормально, и файл, похоже, находится в $_FILES после загрузки на стороне клиента (встроенный угловой). Но функция move_uploaded_file возвращает false и файл никуда не перемещается. В чем может быть проблема?

ВИД.html

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

КОНТРОЛЛЕР.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);
        });
      };

ПОЛЬЗОВАТЕЛЬ.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);
    }    
}

ИЗОБРАЖЕНИЯ.КЛАСС.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;
  }

}

Папка test находится на том же уровне, что и images.class.php, и имеет соответствующие разрешения. Кажется, я не могу понять это.


person Nirakander    schedule 10.06.2016    source источник
comment
где move_uploaded_file возвращает false? ты ничего не проверяешь. Я думаю, проблема в том, что вы возвращаетесь сразу после того, как переместили изображение. следующий код со вставкой никогда не достигается.   -  person Raphael Müller    schedule 10.06.2016
comment
@RaphaelMüller У меня был немного другой код, прежде чем я проверял, возвращает ли move_uploaded_file false или true. Я также попытался удалить возврат после, но без изменений.   -  person Nirakander    schedule 10.06.2016


Ответы (1)


Это была проблема пути! Я решил это, изменив private $img_path = 'test/';

в частный $img_path = '/Applications/MAMP/htdocs/project/test/';

person Nirakander    schedule 10.06.2016