php 检查file_exists是否存在,如果存在,则在末尾添加1

wtzytmuj  于 2022-10-30  发布在  PHP
关注(0)|答案(1)|浏览(120)

我有这个代码,我已经写(upload.php),然而,当它上传,我似乎不能保存一个新的文件与一个-我也试图找到一种方法来做到这一点,但似乎不能让它工作.
[上传. php]

  1. <?php
  2. /* Get the name of the uploaded file */
  3. $filename = $_FILES['file']['name'];
  4. /* Choose where to save the uploaded file */
  5. $location = "upload/".$filename;
  6. /* Save the uploaded file to the local filesystem */
  7. if ( move_uploaded_file($_FILES['file']['tmp_name'], $location) ) {
  8. echo 'Success';
  9. } else {
  10. echo 'Failure';
  11. }
  12. ?>

下面是[index.php]的一部分:

  1. <div class="chat-message" id="chat-area">
  2. <div class="message-content-grid">
  3. <div class="add-file">
  4. <label for="fileupload">
  5. <i class="fa-solid fa-circle-plus"></i>
  6. </label>
  7. <input id="fileupload" type="file" name="fileupload" onchange="uploadFile()"/>
  8. </div>
  9. <div class="message-area">
  10. <form id="send-message-area">
  11. <textarea id="sendie" maxlength ="300" placeholder="Message"></textarea>
  12. </form>
  13. </div>
  14. <div>
  15. </div>
  16. <script>document.getElementById('chat-area').scrollTop = document.getElementById('chat-area').scrollHeight;</script>
  17. <script>
  18. async function uploadFile() {
  19. let formData = new FormData();
  20. formData.append("file", fileupload.files[0]);
  21. await fetch('upload.php', {
  22. method: "POST",
  23. body: formData
  24. });
  25. alert('The file has been uploaded successfully.');
  26. }
  27. </script>

因此,当您单击上传图标时:

  1. <label for="fileupload">
  2. <i class="fa-solid fa-circle-plus"></i>
  3. </label>
  4. <input id="fileupload" type="file" name="fileupload" onchange="uploadFile()"/>

它将运行javascript:

  1. <script>
  2. async function uploadFile() {
  3. let formData = new FormData();
  4. formData.append("file", fileupload.files[0]);
  5. await fetch('upload.php', {
  6. method: "POST",
  7. body: formData
  8. });
  9. alert('The file has been uploaded successfully.');
  10. }
  11. </script>

这将实习,运行php脚本在[upload.php].

  1. <?php
  2. /* Get the name of the uploaded file */
  3. $filename = $_FILES['file']['name'];
  4. /* Choose where to save the uploaded file */
  5. $location = "upload/".$filename;
  6. /* Save the uploaded file to the local filesystem */
  7. if ( move_uploaded_file($_FILES['file']['tmp_name'], $location) ) {
  8. echo 'Success';
  9. } else {
  10. echo 'Failure';
  11. }
  12. ?>

我想要的是检查是否有一个文件具有相同的名称,如果有,添加一个1或2或3取决于该文件是否也存在(file1.txt,file2.txt,file3.txt...)任何人有什么想法?Thx

vc9ivgsu

vc9ivgsu1#

已修复:

  1. <?php
  2. /* Get the name of the uploaded file */
  3. $name = $_FILES['file']['name'];
  4. $actual_name = pathinfo($name,PATHINFO_FILENAME);
  5. $original_name = $actual_name;
  6. $extension = pathinfo($name, PATHINFO_EXTENSION);
  7. $i = 1;
  8. while(file_exists('upload/'.$actual_name.".".$extension))
  9. {
  10. $actual_name = (string)$original_name.$i;
  11. $name = $actual_name.".".$extension;
  12. $i++;
  13. }
  14. /* Choose where to save the uploaded file */
  15. $location = "upload/". $name;
  16. /* Save the uploaded file to the local filesystem */
  17. if ( move_uploaded_file($_FILES['file']['tmp_name'], $location) ) {
  18. echo 'Success';
  19. } else {
  20. echo 'Failure';
  21. }
  22. ?>
展开查看全部

相关问题