使用Ajax拖放在服务器上命名文件

格雷格

我使用的是基本的多次上传Javascript,不允许重新排序文件。我希望能够拖放图片并将其新位置保存在服务器上。棘手的部分是我不将那些文件存储在数据库中

上传过程是这样的:如您所见,这是一个简单的循环,将图片从001.jpg重命名为099.jpg,并将其放置在正确的文件夹中。

if(isset($_FILES['images']) && $_FILES['images']['size'] > 0) {
        if(!file_exists('images/upload/' . $id_client . '/photos/' . $gallery)) {
            mkdir('images/upload/' . $id_client . '/photos/' . $gallery);
        }
        else {
            $gallery = $data_m[0]['gallery'];
        }
        $i = htmlspecialchars(trim($_POST['nb_img']) + 1);
        foreach($_FILES['images']['tmp_name'] as $index => $tmpName) {
            if(!empty($tmpName) && is_uploaded_file($tmpName)) {
                if(substr($_FILES['images']['type'][$index], 0, 5) == "image") {
                    if($i < 10) {
                        $img_url = '00' . $i . '.' . pathinfo($_FILES['images']['name'][$index], PATHINFO_EXTENSION);
                    }
                    else {
                        $img_url = '0' . $i . '.' . pathinfo($_FILES['images']['name'][$index], PATHINFO_EXTENSION);    
                    }
                    move_uploaded_file( $tmpName, $dir_upload . '/photos/' . $gallery . '/'. $img_url);
                }
            }
            $i++;
        }
    }

显示非常简单:页面仅检查文件夹并按字母顺序显示内容:

if(!empty($data['gallery']) && file_exists($dir_upload . '/photos/' . $data['gallery'])) {
            $dirname = $dir_upload . '/photos/' . $data['gallery'];
            $gallery = scandir($dirname);
            asort($gallery);
            $row = 1;
            echo '<div class="gallery">';
                echo '<div class="row">';
                foreach($gallery as $pix) {
                    if(!empty($pix) && $pix != '.' && $pix != '..') {
                        if($row >5) { 
                            echo '</div>'; 
                            echo '<div class="row">';
                            $row = 1; 
                        }
                        $url_img = $dirname . '/' . $pix;
                        echo '<a class="col-md mt-1 mr-1 mb-1 ml-1 btn btn-secondary img-'.$row.' disabled" href="' . $url_img . '" rel="clearbox[gallery=' . $data['gallery'] . ']" style="background:url(' . $url_img . ') center; background-size:cover; height:210px;">';
                        echo '</a>';
                        $row++;
                    }
                }
                echo '</div>';
            echo '</div>';
        }

我的迫切需要是能够简单地将图片拖放到服务器上并将其重命名。例如,如果我将005.jpg拖动到第三位置,它将重命名为002-1608378266.jpg(添加实际时间戳记,以便将其自身放置在003.jpg之前而不擦除先前的文件)。

我知道这不是最好的选择(甚至不能确定这是否可行),并且我已经在研究数据库解决方案,但是在开发新脚本时,我需要一个紧急解决方案来使用户平静下来。

您可以使用ID和密码Stackoverflow查看测试服务器https : //www.0000.yelofox.fr/page-10-at-vero-eos-et-accusam

感谢那些会努力帮助我的人!

布莱克斯

我们能够深入了解聊天中的内容,下面是一个建议的解决方案:

图像的顺序存储在JSON文件中。然后,当我们想获取图库包含的图像列表时,将读取该文件,并在添加新图像以及用户对这些图像进行排序时进行更新。

为了做到这一点,我制作了一个仅包含实用程序功能和演示页面的文件:

utils.php

<?php 

/**
 * Returns the path to a gallery's directory
 */
function getClientGalleryDirPath($clientId, $gallery) {
  return 'images/upload/' . $clientId . '/photos/' . $gallery;
}

/**
 * Returns the path to a gallery's data file
 */
function getClientGalleryDataPath($clientId, $gallery) {
  return getClientGalleryDirPath($clientId, $gallery) . '/data.json';
}

/**
 * Makes sure the client's gallery has a directory, and a JSON file
 * containing the list of images
 */
