0

I wrote the following code in order to find the newest files added in three different folders. I have stored the directories of the folders in an array called path. I also store the names of the newest files in another array.

$path[0] = "/Applications/MAMP/htdocs/php_test/check";
$path[1] = "/Applications/MAMP/htdocs/php_test/check2";
$path[2] = "/Applications/MAMP/htdocs/php_test/check3";

for ($i=0; $i<=2; $i++){

$path_last = $path[$i]; // set the path
$latest_ctime = 0;
$latest_filename = '';    

 $d = dir($path_last);
while (false !== ($entry = $d->read())) {
  $filepath = "{$path_last}/{$entry}";
  if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
$latest_ctime = filectime($filepath);
$latest_filename = $entry;
$array[$i] = $latest_filename ; // assign the names of the latest files in an array.
   }
  }

Everything works fine but now I try to put the same code in a function and call it in my main script. I use this code in order to call it:

   include 'last_file.php'; // Include the function last_file
   $last_file = last_file();  // assign to the function a variable and call the function     

I am not sure if this is correct. What I want to do is to return the array in my main script .. I hope it's clear here what I am trying to explain. Thanks D.

This is the last_file function:

    function last_file(){

        for ($i=0; $i<=2; $i++){

     $path_last = $path[$i]; // set the path
      $latest_ctime = 0;
       $latest_filename = '';    

      $d = dir($path_last);
     while (false !== ($entry = $d->read())) {
      $filepath = "{$path_last}/{$entry}";
      // could do also other checks than just checking whether the entry is a file
       if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
        $latest_ctime = filectime($filepath);
        $latest_filename = $entry;
     $array[$i] = $latest_filename ; // assign the names of the latest files in an   array.
        }
      }

    return $array;

      }//end loop
      }//end function
4
  • 1
    Show your last_file function. You may just need to return the array. Commented Nov 10, 2012 at 20:03
  • The last line of your function should be return $array; assuming $array is the one you wish to return. Commented Nov 10, 2012 at 20:04
  • 1
    After seeing your code - move that return $array; outside the loop, before the function's closing } Commented Nov 10, 2012 at 20:07
  • Thanx. I did it. The problem is that I don't know how to call the array in my main source code. For example, how can I have access of array[0] somewhere in my script? Commented Nov 10, 2012 at 20:16

2 Answers 2

2

You are confusing a function with a file. Your code is put in files. Within those files, you can define functions:

<?php
// This is inside last_file.php

function someFunction {
    // Do stuff.
}

To use someFunction() in another file, you first include last_file.php and then call someFunction():

<?php
// This is inside some other file.

include 'last_file.php';

someFunction();
Sign up to request clarification or add additional context in comments.

Comments

1

If you put it in a function, you need to return it like so.

function func_name()
{
$path[0] = "/Applications/MAMP/htdocs/php_test/check";
$path[1] = "/Applications/MAMP/htdocs/php_test/check2";
$path[2] = "/Applications/MAMP/htdocs/php_test/check3";

for ($i=0; $i<=2; $i++){

$path_last = $path[$i]; // set the path
$latest_ctime = 0;
$latest_filename = '';    

 $d = dir($path_last);
while (false !== ($entry = $d->read())) {
  $filepath = "{$path_last}/{$entry}";
  if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
$latest_ctime = filectime($filepath);
$latest_filename = $entry;
$array[$i] = $latest_filename ; // assign the names of the latest files in an array.
return $array;
}

Then when you call that function and assign it to a variable you will get the contents of $array

$contentsFromFunction = func_name();

7 Comments

I do this but then in the main source how can I use the array for something like thi: $file_path[0] = "/Applications/MAMP/htdocs/".$array[0];
Thanx. I did it. The problem is that I don't know how to call the array in my main source code. For example, how can I have access of array[0] somewhere in my script?
In my example, you would then call $contentsFromFunction[0] So when you call $contentsFromFunction = func_name(); Whatever was in $array in the function func_name() is now in $contentsFromFunction.
I assign the function like this: $last_file = last_file(); And then I want to use the array value like this: $file_path = "/Applications/MAMP/htdocs/php_test/check/".$last_file[0]; But it doesn't seem to work!
What's in the $last_file variable? You can find out using var_dump($last_file);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.