mazraara
(Mazraara)
May 28, 2012, 8:00am
1
Hi,
I have a problem with changing the name of uploaded image.
its been possible to change it when saving in a folder. but when it comes to database, its not allowing me to add path + image.jpg to database.
now what happens with my code is, it saves the file or image in two places…
insde a folder and database.
inside folder, i can change the name and save… for example… image ABC.jpg is saved as…
uploadFolder/05-28-12-11-22-426610-ABC.jpg
but the same image is saved in database as just ABC.jpg with out the path and random numbers. please help me out with this problem. tnx
the code is as follows,
$random_no = rand(0000, 9999);
$date = date(‘mdyGis-’);
$model->pro_picture = CUploadedFile::getInstance($model, ‘pro_picture’);
if ($model->validate() && $model->save()) {
$model->pro_picture->saveAs('C:\xampp\htdocs\UserManagement\upload\members' . DIRECTORY_SEPARATOR . $date.$random_no. $model->pro_picture);
}
mazraara
(Mazraara)
May 28, 2012, 8:11am
2
can somebody help me pls.
$file=CUploadedFile::getInstance($model,'photo_data')
$model->photo_file_name=$file->name;
emduck
(Em)
May 28, 2012, 9:18am
5
I’m a bit confused by your code. You write
$model->pro_picture->saveAs(...);
However, isn’t $model an instance of CActiveRecord? And isn’t pro_picture the column in your database?
If so, then something like the code below should work. (Note that I use date(‘Y-m-d’) so that, when you run “ls”, the files are shown form oldest to newest).
$uploadedFile = CUploadedFile::getInstance($model, 'pro_picture');
$extension = strtolower($uploadedFile->extensionName);
$now = date('Y-m-d-H-i-s');
$randomNum = rand(0000, 9999);
$fileName = "{$now}-{$randomNum}.{$extension}"; // E.g. 2012-05-28-13-28-18-9988.png
$fullPath = 'C:\xampp\htdocs\UserManagement\upload\members' . DIRECTORY_SEPARATOR . $fileName;
$model->pro_picture = $fullPath;
if ($model->validate() && $model->save()) {
$uploadedFile->saveAs($fullPath);
}
p.s. Perhaps unrelated, but, if it were me, I’d rather generate the filename as a function of the user’s id, plus a random number for security, rather than the date. That way, I know instantly which user an image corresponds to. But perhaps you have a reason for having the filename be a function of the date and time?
mazraara
(Mazraara)
May 28, 2012, 10:01am
6
this way, its not allowing. Property "CUploadedFile.name" is read only.
mazraara
(Mazraara)
May 28, 2012, 10:13am
7
I’m a bit confused by your code. You write
$model->pro_picture->saveAs(...);
However, isn’t $model an instance of CActiveRecord? And isn’t pro_picture the column in your database?
If so, then something like the code below should work. (Note that I use date(‘Y-m-d’) so that, when you run “ls”, the files are shown form oldest to newest).
$uploadedFile = CUploadedFile::getInstance($model, 'pro_picture');
$extension = strtolower($uploadedFile->extensionName);
$now = date('Y-m-d-H-i-s');
$randomNum = rand(0000, 9999);
$fileName = "{$now}-{$randomNum}.{$extension}"; // E.g. 2012-05-28-13-28-18-9988.png
$fullPath = 'C:\xampp\htdocs\UserManagement\upload\members' . DIRECTORY_SEPARATOR . $fileName;
$model->pro_picture = $fullPath;
if ($model->validate() && $model->save()) {
$uploadedFile->saveAs($fullPath);
}
p.s. Perhaps unrelated, but, if it were me, I’d rather generate the filename as a function of the user’s id, plus a random number for security, rather than the date. That way, I know instantly which user an image corresponds to. But perhaps you have a reason for having the filename be a function of the date and time?
tnx alot Emily… this helped
I think just need to save a different name …
then use standard path in above code like
$_SERVER['DOCUMENT_ROOT'].Yii::app()->request->baseUrl.'/patients/'...;