简体   繁体   English

如果名称已经存在,如何重命名文件?

[英]How to rename a file if the name already exists?

I am using the following script.我正在使用以下脚本。 it basically checks if the name of the file being uploaded already exists, if it does it should rename it to something else and upload it.它基本上检查正在上传的文件的名称是否已经存在,如果存在,则应将其重命名为其他名称并上传。 So far it doesn't work.到目前为止,它不起作用。 either it renames all files or when i try to open the file via webpage it says corrupt file.要么它重命名所有文件,要么当我尝试通过网页打开文件时,它说文件损坏。

Code:代码:

$sql="SELECT filename FROM doc_u WHERE person_id= '$pid'";  

    $result=mysql_query($sql);

    $query = mysql_query($sql) or die ("Error: ".mysql_error());

 if ($result == "")
 {
 echo "";
  }
   echo "";


    $rows = mysql_num_rows($result);

    if($rows == 0)
    {
   print("");

    }
    elseif($rows > 0)
    {
   while($row = mysql_fetch_array($query))
    {

   $existing = $row['filename'];

   print("");
   }

   }

     if ( $filename === $existing ) {
$filename = $uniqueidgenerator.strrchr($_FILES['filename']['name'], ".");

    } else {
     $filename = $_FILES['filename']['name'];;
    }

//After checking it will move the files

      if(move_uploaded_file($_FILES['filename']['tmp_name'],$upload_path . $filename))
     echo ''; 
  else
     echo ''; 

Given you're using a database already, let me say it asplainly as possible: NEVER USE A USER-PROVIDED FILENAME.鉴于您已经在使用数据库,让我尽可能简单地说:永远不要使用用户提供的文件名。 Instead, store each uploaded file by its corresponding DB record's primary key ID number (you are using auto-incrementing integers for ids, right?).相反,通过其对应的数据库记录的主键 ID 号存储每个上传的文件(您正在使用自动递增的整数作为 id,对吗?)。 Store the file's name in the database, and now you can have as many "text.txt" files as you want, because each actual file will be named "1", "53", and "207", etc...将文件名存储在数据库中,现在您可以拥有任意数量的“text.txt”文件,因为每个实际文件将被命名为“1”、“53”和“207”等...

There isn't much code to go on here. go 这里没有太多代码。 I suspect that $filename isn't initialized properly.我怀疑$filename没有正确初始化。 So in your if ( $filename === $existing ) that is really saying if(undefined === undefined) which would always be true.因此,在您的if ( $filename === $existing )中,这确实是在说if(undefined === undefined)始终是正确的。

I don't see why the files would be corrupted.我不明白为什么文件会损坏。 That could be a different issue entirely.这可能是一个完全不同的问题。

Also, watch out for your sql statement there.另外,请注意那里的 sql 声明。 That is an SQL injection waiting to happen.那是等待发生的 SQL 注入。 It would be a lot better if you used prepared statements or at the very least use mysql_escape_string .如果您使用准备好的语句或至少使用mysql_escape_string会好得多。 This has been deprecated of course in favor of prepared statements.当然,这已被弃用,有利于准备好的陈述。

A better workflow would be to insert a row in the db first with the file's meta data then rename the file using the primary key you got back.更好的工作流程是首先使用文件的元数据在数据库中插入一行,然后使用您返回的主键重命名文件。 It is guaranteed to be unique always.它保证始终是唯一的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM