Reputation: 163
I'm trying to upload some images with php and I have a problem with the rand function and the database. first I select an image and change her name (by adding some random numbers) befor moving it, after that I pick the new name with the directory and put it in th data base this is the code of the upload
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'] is the variable to put in the data base the code of upload class is
/**
*
*/
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;
}
}
?>
the problem is that i heave tow different value from the rand one for the name in the folder and the other in the database, although when i echo the rand and the query just before the update the value is the same
value of the rand -> 9035
name of the image in the folder 903529-01-2015_a.jpg
the same value here -> UPDATE users SET `profile_pic` = 'resources/uploads/profiles/903529-01-2015-22-46-01_a.jpg' WHERE id = 13
but in the database i have a different value
for example: resources/uploads/profiles/14729-01-2015-22-46-01_a.jpg
where is the problem ??
the table of users updated
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;
Upvotes: 0
Views: 283
Reputation: 177
U can use uniqid();
instead of rand(min,max);
better solution or ad with time();
a timestamp
;) or count the fils by ur self: if only numbred files in folder $nextIndex = count(scandir(FOLDER))-1;
Upvotes: 1