重命名上传文件,将新名称发布到数据库

时间:2012-01-28 04:00:47

标签: php mysql upload rename

我正在使用以下代码上传和重命名文件。该部分工作得很棒,但它也会将一些数据发布到db表。

问题是旧名称是否已发布到数据库,但文件正在重命名为ID ...如何将新名称输入数据库?

在此先感谢我的代码:

 <?php

//This is the directory where images will be saved
$allowed_filetypes = array('.jpg','.pdf','.xlsx','.xls','.doc','.docx','.ppt','.pptx','.jpeg','.png','.gif','.pdf');
$max_filesize = 52428800; // max file size = 50MB
$target = $target . basename( $_FILES['document']['name']);


//This gets all the other information from the form
$billing_id=$_POST['billing_id'];
$shipping_id=$_POST['shipping_id'];
$file_name=$_POST['file_name'];
$file_type=$_POST['file_type'];
$file_description=$_POST['file_description'];

        $file = $_FILES['document']['name']; // Get the name of the file (including file extension).
        $ext = substr($file, strpos($file,'.'), strlen($file)-1);
        if(!in_array($ext,$allowed_filetypes))//check if file type is allowed
            die('The file extension you attempted to upload is not allowed.'); //not allowed
        if(filesize($_FILES['document']['tmp_name']) > $max_filesize) //check that filesize is less than 50MB
            die ('The file you attempted to upload is too large, compress it below 50MB.');


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

//Writes the information to the database
mysql_query("INSERT INTO customer_files (billing_id, shipping_id, file_name, file_type, file_description, file)
VALUES ('$billing_id', '$shipping_id', '$file_name', '$file_type', '$file_description', '$target')") ;

$target = "../../file_management/uploads/customers/" .mysql_insert_id() . $ext; 

//Writes the file to the server
if(move_uploaded_file($_FILES['document']['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.";
}
?>

2 个答案:

答案 0 :(得分:1)

新的“名称”已经在数据库中 - 它是您插入上传数据时创建的记录的主键:

$target = "../../file_management/uploads/customers/" .mysql_insert_id() . $ext;
                                                      ^^^^^^^^^^^^^^^^^ the new filename

答案 1 :(得分:1)

在重命名文件之前,您正在将值插入数据库。您必须在代码中进行更改。首先在数据库中插入计费和发货ID,然后取最后插入的ID,使用最后一个插入ID重命名该文件,并在数据库中更新新名称。将您的代码更改为:

<?php

   //This is the directory where images will be saved
   $allowed_filetypes =array('.jpg','.pdf','.xlsx','.xls','.doc','.docx','.ppt','.pptx','.jpeg','.png','.gif','.pdf');
   $max_filesize = 52428800; // max file size = 50MB
   $target = $target . basename( $_FILES['document']['name']);


   //This gets all the other information from the form
  $billing_id=$_POST['billing_id'];
  $shipping_id=$_POST['shipping_id'];
  $file_name=$_POST['file_name'];
  $file_type=$_POST['file_type'];
  $file_description=$_POST['file_description'];

    $file = $_FILES['document']['name']; // Get the name of the file (including file extension).
    $ext = substr($file, strpos($file,'.'), strlen($file)-1);
    if(!in_array($ext,$allowed_filetypes))//check if file type is allowed
        die('The file extension you attempted to upload is not allowed.'); //not allowed
    if(filesize($_FILES['document']['tmp_name']) > $max_filesize) //check that filesize is less than 50MB
        die ('The file you attempted to upload is too large, compress it below 50MB.');


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

    //Writes the information to the database
   mysql_query("INSERT INTO customer_files (billing_id, shipping_id) VALUES ('$billing_id', '$shipping_id')") ;

  $target = "../../file_management/uploads/customers/" .mysql_insert_id() . $ext; 

  $last_id = mysql_insert_id();
  $new_file_name = mysql_insert_id() . $ext;

  mysql_query("UPDATE customer_files SET file_name='$new_file_name',file_type='$file_type',file_description='$file_description',file='$target' WHERE id=$last_id");


//Writes the file to the server
if(move_uploaded_file($_FILES['document']['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.";
}
?>

希望这有帮助