Open php file in ascending order

Asked

Viewed 544 times

0

I have an update/ folder with PHP files, so:

inserir a descrição da imagem aqui

These are mysql update files, when it is updated soon after the server erases. When the client does not access the Admin the file stays there, when we release another update then adds the file of the previous version and the latest one and so on until it accesses the Admin to run this update.

My problem:

It is not running in ascending order. I need to make it always open from the smallest to the largest, because that is the order of the update.

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();
?>

3 answers

1


Save file names to an array and use the function natcasesort() 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);

You just have to be careful with file nomenclature.

Example, archive11.txt will appear before arquivo2.txt because the number 1 comes before the number 2. Alphabetic sorting reads as strings, so to avoid this problem, the nomenclature should have zeros to the left if they contain sequential numbers.

The archive11.txt would look like arquivo000011.txt

The arquivo2.txt would look like arquivo000002.txt

The amount of zeros left, you define as your case.

  • I just didn’t understand how to determine the location of the folder where you have the files, as in my example above.

  • In your script, you used an Apache dependent global variable. $_SERVER["DOCUMENT_ROOT"]. I do not recommend for this purpose. In the example I posted , I used the constant __DIR__, which returns the current script directory. Just test the example.. Give a echo $base; exit; to see the result. Easily you will realize and know how to implement what you need.

  • Thanks for the tip, I was able to understand, but it seems that there is a problem, the last file to be run is in the first value of the array that is ZERO. Behold Array ( [1] => updateSQL_v419.php [2] => updateSQL_v420.php [3] => updateSQL_v421.php [0] => updateSQL_v422.php )

  • must have done something wrong.. Also note that there are different ways to do it. The central idea is to use the natcasesort() function to sort the array. It usually comes sorted alphabetically, but it’s good to use the function to make sure it’s really well ordered. As for the error you describe, I cannot evaluate without knowing what and how you are making your adaptation.

  • I did as you informed <?php &#xA;$base = __DIR__.DIRECTORY_SEPARATOR."/update/";&#xA;$dir = dir($base);&#xA;while (false !== ($e = $dir->read()))&#xA; if (is_file($base.$e)) &#xA; $f[] = $e;&#xA;natcasesort($f);&#xA;print_r($f);&#xA;?>

  • Ahhh got it.. Man.. Exit went right.. what happens is that the indexes of the array are preserved, but the order of them has been modified correctly. Note you even the output of print_r(). Despite having ZERO key, the value is in the last position.

  • And what’s the solution to that? :)

  • how so? solution? is correct... rsrs

  • is worried about how to do the loop loop? is that? if you use the foreach() why you won’t need to worry about the order of the keys.

  • How do I display one file at a time? It has to be run one by one, I’m using the foreach to get the file name, but it displays all.

Show 5 more comments

-2

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

Assembling an Array

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

i = 0
numeroDeArquivos = Array.len()
while i < numeroDeArquivos:
    open(Array[i], "a")

-3

Good Morning, Friend I have an idea for you to do what you want: You can do as follows each time you do the update you annotate in a txt file the name of the file ex: TXT file = updateSQL_v413.php and at the time of the next update you will:

read the txt file deletes the first 11 string = updateSQL_v divide array by "." = Array[0] = 413, Array[1] = .php. take the array[1] position and sum + 1. >> R: 413 + 1 = 414 the result you go and pull the update by replenishing the 11 string first and joining the numbers + . php that would be:

updateSQL_v414.php.

I hope you understand and help.

  • 1

    You can exemplify this through code?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.