• <i id='sUayf'><tr id='sUayf'><dt id='sUayf'><q id='sUayf'><span id='sUayf'><b id='sUayf'><form id='sUayf'><ins id='sUayf'></ins><ul id='sUayf'></ul><sub id='sUayf'></sub></form><legend id='sUayf'></legend><bdo id='sUayf'><pre id='sUayf'><center id='sUayf'></center></pre></bdo></b><th id='sUayf'></th></span></q></dt></tr></i><div id='sUayf'><tfoot id='sUayf'></tfoot><dl id='sUayf'><fieldset id='sUayf'></fieldset></dl></div>

    <small id='sUayf'></small><noframes id='sUayf'>

  • <tfoot id='sUayf'></tfoot>

        <legend id='sUayf'><style id='sUayf'><dir id='sUayf'><q id='sUayf'></q></dir></style></legend>

        • <bdo id='sUayf'></bdo><ul id='sUayf'></ul>
      1. 重命名文件,如果已经存在 - php上传系统

        时间:2023-12-02
          <bdo id='hXY3N'></bdo><ul id='hXY3N'></ul>
        • <i id='hXY3N'><tr id='hXY3N'><dt id='hXY3N'><q id='hXY3N'><span id='hXY3N'><b id='hXY3N'><form id='hXY3N'><ins id='hXY3N'></ins><ul id='hXY3N'></ul><sub id='hXY3N'></sub></form><legend id='hXY3N'></legend><bdo id='hXY3N'><pre id='hXY3N'><center id='hXY3N'></center></pre></bdo></b><th id='hXY3N'></th></span></q></dt></tr></i><div id='hXY3N'><tfoot id='hXY3N'></tfoot><dl id='hXY3N'><fieldset id='hXY3N'></fieldset></dl></div>

          <small id='hXY3N'></small><noframes id='hXY3N'>

            <tfoot id='hXY3N'></tfoot>

                <tbody id='hXY3N'></tbody>
              <legend id='hXY3N'><style id='hXY3N'><dir id='hXY3N'><q id='hXY3N'></q></dir></style></legend>

                1. 本文介绍了重命名文件,如果已经存在 - php上传系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我这个 PHP 代码:

                  I this PHP code:

                  <?php
                  
                  // Check for errors
                  if($_FILES['file_upload']['error'] > 0){
                      die('An error ocurred when uploading.');
                  }
                  
                  if(!getimagesize($_FILES['file_upload']['tmp_name'])){
                      die('Please ensure you are uploading an image.');
                  }
                  
                  // Check filesize
                  if($_FILES['file_upload']['size'] > 500000){
                      die('File uploaded exceeds maximum upload size.');
                  }
                  
                  // Check if the file exists
                  if(file_exists('upload/' . $_FILES['file_upload']['name'])){
                      die('File with that name already exists.');
                  }
                  
                  // Upload file
                  if(!move_uploaded_file($_FILES['file_upload']['tmp_name'], 'upload/' . $_FILES['file_upload']['name'])){
                      die('Error uploading file - check destination is writeable.');
                  }
                  
                  die('File uploaded successfully.');
                  
                  ?>
                  

                  而且我需要对现有文件采取windows"的处理方式——我的意思是,如果文件存在,我希望将其更改为文件名,后面带有数字 1.

                  and I need to act like a "windows" kind of treatment for existing files - I mean the if the file exists, i want it to be changed to the name of the file with the number 1 after it.

                  例如:myfile.jpg 已经存在,所以再上传就是myfile1.jpg,如果myfile1.jpg 存在就是myfile11.jpg 以此类推……

                  for example: myfile.jpg is already exists, so if you'll upload it again it will be myfile1.jpg, and if myfile1.jpg exists, it will be myfile11.jpg and so on...

                  我该怎么做?我尝试了一些循环,但不幸的是没有成功.

                  how can i do it? i tried some loops but unfortunately without success.

                  推荐答案

                  你可以这样做:

                  $name = pathinfo($_FILES['file_upload']['name'], PATHINFO_FILENAME);
                  $extension = pathinfo($_FILES['file_upload']['name'], PATHINFO_EXTENSION);
                  
                  // add a suffix of '1' to the file name until it no longer conflicts
                  while(file_exists($name . '.' . $extension)) {
                      $name .= '1';
                  }
                  
                  $basename = $name . '.' . $extension;
                  

                  为了避免名字太长,附加一个数字可能会更整洁,例如file1.jpgfile2.jpg 等:

                  To avoid very long names, it would probably be neater to append a number, e.g. file1.jpg, file2.jpg etc:

                  $name = pathinfo($_FILES['file_upload']['name'], PATHINFO_FILENAME);
                  $extension = pathinfo($_FILES['file_upload']['name'], PATHINFO_EXTENSION);
                  
                  $increment = ''; //start with no suffix
                  
                  while(file_exists($name . $increment . '.' . $extension)) {
                      $increment++;
                  }
                  
                  $basename = $name . $increment . '.' . $extension;
                  

                  这篇关于重命名文件,如果已经存在 - php上传系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用唯一文件名通过 php 上传图像 下一篇:安全的 PHP 文件上传脚本

                  相关文章

                    <bdo id='l3hpn'></bdo><ul id='l3hpn'></ul>

                2. <legend id='l3hpn'><style id='l3hpn'><dir id='l3hpn'><q id='l3hpn'></q></dir></style></legend>

                  <i id='l3hpn'><tr id='l3hpn'><dt id='l3hpn'><q id='l3hpn'><span id='l3hpn'><b id='l3hpn'><form id='l3hpn'><ins id='l3hpn'></ins><ul id='l3hpn'></ul><sub id='l3hpn'></sub></form><legend id='l3hpn'></legend><bdo id='l3hpn'><pre id='l3hpn'><center id='l3hpn'></center></pre></bdo></b><th id='l3hpn'></th></span></q></dt></tr></i><div id='l3hpn'><tfoot id='l3hpn'></tfoot><dl id='l3hpn'><fieldset id='l3hpn'></fieldset></dl></div>

                    <tfoot id='l3hpn'></tfoot>

                    1. <small id='l3hpn'></small><noframes id='l3hpn'>