如何在数据库中散列上传的文件名?
How to hash the uploaded file name in db?
我有一个 "profile picture" 系统,每个注册用户都可以更改他们的个人资料图片,这样图片就会显示出来。上传的过程是将图像上传到名为 pictures
的文件夹中,然后将文件名写入名为 "picture"
的数据库行,它工作正常,但我需要知道如何哈希上传图片的名字 所以如果2个人不同的图片,但是同名的图片会上传一个文件,就会有问题...
这是我的提交表单:
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="submit">
</form>
我有将文件上传到文件夹和数据库行的代码:
<?php
if(isset($_POST['submit'])){
move_uploaded_file($_FILES['file']['tmp_name'],"pictures/".$user_id.$_FILES['file']['name']);
$con = mysqli_connect("host","name","pass","db");
$q = mysqli_query($con,"UPDATE members SET picture = '".$_FILES['file']['name']."' WHERE username = '".$_SESSION['username']."'");
}
?>
这是在网站上显示图片的代码:
<?php
$con = mysqli_connect("host","name","pass","db");
$q = mysqli_query($con,"SELECT * FROM members WHERE username = '".$_SESSION['username']."'");
while($row = mysqli_fetch_assoc($q)){
if($row['picture'] == ""){
echo " <img src='pictures/public_icon.png' width='25' height='25'>"; *//This is default pic//*
} else {
echo " <img width='25' height='25' src='pictures/".$row['picture']."' alt='Profile Pic'>"; *//And this is pic from db row//*
}
}
?>
我想知道如何在上传的文件夹和数据库行中散列这张图片的名称...我可以做吗?我会用 db 的 "id" 和 "date" 行对其进行哈希处理,但我不知道如何......:/
改为使用此上传:
<?php
if(isset($_POST['submit'])){
/*
* create a unique file name with the current time and ip address of
* user, since it is highly unlikely to have the same file uploaded
*form the same ip address at the same time
*/
$unique_file_name = time(). $_SERVER['REMOTE_ADDR']. $_FILES['file']['name'];
//run query only if file upload and transfer succeeded
if( move_uploaded_file($_FILES['file']['tmp_name'],"pictures/".$user_id. $unique_file_name) ){
$con = mysqli_connect("host","name","pass","db");
$q = mysqli_query($con,"UPDATE members SET picture = '".$_FILES['file']['name']."' WHERE username = '".$_SESSION['username']."'");
}
}
?>
你可以试试这个
<?php
if(isset($_POST['submit'])){
$filename = $user_id.$_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'],"pictures/".$filename);
$con = mysqli_connect("host","name","pass","db");
$q = mysqli_query($con,"UPDATE members SET picture = '".$filename."' WHERE username = '".$_SESSION['username']."'");
} ?>
我有一个 "profile picture" 系统,每个注册用户都可以更改他们的个人资料图片,这样图片就会显示出来。上传的过程是将图像上传到名为 pictures
的文件夹中,然后将文件名写入名为 "picture"
的数据库行,它工作正常,但我需要知道如何哈希上传图片的名字 所以如果2个人不同的图片,但是同名的图片会上传一个文件,就会有问题...
这是我的提交表单:
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="submit">
</form>
我有将文件上传到文件夹和数据库行的代码:
<?php
if(isset($_POST['submit'])){
move_uploaded_file($_FILES['file']['tmp_name'],"pictures/".$user_id.$_FILES['file']['name']);
$con = mysqli_connect("host","name","pass","db");
$q = mysqli_query($con,"UPDATE members SET picture = '".$_FILES['file']['name']."' WHERE username = '".$_SESSION['username']."'");
}
?>
这是在网站上显示图片的代码:
<?php
$con = mysqli_connect("host","name","pass","db");
$q = mysqli_query($con,"SELECT * FROM members WHERE username = '".$_SESSION['username']."'");
while($row = mysqli_fetch_assoc($q)){
if($row['picture'] == ""){
echo " <img src='pictures/public_icon.png' width='25' height='25'>"; *//This is default pic//*
} else {
echo " <img width='25' height='25' src='pictures/".$row['picture']."' alt='Profile Pic'>"; *//And this is pic from db row//*
}
}
?>
我想知道如何在上传的文件夹和数据库行中散列这张图片的名称...我可以做吗?我会用 db 的 "id" 和 "date" 行对其进行哈希处理,但我不知道如何......:/
改为使用此上传:
<?php
if(isset($_POST['submit'])){
/*
* create a unique file name with the current time and ip address of
* user, since it is highly unlikely to have the same file uploaded
*form the same ip address at the same time
*/
$unique_file_name = time(). $_SERVER['REMOTE_ADDR']. $_FILES['file']['name'];
//run query only if file upload and transfer succeeded
if( move_uploaded_file($_FILES['file']['tmp_name'],"pictures/".$user_id. $unique_file_name) ){
$con = mysqli_connect("host","name","pass","db");
$q = mysqli_query($con,"UPDATE members SET picture = '".$_FILES['file']['name']."' WHERE username = '".$_SESSION['username']."'");
}
}
?>
你可以试试这个
<?php
if(isset($_POST['submit'])){
$filename = $user_id.$_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'],"pictures/".$filename);
$con = mysqli_connect("host","name","pass","db");
$q = mysqli_query($con,"UPDATE members SET picture = '".$filename."' WHERE username = '".$_SESSION['username']."'");
} ?>