php中rand函数的问题

时间:2015-01-29 22:21:21

标签: php mysql

我正在尝试使用php上传一些图像,我对rand函数和数据库有问题。 首先我选择一个图像并更改她的名字(通过添加一些随机数字),然后移动它,之后我用目录选择新名称并将其放入数据库中 这是上传的代码

if (!empty($_FILES['profile-pic']['name'][0])) {
        $file = $_FILES['profile-pic'];
        $allowedExt = array('jpg', 'jpeg', 'png');
        $uploadsDirectory = 'resources/uploads/profiles/';
        $maxSize = 4000000;
        $upload = new Upload($file, $allowedExt, $uploadsDirectory, $maxSize);
        $uploadFile = $upload->uploadFiles();
        $data['profile_pic'] = $uploadsDirectory .  $upload->getFileUrl();
}

$ data ['profile_pic']是放入数据库的变量 上传类的代码是     

/**
 * 
 */
class Upload {

private $files;
private $allowedExt;
private $uploadsDirrectory;
private $maxSize;
private $fileUrl;
private $filesName = array();
private $fileDimens = array();

function __construct($files, $allowedExt, $uploadsDirectory, $maxSize) {

    if (is_array($allowedExt) AND is_int($maxSize)) {
        $this->files = $files;
        $this->allowedExt = $allowedExt;
        $this->uploadsDirrectory = $uploadsDirectory;
        $this->maxSize = $maxSize;
    } else {
        echo "file extension must be an array and max size must be integer value";
    }
}

function uploadFiles() {

    $file = $this->files;
    $allowedExt = $this->allowedExt;
    $uploadsDir = $this->uploadsDirrectory;
    $maxSize = $this->maxSize;


    for ($i = 0; $i < count($file['name']); $i++) {
        $errors = array();
        $filename = $file['name'][$i];
        $fileex = explode('.', $filename);
        $extension = strtolower(array_pop($fileex));
        $size = $file['size'][$i];
        $tmpname = $file['tmp_name'][$i];

        if (in_array($extension, $allowedExt) === FALSE) {
            $errors[] = 'File extension not allowed';
        }

        if ($size > $maxSize) {
            $errors[] = 'File size must be less then ".$maxSize." KB';
        }

        if (empty($errors)) {
            $rand = rand(0, 9999);
            $this->fileUrl = $rand.date('d-m-Y') . $filename;
            $destination = ADMIN . $uploadsDir . $this->fileUrl;
            move_uploaded_file($tmpname, $destination);
            $this->filesName[] = $this->fileUrl;
        } else {
            foreach ($errors as $error) {
                echo $error . "<br />";
            }
        }
    } // end for loop
    return true;
}

// end function



function getFileUrl() {
    return $this->fileUrl;
}

function getFilesName() {
    return $this->filesName;
}

}

?>

问题是,我在文件夹中的名称和数据库中的另一个名称中使用rand一个拖出不同的值,尽管当我在更新之前回显rand和查询时,值是相同的 兰特的价值 - &gt; 9035

文件夹903529-01-2015_a.jpg

中图片的名称

此处的值相同 - &gt; UPDATE users SET `profile_pic` = 'resources/uploads/profiles/903529-01-2015-22-46-01_a.jpg' WHERE id = 13

但在数据库中我有不同的价值 例如:resources/uploads/profiles/14729-01-2015-22-46-01_a.jpg

问题在哪里?

用户表已更新

CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL,
`username` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
`email` varchar(150) NOT NULL,
`adresse` varchar(255) NOT NULL,
`telephone` varchar(25) NOT NULL,
`profile_pic` varchar(255) CHARACTER SET utf8 NOT NULL DEFAULT   'resources/uploads/profile.png',
`commentStatus` int(11) NOT NULL DEFAULT '1',
`group` int(11) NOT NULL DEFAULT '2'
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=latin1;

1 个答案:

答案 0 :(得分:1)

您可以使用uniqid();代替rand(min,max);更好的解决方案或广告time();时间戳

) 或者通过你自己计算fils: 如果文件夹中只有numbred文件 $ nextIndex = count(scandir(FOLDER)) - 1;