当前位置: 动力学知识库 > 问答 > 编程问答 >

html - Change filename/order in array with PHP

问题描述:

I am working on a website where images will be displayed using a library called photoswipe. I have got two folders, one for thumbnails and one with the original image. Both folders contain 70 images with filenames starting from '001.jpg' until '070.jpg'.

I am using PHP to put all these file names in an array. After that, I am using a foreach-loop to print the images.

The printing looks like this:

<a href="<?php echo $portraitsDir ?>original/<?php echo $valueOriginal; ?>" itemprop="contentUrl" data-size="<?php echo $dimensions?>" data-index="<?php $key ?>">

<img src="<?php echo $portraitsDir ?>thumbnail/<?php echo $imagesPortaitsThumbnail[$key]; ?>" height="100" width="100" itemprop="thumbnail" alt="All rights reserved."></a>

What would be the easiest way to re-order the files. Let's say that image '052.jpg' should be placed on position 7 on the website. Until now I was manually editing the file names. So 052 would become 007, 007 would become 008, 008 would become 009 and so on.

网友答案:

You might have to use the filemtime() function to find the modification date of each file.BTW check out this answer for more info! http://stackoverflow.com/a/16599152

网友答案:

Sorts $Originals and $Thumbnails by modified time of $Originals:

array_multisort(array_map('filemtime', $Originals), SORT_DESC, $Originals, $Thumbnails);

Based on your comment, you should use a database, even a small one. Then you just select the data and sort on a custom sort order stored in the database. You can present a form or button to allow people to move them and change that sort order. Check SQLite.

分享给朋友:
您可能感兴趣的文章:
随机阅读: