2016-06-10 2 views
0

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

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

} 

Тестирование папки находится на том же уровне, что и picture.class.php, и имеет права доступа. Кажется, это не похоже на это.

+0

где move_uploaded_file возвращение ложным? вы ничего не проверяете. я думаю, проблема в том, что вы возвращаетесь сразу после того, как вы переместили свой образ. следующий код со вставкой никогда не достигается. –

+0

@ RaphaelMüller У меня был немного другой код до того, где я тестировал, если move_uploaded_file возвращал false или true. Я также попытался удалить возвращение после, но никаких изменений. – Nirakander

ответ

1

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

частному $img_path = '/Applications/MAMP/htdocs/project/test/';

Смежные вопросы