Tôi đã viết những mã để tải lên một tập tin và lưu đường dẫn trong cơ sở dữ liệu sử dụng thông số liên kết
$model->attributes=$_POST['Form'];
$uploadedFile=CUploadedFile::getInstance($model,'resume');
$path = '/protected/upload/'.time().$uploadedFile;
if($model->validate()){
$connection=Yii::app()->db;
$filePath=Yii::app()->basePath .DIRECTORY_SEPARATOR.'..'.$path; // file upload location
$result = $uploadedFile->saveAs($filePath); //upload file
// an SQL with placeholders
$hobbies=implode(",",$model->hobbies); // comma deliminated string of hobbies
$dob =date('Y-m-d',strtotime($model->dob)); // convert date
$status=1; // status of record
$sql='INSERT INTO student (name, dob, email, gender, hobbies, city, resume, message,status, created_date,modified_date) VALUES(:name,:dob,:email,:gender,:hobbies,:city,:resume,:msg,:status,now(),now())';
$command=$connection->createCommand($sql);
// replace the placeholder with the actual username value
$command->bindParam(":name",$model->name,PDO::PARAM_STR);
$command->bindParam(":dob",$dob,PDO::PARAM_STR);
$command->bindParam(":email",$model->email,PDO::PARAM_STR);
$command->bindParam(":gender",$model->gender,PDO::PARAM_INT);
$command->bindParam(":hobbies",$hobbies,PDO::PARAM_STR);
$command->bindParam(":city",$model->city,PDO::PARAM_INT);
$command->bindParam(":resume",$path,PDO::PARAM_STR);
$command->bindParam(":msg",$model->msg,PDO::PARAM_STR);
$command->bindParam(":status",$status,PDO::PARAM_INT);
$result=$command->execute();
}
Tôi hy vọng các thông số này hữu ích cho bạn.
Chỉ cần thêm số trong trường hợp bạn cần. Thật đơn giản. Đi trước, bạn có thể làm cho nó xảy ra! – hakre
@hakre Tôi có nghĩa là tôi cần phải thêm số chỉ khi tệp có cùng tên tồn tại trong thư mục đó. Tôi chỉ yêu cầu nếu có bất kỳ chức năng được xây dựng trong để xử lý đó. Nếu không, tôi sẽ phải làm một file_exists() kiểm tra và lặp qua tên tập tin với các số cho đến khi một tên tập tin hợp lệ đạt được. Nếu Yii có thể xử lý điều đó thì nó sẽ rất tuyệt. – ajaybc
Sau đó kiểm tra tài liệu API của YII những gì nó cung cấp. Bạn đã hoàn thành chưa? – hakre