アプリでアップロードを処理できるようにしようとしています。これは、DealsController.php の add アクションです。
<?php
public function add() {
if ($this->request->is('post')) {
$this->request->data['Deal']['user_id'] = $this->Auth->user('id');
$this->Deal->create();
if ($this->Deal->uploadFile($this->request->data['Deal']['pic'])){
$file = $this->request->data['Deal']['pic'];
$this->request->data['Deal']['pic'] = $this->request->data['Deal']['pic']['name'];
if ($this->Deal->save($this->request->data)) {
$this->Session->setFlash(__('Your deal has been saved. %s', h(serialize($file)) ));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to add your deal. %s', h(serialize($file)) ));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to upload the file. Please try again. %s', h(serialize($this->request->data['Deal']['pic']))));
}
}
uploadFile
関数は、Deal.php のモデルで定義されています。
<?php
public function uploadFile( $check ) {
$uploadData = array_shift($check);
if ( $uploadData['size'] == 0 || $uploadData['error'] !== 0) {
return false;
}
$uploadFolder = 'files'. DS .'your_directory';
$fileName = time() . '.pdf';
$uploadPath = $uploadFolder . DS . $fileName;
if( !file_exists($uploadFolder) ){
mkdir($uploadFolder);
}
if (move_uploaded_file($uploadData['tmp_name'], $uploadPath)) {
$this->set('pic', $fileName);
return true;
}
return false;
}
`uploadFile` keeps returning false and I get my
ファイルをアップロードできません。もう一度やり直してください。a:5:{s:4:"名前";s:7:"012.PDF";s:4:"タイプ";s:15:"アプリケーション/pdf";s:8:"tmp_name";s :14:"/tmp/phpw0uVGS";s:5:"エラー";i:0;s:4:"サイズ";i:70118;}
閃光。ファイルが実際に一時ディレクトリにアップロードされたように見えるため、これは紛らわしいです。さまざまなパスを試しましmove_uploaded_file
たが、何も機能しませんでした。これはファイルのアクセス許可と関係がありますか? 私のアップロード ファイルは約 80 KB しかなかったので、ファイル サイズにはなりませんでした。