function ensureClientGalleryExists($clientId, $gallery) {
  $dirPath = getClientGalleryDirPath($clientId, $gallery);

  if(!file_exists($dirPath)) {
    mkdir($dirPath, 0755, true);
  }

  $jsonPath = getClientGalleryDataPath($clientId, $gallery);

  if (!file_exists($jsonPath)) {
    // Create an empty Array
    $data = [];

    // If pictures exist, we'll add them
    $pictureFiles = scandir($dirPath);
    foreach($pictureFiles as $pic) {
      if(!empty($pic) && $pic != '.' && $pic != '..') {
          array_push($data, $pic);
      }
    }
    // Make the Array a JSON string, and save it
    $json = json_encode($data);
    file_put_contents($jsonPath, $json);
  }
}

/**
 * Ensures the gallery is created, and returns the data
 */
function getOrCreateClientGallery($clientId, $gallery) {
  // Make sure it exists
  ensureClientGalleryExists($clientId, $gallery);

  $dataPath = getClientGalleryDataPath($clientId, $gallery);
  $json = file_get_contents($dataPath);

  return json_decode($json, true);
}

/**
 * Saves new data in a client's gallery
 */
function saveClientGallery($clientId, $gallery, $data) {
  // Make sure it exists
  ensureClientGalleryExists($clientId, $gallery);

  $dataPath = getClientGalleryDataPath($clientId, $gallery);
  $json = json_encode($data);

  return file_put_contents($dataPath, $json);
}

/**
 * Uploads pictures, and updates the gallery data
 */
function addPicturesToGallery($clientId, $gallery, $files) {
  // Get the existing data
  $data = getOrCreateClientGallery($clientId, $gallery);
  $dirPath = getClientGalleryDirPath($clientId, $gallery);

  foreach($files['tmp_name'] as $index => $tmpName) {
    if (
         !empty($tmpName) &&
         is_uploaded_file($tmpName) &&
         substr($files['type'][$index], 0, 5) == "image"
       ) {
         // Create a unique name, and move the file
         $newName = uniqid() . '.' . pathinfo($files['name'][$index], PATHINFO_EXTENSION);
         move_uploaded_file($tmpName, $dirPath . '/'. $newName);
         // Add the new name to $data 
         array_push($data, $newName);
    }
  }

  // We're done, save the data
  saveClientGallery($clientId, $gallery, $data);
}

/**
 * Compares the new list with the previous one, and updates it
 */
function  reorderClientGallery($clientId, $gallery, $newData) {
  $oldData = getOrCreateClientGallery($clientId, $gallery);

  // Make sure that every element is present in both Arrays
  // (otherwise there's something wrong with the provided data)
  if (
       count(array_diff($oldData, $newData)) == 0 &&
       count(array_diff($newData, $oldData)) == 0
     ) {
       saveClientGallery($clientId, $gallery, $newData);
  }
}

index.php

<?php 

require_once('utils.php');

// Just for the demo, let's say that:
$clientId = 'robert';
$gallery = 'my-first-gallery';

// If pictures were sent, upload them
if (isset($_FILES['images']) && $_FILES['images']['size'] > 0) {
  addPicturesToGallery($clientId, $gallery, $_FILES['images']);
}

// If a picture ordering was sent, apply it
if (isset($_POST['order'])) {
  reorderClientGallery($clientId, $gallery, json_decode($_POST['order']));
}



// To avoid cluttering the HTML below, I declared this here
function displayGalleryItems($clientId, $gallery) {
  $dirPath = getClientGalleryDirPath($clientId, $gallery);
  // Get the gallery and display it
  $data = getOrCreateClientGallery($clientId, $gallery);
  foreach($data as $pic) {
    echo '<div class="pic" data-pic="' . $pic . '" style="background-image: url(\'' . $dirPath . '/' . $pic . '\')"></div>';
  }
}

?><!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>My gallery</title>
  <style>
    #saveBtn { display: none; } /* will only be displayed after reordering */
    .pic { display: inline-block; width: 250px; height: 190px; background-size: cover; margin: .5em; }
  </style>
</head>
<body>
  <h1><?=$gallery?></h1>

  <p>Add pictures to your gallery:</p>
  <form method="post" enctype="multipart/form-data">
    <input type="file" name="images[]" multiple>
    <input type="submit">
  </form>

  <div id="sortable">
    <?php displayGalleryItems($clientId, $gallery); ?>
  </div>

  <form method="post">
    <input type="hidden" name="order">
    <input type="submit" value="Enregistrer" id="saveBtn">
  </form>

  <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
  <script>
  $(function() {
    $("#sortable").sortable({
      revert: 200,
      update: function() {
        // Every time the order changes, grab the filenames
        var order = $('.pic').map(function(i, p) { return $(p).attr('data-pic'); }).toArray();
        // Set the form input's value
        $('input[name="order"]').val( JSON.stringify(order) );
        // Show the save button
        $('#saveBtn').show();
      }
    });

    $(".pic").disableSelection();
  });
  </script>
