<?php
class UploadsController extends AppController {
var $name = 'Uploads';
var $components = array('Auth');
var $uses = array('Upload');
function beforeFilter() {
$this->Auth->allow('*');
}
function upload($event) {
App::import('Vendor', 'UploadedFiles', array('file' => 'UploadedFiles.php'));
$user = $this->Auth->user('id');
$this->set('user', $user);
if(!$this->Auth->user()) { $this->Session->setFlash(__('Please login.', true)); }
echo $user;
echo $event;
$vardir = date('d-m-Y');
$dir = 'img/gallery/'.$vardir.'/';
$thmbdir = 'img/gallery/'.$vardir.'/thumbnails/';
if(!is_dir($dir)) {
mkdir($dir, 0777);
mkdir($thmbdir, 0777);
}
$galleryPath = $dir;
$absGalleryPath = realpath($galleryPath) . '/';
$absThumbnailsPath = realpath($galleryPath . 'thumbnails/') . '/';
//Iterate through uploaded data and save the original file, thumbnail, and description.
while(($file = UploadedFiles::fetchNext()) !== null) {
$fileName = $file->getSourceFile()->getSafeFileName($absGalleryPath);
$file->getSourceFile()->save($absGalleryPath . '/' . $fileName);
$thumbFileName = $file->getThumbnail(1)->getSafeFileName($absThumbnailsPath);
$file->getThumbnail(1)->save($absThumbnailsPath . '/' . $thumbFileName);
$this->Upload->create();
$this->Upload->set(array(
'name' => $absGalleryPath . $fileName,
'width' => $file->getSourceFile()->getWidth(),
'height' => $file->getSourceFile()->getHeight(),
'description' => $file->getDescription(),
'event_id' => $event,
'user_id' => $user
));
$this->Upload->save();
}
}
}
데이터베이스에 저장하려고하는 코드의 마지막 부분을 확인하십시오. $ event와 $ users는 비어 있기 때문에 저장하지 않습니다. 그러나 내가 그들을 에코 할 때 (라인 17과 18), 올바른 값으로 페이지에 나타납니다. while 루프에서 '지워진'것으로 보인다 ... 'event_id' => '123'
및 'user_id' => '123'
에 대한 더미 데이터를 넣으면 스크립트가 성공적으로 저장됩니다. 그러나 변수를 사용하면 저장되지 않습니다. 내 변수은 어떻게됩니까? 어떤 생각?CakePHP 변수가 비어집니다.
감사합니다.
값을 문자열로 캐스팅하면이 가설을 확인할 수 있습니다 :'event_id '=> (string) $ event, user_id'=> (string) $ user' –
Hi Tor, 모델 배열에 오류가 없습니다. '123'과 123 모두 저장됩니다. $ event 변수를 어떻게 지울 수 있는지 이해하지 못하겠습니까? – octavian
안녕하세요 Ewan, 시도하고 빈 필드를 삽입합니다. 다시 말하지만, $ 이벤트가 비어 있음을 의미하지만, 17 행에 11042를 표시하면 ... – octavian