I'm using a basic multiple upload Javascript that doesn't allow to re-order files. I would like to be able to drag and drop pictures and save their new position on the server. The tricky part is that I do not store those files in a database.
The upload process works that way : as you can see, it's a simple loop that rename the pics from 001.jpg to 099.jpg and place them in the right folder.
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++;
}
}
The display is as simple : the page just checks the folder and shows the content in alphabetical order :
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>';
}
My immediate need would be to be able to simply drag and drop the pics and rename them on the server. For example, if I drag 005.jpg in third position, it would rename it 002-1608378266.jpg (adding the actual Timestamp so it places itself before 003.jpg without erasing the previous file).
I'm aware it's not the best option (not even sure that's doable) and I'm already working on a database solution, but I need an emergency solution to calm down users while I develop the new script.
You can have a look at the test server using id and password Stackoverflow : https://www.0000.yelofox.fr/page-10-at-vero-eos-et-accusam
Thanks to those who will try and help me !