njasm, on 15 November 2012 - 08:21 PM, said:
In this case, yes. You can assign a full path to your file attribute (path) cos you have it like text in your DB.
How else can I store a file in the DB apart from storing file separately and putting the path in the DB table?
And nope no luck. It won't accept me assigning just a name, with a file validator. It says "Path cannot be blank". It wants the CUploadedFile instance.
What I want to do is to process the step after this:
njasm, on 15 November 2012 - 08:21 PM, said:
maybe move image files to final folder, assign them a unique name that will identify them with a single and specific record and run the script.
into individual records. I directly upload them in a zip file with unique identifiable names.
For reference, here is the code that does this, though may not contain much additional info :
ProductController.php
public function actionBulkUpload() {
if(isset($_FILES['allphotos'])) {
$logfile = fopen("bulk.log","w");
$zipfile = CUploadedFile::getInstanceByName('allphotos');
$zipper = new ZipArchive();
$res = $zipper->open($zipfile->tempName);
if ($res == true) {
fputs($logfile,"Zip file opened.\n");
$path = $_SERVER['DOCUMENT_ROOT'] . '../' . 'tempDir/';
if(!is_dir($path))
mkdir($path, 0755);
$zipper->extractTo($path);
fputs($logfile,"Zip file extracted.\n");
$files = scandir($path);
foreach ($files as $i => $file) {
if ($file !== '.' && $file !== '..') {
$filepath = $path . '/' . $file;
$fileinfo = pathinfo($filepath);
$ext = $fileinfo['extension'];
$extsall = array('jpg', 'jpeg', 'gif', 'png');
if (in_array($ext, $extsall)) {
$names = explode('_', $file);
$prodid = $names[0];
$prodidd = intval($prodid);
if (strval($prodidd) == $prodid) {
$prod = Product::model()->findByPk($prodidd);
$fna = $names[1];
$prod->addFSImageFile($filepath,$fna);
fputs($logfile,"\t$file added to Product ID $prodidd\n");
}
else
fputs($logfile,"\t\t$file 's first part is not a pure number\n");
}
else
fputs($logfile,"\t\t$file does not have a permitted extension\n");
}
}
fputs($logfile,"All files are done being copied\n");
}
else
fputs($logfile,"Archive could not be extracted\n");
fclose($logfile);
}
$this->render('bulkupload',array('log' => @file_get_contents("bulk.log")));
@unlink("bulk.log");
}
ProductPhoto.php (model)
public function rules() {
return array(
array('productID, path', 'required'),
array('productID', 'numerical', 'integerOnly'=>true),
array('productID', 'safe', 'on'=>'search'),
array('path','file','types'=>'jpg, gif, png','allowEmpty'=>false),
);
}
Product.php (model)
public function addFSImageFile($path,$fna) {
$res = rename($path, $this->imagePathDir.$fna);
if ($res) {
$img->path = /*$this->imagePathDir.*/$fna;
$img = new ProductPhoto;
$img->productID = $this->ID;
if(!$img->validate())
print_r($img->errors);
$img->save();
}
}