0

I have a short PHP code to help me display random images from a specific folder. But now it seems to select any image in any size. I want those selected images are between 100-500 kb. If it's less than 100 kb or over 500 kb, the function won't select and display it. Could you please tell me how to modify this code? Probably need to add some function.

<?php $randomdir = dir('images/random');
$count = 1;
$pattern="/(gif|jpg|jpeg|png)/";
while($file = $randomdir->read()) {
    $ext = pathinfo($file, PATHINFO_EXTENSION);
    if (preg_match($pattern, $ext)) {
        $imagearray[$count] = $file;
        $count++;
    }
}
$random = mt_rand(1, $count - 1); 
echo '<img src="images/random/'.$imagearray[$random].'" alt />';
?>

2 Answers2

1

Try now

<?php 
$dir_name = 'images/random/';
$pattern="/(gif|jpg|jpeg|png)/";

$min = 100;
$max = 500;
$imagearray = array();
$scanned_directory = array_diff(scandir($dir_name), array('..', '.'));
$count = count($scanned_directory);
$ids = array_keys($scanned_directory);
 

$s = TRUE;
$stop = $count;
while( ($s === TRUE) && ($stop >=0))
{
        
    
    $random = mt_rand(0, $count - 1);
    $full_path_to_file = $dir_name.$scanned_directory[$ids[$random]];
    $ext = pathinfo($full_path_to_file, PATHINFO_EXTENSION);
    $file_size_kb = round(filesize($full_path_to_file)/1024);
    if (preg_match($pattern, $ext) && ($file_size_kb>=$min && $file_size_kb<=$max)) 
    {
        $s = FALSE;
        echo '<img src="'.$full_path_to_file.'" alt />';
    }
    $stop--;
}

?>
WiatroBosy
  • 1,076
  • 1
  • 6
  • 17
  • Hi @WiatroBosy Thanks for your help. But I don't know which error, it stills not work – Gates Zuckerburg Jan 10 '21 at 15:50
  • I guess it's the error from this line: $file_size_kb = round(filesize($file)/1024; – Gates Zuckerburg Jan 10 '21 at 15:58
  • Hi @Jitendra Yadav, I tried both of your solution. The page says" This page contains the following errors: error on line 2 at column 1: Extra content at the end of the document Below is a rendering of the page up to the first error. – Gates Zuckerburg Jan 10 '21 at 17:37
  • When the server proceed this line '$file_size_kb = round(filesize($file)/1024);', it seems the server executes all images and cost long time. It should be execute only one file. – Gates Zuckerburg Jan 10 '21 at 17:39
  • @Gates, no issues with the answer, I think you have some issues with your code. Add your code to the question, so we can help. – Dark Knight Jan 10 '21 at 17:41
  • And how many files you have in directory. And is ther a sub directory ? And next time show Exactly error message – WiatroBosy Jan 10 '21 at 17:44
  • The folder has thousands of images. It call back the error: Warning: filesize(): stat failed for xxxxxxx-1536x1536.jpg in /home/test/test.php on line 19
    – Gates Zuckerburg Jan 10 '21 at 17:58
  • Can I follow this thread solution ? https://stackoverflow.com/questions/34481697/filesize-stat-failed-for-specific-path-php/34481934 – Gates Zuckerburg Jan 10 '21 at 18:00
  • Hi @Jitendra Yadav, I understand your solution and the attached screenshot. In your screenshot, the code export all listed images, dev1.jpg dev2.jpg. That's why in my server it listed all thousands images at one time then caused filesize(): stat failed error. I think we misunderstand. What I wish is only export one random image, not all. Could you help me modify? – Gates Zuckerburg Jan 11 '21 at 05:52
1

Try this one We have to set 2 conditions

$min =  100; //KB
$max = 500; //KB

if($_FILES['myfile']['size'] < $min * 1024 || $_FILES['myfile']['size'] > $max * 1024){
 echo 'error';
}

Darshan Malani
  • 478
  • 3
  • 12