dongxian1699 2016-06-10 09:11
浏览 38

move_uploaded_file不适用于ng-file-upload

Im trying to upload pictures to a server and also insert the path to a database. The insertion to the db is working fine and the file seems to be found in $_FILES after upload on client-side (built in angular). But the move_uploaded_file function is returning false and the file is not moving anywhere. What could be the problem?

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

}

The folder test is placed within the same level as pictures.class.php and has the right permissions. Can't seem to figure this one out.

  • 写回答

1条回答 默认 最新

  • douchuza8908 2016-06-10 10:57
    关注

    This was a path issue! I solved this by changing private $img_path = 'test/';

    to private $img_path = '/Applications/MAMP/htdocs/project/test/';

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!