将更改的文件名存储到mysql字段中

时间:2011-08-02 07:27:05

标签: php mysql file-upload

我有一个表单,其中用户可以上传图像,这些图像保存在服务器中的特定文件夹中。同时,照片的文件名存储在mysql表的特定字段中。

我尝试通过添加时间戳更改文件名(并且它有效)但是这个新文件名不是存储在mysql字段中的文件名,这意味着它存储了用户的原始照片文件名。

是否有一种方法可以将新文件名存储在mysql表中。

以下是我正在使用的代码:

enter code here

//This is the directory where images will be saved 
 $target = "pics/"; 
 $target = $target. basename( $_FILES['photo']['name']); 


 //This gets all the other information from the form 
 $name=$_POST['name']; 
 $email=$_POST['email']; 
 $phone=$_POST['phone']; 
 $pic=($_FILES['photo']['name']); 

 // Connects to your Database 
 mysql_connect("localhost", "username", "password") or die(mysql_error()) ; 
 mysql_select_db("test") 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'], $target)) 
 { 

 //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."; 
 } 
 ?> 

我希望将文件重命名为独特的文件,并且我希望将唯一的文件名存储在我的sql表的照片字段中。

希望你能帮助我

3 个答案:

答案 0 :(得分:1)

在插入之前,将新文件名保存到变量中,然后更改名称,最后将新名称插入数据库。

或者,如果要在完成插入后更改名称,请在表上运行UPDATE查询,在其中更新文件名: UPDATE表SET filename ='new_filename'WHERE filename ='old_filename“

答案 1 :(得分:0)

您必须为此创建一个系统,因为每次更改文件名时服务器都无法决定进行mysql查询。

您必须拥有某种管理员,您可以在目录中显示所有文件,并执行一个更改文件名和mysql行的命令。

答案 2 :(得分:0)

下面是我刚刚编写的一些代码,它会计算当前系统中有多少文件并将其递增一个因此使其成为唯一的file_name,如果每个文件都是例如“user_1.jpg”等等。< / p>

    // Connects to your Database 
 mysql_connect("localhost", "username", "password") or die(mysql_error()) ; 
 mysql_select_db("database") or die(mysql_error()) ; 

 //handle the form information
$name=$_POST['name']; 
$email=$_POST['email']; 
$phone=$_POST['phone'];

 //handle our file
$photo = $_FILES['photo']['tmp_name'];
//set where the file is ment to go
$target = "pics/"; 
//get the base name of the file
$filename = basename($photo);
//get the extension of the requested file
$extension = substr($filename, strrpos($filename, '.') + 1);
//count the length of items in the string
$stringLength = strlen(basename($photo));
//Get the file path of the photo minus the name of the photo
$filePath = substr($photo, 0, - $stringLength);
//set the inital name of the file
 $PhotoName = "username.jpg";
//set the full path and name of the file to be uploaded
$checkThisFile = $target . $PhotoName;

//count how many files are in the folder and increment it by 1 in the while loop
$filecount = count(glob($target. "*.jpg"));

//while the file can be found in the system
while(file_exists($checkThisFile)){

    //increment the count till the file is unquie
    $filecount++;
    //change the name of the file
    $PhotoName = "username_$filecount.jpg";
    //set the target and name of the file to be uploaded to keep testing it through our loop
    $checkThisFile = $target . $PhotoName;

    //if the file is not in the system then
    if(!file_exists($checkThisFile)) {

        //break the while loop
        break;

    } //end if condition

} //end while loop

    //rename the photo file from the temporary name to the full name of the file
    if($renamedFile = rename("$photo", "$checkThisFile")) {
        //print a success tha our file was renamed
        echo "<br/>the file was renamed";
        //check if our renamed file exists
        if(!empty($renamedFile)) {
            //print a success that our file is there
            echo "<br/>The renamed file exists!";
            //move the uploaded file!
            if(move_uploaded_file($renamedFile, $target)) {
                //print there was an error with the transfer
                echo "cannot move files!";
            } //end he if condition
            else {
                //write to the database
                $MyDatabaseQuery = "INSERT INTO employees VALUES ('','$name', '$email', '$phone', '$PhotoName')";
                //run the query
                @mysql_query($MyDatabaseQuery);
            } //end else condition
        } //end if condition
    } //end if condition

代码的重点是继续在循环中运行,直到文件不在系统中,然后重命名临时文件并上传它然后只存储文件+扩展名在数据库中的任何其他记录你想要存储。我希望这会有所帮助。

这也是MySQL创建TABLE脚本

    CREATE TABLE IF NOT EXISTS `employees` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` text NOT NULL,
  `email` text NOT NULL,
  `phone` text NOT NULL,
  `picture` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;