我的主要目标是将文件夹中的每个上传图片保存为具有唯一名称。我这样做但是无论它有什么名称,在DB中它仍然有旧的原始名称,我怎么能在DB中保存相同的uniqe名称? 我的代码是:
<?php
if(isset($_SESSION['username'])){
if(isset($_POST['upload'])) {
$allowed_filetypes = array('.jpg','.jpeg','.png','.gif');
$max_size = "100000"; /// 1 Мегабайт
$max_height = "90000"; /// 300 Килобайта
$max_width = "90000"; /// 300 Килобайта
$upload_path = 'gallery/';
$random = md5(time());
$filename = $_FILES['userfile']['name'];
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
if(!in_array($ext,$allowed_filetypes))
die('The file you attempted to upload is not allowed.');
if(filesize($_FILES['userfile']['tmp_name']) > $max_size)
die('Прекалено е тежка снимката.');
if(filesize($_FILES['userfile']['tmp_name']) > $max_height)
die('Прекалено е голяма снимка във височина.');
if(filesize($_FILES['userfile']['tmp_name']) > $max_width)
die('Прекалено е голяма снимка във височина.');
if(!is_writable($upload_path))
die('You cannot upload to the specified directory, please CHMOD it to 777.');
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path .$random . $filename)) {
$q = mysqli_query($connection,"UPDATE users SET avatar='".$_FILES['userfile']['name']."' WHERE username ='".$_SESSION['username']."'");
echo "<font color='#5cb85c'>Браво, успешно си качил/а профилна снимка!</font>";
} else {
echo 'There was an error during the file upload. Please try again.';
}
}
echo ' <form action="images.php" method="post" enctype="multipart/form-data"> ';
echo ' <input type="file" name="userfile"/>';
echo ' <input type="submit" name="upload" value="Качи">';
echo ' </form>';
}
else
{
echo "<font color='#ec3f8c'>Съжелявам! За да качиш снимка във профила си, <a href='login.php'><font color='#ec3f8c'><b> трябва да се логнеш</b> </font></a></font>";
}
?>
有人可以解释一下,我还有什么要补充的?
答案 0 :(得分:0)
如果我理解了该问题,图像将以正确的唯一名称保存在文件系统中。那是$upload_path .$random . $filename
,但数据库中的引用是错误的。
只需替换查询
UPDATE users SET avatar='".$_FILES['userfile']['name'].
与
UPDATE users SET avatar='".$upload_path .$random . $filename
HTH, 马塞罗