• <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上传系统

        Rename a file if already exists - php upload system(重命名文件,如果已经存在 - php上传系统)
          <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上传系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                    <bdo id='l3hpn'></bdo><ul id='l3hpn'></ul>

                  • <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>

                            <tbody id='l3hpn'></tbody>

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