我必须为上传的文件分配随机值。这是我的组件代码n控制器代码,我调用组件,但它正在创建一个芽,所以需要一个帮助
//component
App::uses('Component', 'Controller');
$components = array('Useful');
class UsefulComponent extends Component {
/*generate random numbers*/
public function random_code() {
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;
while ($i <= 10) {
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
}
//contrroller
$random_number = $this->Useful->random_code();
$random_number.$this->request->data['User']['profile_photo']['tmp_name'];
答案 0 :(得分:2)
php tempnam
中有唯一文件名的就绪函数$uniqueFilePath = tempnam("/mydir", "file_prefix");
$uploadedFile = $this->request->data['User']['profile_photo']['tmp_name'];
move_uploaded_file($uploadedFile, $uniqueFilePath);
答案 1 :(得分:1)
尝试uniqid():
<?php
$name = uniqid();
$tmp_name = $_FILES["pictures"]["tmp_name"];
move_uploaded_file($tmp_name, $name);
?>