iMarkDesigns
iMarkDesigns

Reputation: 1262

Usort with deprecated create_function

I have my case similar to the others, tried their suggestions but it was not working for me.

usort($file, create_function('$a,$b', 'return filemtime($a)<filemtime($b);'));

the code represents pdf display in our client dashboard (random pdf display) for them to be able to download. however, I tried to convert them into a native function but the pdf display shows only the very end of the uploaded pdf files.

usort($file, function($a, $b) {
   return filemtime($a) < filemtime($b);
} );

here's the code above I tried to change the create_function into function() but it doesn't change the display.

I do not own this code. It was assigned to me to fix it.

Update: In case you need the whole codes, here it is.

if($newDIR != "" && is_dir($newDIR)) {

    $file = File::allFiles($newDIR);

    usort($file, create_function('$a,$b', 'return filemtime($a)<filemtime($b);'));

    foreach($file as $files) {
        $fileInfo = pathinfo($files);

        $name = $fileInfo['filename'];

        $size = File::size($files);
        $datemodiy = File::lastModified($files);
        $size = $size/1048576;
        $fileLink = str_replace('\\', '/', $fileInfo['dirname']).'/'.$fileInfo['basename'];
        $folderLink= str_replace('documents/','',$mainDIR);

        $date2 = date('Y-m-d',$datemodiy);
        $dateno = date('Y-m-d');
        $intervalDate = (int)abs((strtotime($dateno) - strtotime($date2))/(60*60*24*30));

        if($intervalDate <= 2) {
            $fileDir[] = array('link'=>$fileLink,'name'=>$name,'size'=>$size,'datemodiy'=>$datemodiy,'folderLink'=>$folderLink);
        }
    } // end for each

} // if $dir

Updates:

The code above I used to replace the old create_function was ok... and working fine. I was skeptical only because I've downloaded all the files at once instead of manually one by one per different files. I see now each of the pdf displaying random files.

Upvotes: 1

Views: 1435

Answers (1)

Prafulla Kumar Sahu
Prafulla Kumar Sahu

Reputation: 9703

Why you can not use a closure

usort($file, function($a, $b){
    return filemtime($a)<filemtime($b);
});

Or Yes the function create_function but in that case, you can create a function in your helper and make it working, the definition of this function will be something like

$str = '$a,$b';
$code = "return filemtime($a)<filemtime($b);";

var_dump(createFunction($str, $code));

function createFunction($str, $code) {
    $code;
}

Upvotes: 1

Related Questions