gpt4 book ai didi

php - php中rand函数的问题

转载 作者:行者123 更新时间:2023-11-29 12:26:31 24 4
gpt4 key购买 nike

我正在尝试使用 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 和查询时,该值是相同的兰特值 -> 9035

文件夹中图像的名称903529-01-2015_a.jpg

此处的值相同 -> 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;

最佳答案

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

;)或者自己数一下 fils:如果文件夹中只有编号的文件$nextIndex = count(scandir(FOLDER))-1;

关于php - php中rand函数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28225761/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com