Open php file in ascending order

0

I have an update / folder with PHP files, like this:

Thesearemysqlupdatefiles,whenitisupdatedshortlyaftertheservershutsdown.WhentheclientdoesnotaccesstheAdmin,thefilestaysthere,whenwereleaseanotherupdate,thenitaddsthefileofthepreviousversionandthelatestversionandsoonuntilitaccessestheAdmintorunthisupdate.

Myproblem:

Notrunninginascendingorder.Ineedtomakeitalwaysopenfromthesmallesttothelargest,asthisistheorderoftheupdate.

Code:

<?php$path=$_SERVER["DOCUMENT_ROOT"]."/update/"; 
$diretorio = dir($path); $i = 0;
while($i <= 3){
    $arquivo = $diretorio -> read(); 
    $file = explode("_v", $arquivo);
    if ($file[0] == 'updateSQL'){
?>

    <script type="text/javascript">$(function() {$('#myModal').modal('show');})</script>
    <script type="text/javascript">$('#myModal').modal({backdrop: 'static',keyboard: false})</script>

    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title" id="myModalLabel">Espere... Não feche até acabar.</h4>
          </div>
          <div class="modal-body">
            <iframe src="../update/<?php echo $arquivo?>" frameborder="0" height="300px" width="100%" onload="resizeIframe(this);"></iframe>
          </div>
        </div>
      </div>
    </div>

<?php 
    }$i++;
} 
$diretorio -> close();
?>
    
asked by anonymous 03.01.2016 / 12:44

3 answers

1

Save the names of the files in an array and use the natcasesort () function to organize them alphabetically.

Teaching example:

$base = __DIR__.DIRECTORY_SEPARATOR;
$dir = dir($base);
while (false !== ($e = $dir->read()))
    if (is_file($base.$e)) 
        $f[] = $e;
natcasesort($f);
print_r($f);

Just be careful about file naming.

Example, file11.txt will appear before file2.txt because the number 1 comes before the number 2. Alphabetic ordering reads as strings, to avoid this problem, the nomenclature must have leading zeros if they contain sequential numbers.

The file11.txt would look like file000011.txt

The file2.txt would look like file000002.txt

The number of leading zeros, you define according to your case.

    
03.01.2016 / 15:13
-2

Friend, besides you can do for a TXT file, if you have a base numbering you can do by this method:

Mounting an Array

You would mount an array with the name of the files manually or with some PHP directory reading function. With the array ready in the code, it will be enough to feed you with the different files and can run through a while (I will make an example in Python just so you understand the logic):

i = 0
numeroDeArquivos = Array.len()
while i < numeroDeArquivos:
    open(Array[i], "a")
    
03.01.2016 / 14:19
-3

Good day, friend I have an idea for you to do what you want: You can do the following way every time you do the update you note in a file txt the file name ex: TXT FILE = updateSQL_v413.php and at the time of the next update you will do:

reading the txt file deletes the first 11 string = updateSQL_v divide array by "." = Array [0] = 413, Array [1] = .php. takes the array position [1] and sums it up to 1.

03.01.2016 / 13:50