【发布时间】:2014-03-07 08:00:12
【问题描述】:
我正在学习如何使用唯一 id 重命名上传的图片(或文件),使其与其他上传的文件不同;将其保存到数据库中,以便可以将其称为用户图像并将其保存在图像文件夹中。
我的问题是,我已经知道如何用唯一的文件名重命名和保存图像,但我不知道如何将唯一的文件名保存在数据库中,所以它调用了那个确切的图像。它似乎将“数组”保存到数据库而不是唯一的文件名。
如果有人对我如何纠正这个问题有任何想法,我将非常感谢学习经验哈哈:
<?php
//This is the directory where images will be saved
//This gets all the other information from the form
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$pic=pathinfo($_FILES["photo"]["name"]);
// Connects to your Database
mysql_connect("businessdb1.db.9878324.hostedresource.com", "user", "password") or die(mysql_error()) ;
mysql_select_db("businessdb1") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$pic')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES["photo"]["tmp_name"],
"avatars/" . uniqid() . '.' . $pic['extension']))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
【问题讨论】:
-
重命名后为什么不保存?
-
pathinfo()返回一个数组 -
尝试用
var_dump($pic)探索$pic,它是一个关联数组,这就是你得到Array的原因。此外,此代码也不安全,因为您直接插入用户数据,更糟糕的是,您使用了已弃用的mysql_*函数。
标签: php mysql arrays database unique