</body>
</html>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

FTPS使用Shell将文件放在FTP服务器上

来自分类Dev

在Python上重命名远程服务器上的文件

来自分类Dev

重命名远程服务器上的文件夹

来自分类Dev

重命名服务器上的所有.htaccess文件

来自分类Dev

使用Dropzone.js删除服务器上的重命名文件

来自分类Dev

使用Dropzone.js删除服务器上的重命名文件

来自分类Dev

使用FtpWebRequest重命名FTP服务器上的目录

来自分类Dev

将网站部署到TFS构建服务器上的拖放文件夹

来自分类Dev

将WAR文件放在Pivotal TC服务器上的什么位置?

来自分类Dev

放在远程服务器上时,CSS 文件不起作用

来自分类Dev

获取 FTP 服务器上的文件大小并将其放在标签上

来自分类Dev

无法在服务器上使用Ajax发布数据

来自分类Dev

使用AJAX在服务器上执行PHP

来自分类Dev

如何使用AJAX与服务器通信并在服务器上运行代码?

来自分类Dev

使用AJAX将Zip文件上传到服务器

来自分类Dev

文件未使用Ajax上传到服务器

来自分类Dev

使用AJAX从其他远程服务器获取文件?

来自分类Dev

使用AJAX将文件上传到服务器

来自分类Dev

在没有ajax的情况下获取Json文件(拖放)并将其发送到服务器

来自分类Dev

使用 angular 等框架时服务器上服务器端脚本的文件结构

来自分类Dev

使用PHP在我的Owncloud服务器上上传文件

来自分类Dev

使用php在ftp服务器上显示文件

来自分类Dev

使用PowerShell更改文件服务器上的权限

来自分类Dev

使用itextsharp在ftp服务器上提取pdf文件

来自分类Dev

在服务器上使用rsync推送文件夹

来自分类Dev

使用php在wordpress服务器上保存文件

来自分类Dev

如何使用 ftplib 写入远程服务器上的文件

来自分类Dev

Kendo UI在服务器上上传重命名文件并更新客户端

来自分类Dev

可以在12.04服务器上重命名默认的“ Ubuntu one”文件夹吗?

Related 相关文章

  1. 1

    FTPS使用Shell将文件放在FTP服务器上

  2. 2

    在Python上重命名远程服务器上的文件

  3. 3

    重命名远程服务器上的文件夹

  4. 4

    重命名服务器上的所有.htaccess文件

  5. 5

    使用Dropzone.js删除服务器上的重命名文件

  6. 6

    使用Dropzone.js删除服务器上的重命名文件

  7. 7

    使用FtpWebRequest重命名FTP服务器上的目录

  8. 8

    将网站部署到TFS构建服务器上的拖放文件夹

  9. 9

    将WAR文件放在Pivotal TC服务器上的什么位置?

  10. 10

    放在远程服务器上时,CSS 文件不起作用

  11. 11

    获取 FTP 服务器上的文件大小并将其放在标签上

  12. 12

    无法在服务器上使用Ajax发布数据

  13. 13

    使用AJAX在服务器上执行PHP

  14. 14

    如何使用AJAX与服务器通信并在服务器上运行代码?

  15. 15

    使用AJAX将Zip文件上传到服务器

  16. 16

    文件未使用Ajax上传到服务器

  17. 17

    使用AJAX从其他远程服务器获取文件?

  18. 18

    使用AJAX将文件上传到服务器

  19. 19

    在没有ajax的情况下获取Json文件(拖放)并将其发送到服务器

  20. 20

    使用 angular 等框架时服务器上服务器端脚本的文件结构

  21. 21

    使用PHP在我的Owncloud服务器上上传文件

  22. 22

    使用php在ftp服务器上显示文件

  23. 23

    使用PowerShell更改文件服务器上的权限

  24. 24

    使用itextsharp在ftp服务器上提取pdf文件

  25. 25

    在服务器上使用rsync推送文件夹

  26. 26

    使用php在wordpress服务器上保存文件

  27. 27

    如何使用 ftplib 写入远程服务器上的文件

  28. 28

    Kendo UI在服务器上上传重命名文件并更新客户端

  29. 29

    可以在12.04服务器上重命名默认的“ Ubuntu one”文件夹吗?

热门标签

归档