Php 如何选择大小在100-500 kb之间的图像?
我有一个简短的PHP代码来帮助我显示特定文件夹中的随机图像。但现在它似乎选择了任何大小的任何图像。我希望这些选定的图像介于100-500 kb之间。如果小于100KB或大于500KB,函数将不会选择并显示它。 你能告诉我如何修改这个代码吗?可能需要添加一些函数Php 如何选择大小在100-500 kb之间的图像?,php,Php,我有一个简短的PHP代码来帮助我显示特定文件夹中的随机图像。但现在它似乎选择了任何大小的任何图像。我希望这些选定的图像介于100-500 kb之间。如果小于100KB或大于500KB,函数将不会选择并显示它。 你能告诉我如何修改这个代码吗?可能需要添加一些函数 <?php $randomdir = dir('images/random'); $count = 1; $pattern="/(gif|jpg|jpeg|png)/"; while($file = $rando
<?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 />';
?>
现在试试
<?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--;
}
?>
现在试试
<?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--;
}
?>
试试这个,我们必须设置两个条件
$min = 100; //KB
$max = 500; //KB
if($_FILES['myfile']['size'] < $min * 1024 || $_FILES['myfile']['size'] > $max * 1024){
echo 'error';
}
$min=100//KB
$max=500//KB
如果($_文件['myfile']['size']<$min*1024 |$_文件['myfile']['size']>$max*1024){
回声“错误”;
}
试试这个,我们必须设置两个条件
$min = 100; //KB
$max = 500; //KB
if($_FILES['myfile']['size'] < $min * 1024 || $_FILES['myfile']['size'] > $max * 1024){
echo 'error';
}
$min=100//KB
$max=500//KB
如果($_文件['myfile']['size']<$min*1024 |$_文件['myfile']['size']>$max*1024){
回声“错误”;
}
Hi@WiatroBosy谢谢你的帮助。但我不知道是哪个错误,它仍然不起作用。我猜是这行的错误:$file\u size\u kb=round(filesize($file)/1024;什么错误?显示这个error@JitendraYadav更正-检查nowHi@Jitendra Yadav,我尝试了你的两种解决方案。页面上说“此页面包含以下错误:第1列第2行的错误:下面文档末尾的额外内容是第一个错误之前的页面呈现。您好@WiatroBosy谢谢您的帮助。但我不知道是哪个错误,它仍然不起作用。我猜是这一行的错误:$file\u size\u kb=round(filesize($file)/什么错误?显示这个error@JitendraYadav已更正-检查nowHi@Jitendra Yadav,我尝试了两种解决方案。页面显示“此页面包含以下错误:第1列第2行错误:下面文档末尾的额外内容是第一个错误之前的页面呈现。