jakebur01 Posted March 6, 2009 Share Posted March 6, 2009 I was trying to add if statements into the while loop to see if it is an image. But, I couldn't not make it work. I am only wanting to display images here. Right now it is listing all the files now. When it lists the files, it list one . and the second line is .. $image_path = dirname(__FILE__) . "/gallery/$_SESSION[valid_user]/uploads"; $dir_handle = null; if (!($dir_handle = opendir($image_path))) { //trigger_error('error, path not found'); return; } $html = ''; $file = readdir($dir_handle); while ($file) { $html .= "<IMG SRC='/gallery/$_SESSION[valid_user]/uploads/{$file}' width='100' align='top' vspace='2' alt='{$file}' />{$file}<br />\r\n"; $file = readdir($dir_handle); } closedir($dir_handle); // $html .= '<input type="submit" name="submit" value="Submit" />'; // $html .= "</form></html>"; echo $html; Quote Link to comment https://forums.phpfreaks.com/topic/148195-solved-file-type-check/ Share on other sites More sharing options...
ratcateme Posted March 6, 2009 Share Posted March 6, 2009 try a if like this //add this line to the top $image_types = array("jpg","jpeg","gif","png"); //add this inside your while if(in_array(substr($text,strpos($text,".")+1),$image_types)){ //display image } also the loop should work like while (false !== ($file = readdir($handle))) { Scott. Quote Link to comment https://forums.phpfreaks.com/topic/148195-solved-file-type-check/#findComment-777896 Share on other sites More sharing options...
jakebur01 Posted March 6, 2009 Author Share Posted March 6, 2009 I made the changes. It is not displaying any errors. It is also not displaying any images. <?php //$file_ary = isset($_POST['rfile']) ? $_POST['rfile'] : array(); $image_path = dirname(__FILE__) . "/gallery/$_SESSION[valid_user]/uploads"; $dir_handle = null; if (!($dir_handle = opendir($image_path))) { //trigger_error('error, path not found'); return; } $html = '<table width="500">'; $file = readdir($dir_handle); $image_types = array("jpg","jpeg","gif","png"); while (false !== ($file = readdir($dir_handle))) { if(in_array(substr($text,strpos($text,".")+1),$image_types)){ $html .= "<tr><td><center><IMG SRC='/gallery/$_SESSION[valid_user]/uploads/{$file}' width='100' align='top' vspace='2' alt='{$file}' /></center></td><td>{$file}</td></tr>"; $file = readdir($dir_handle); } } closedir($dir_handle); // $html .= '<input type="submit" name="submit" value="Submit" />'; $html .= "</table>"; echo $html; if (!empty($file_ary)) { echo '<pre>'; print_r($file_ary); echo '</pre>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/148195-solved-file-type-check/#findComment-777898 Share on other sites More sharing options...
ratcateme Posted March 6, 2009 Share Posted March 6, 2009 sorry got it a little wrong try if(in_array(substr($file,strpos($file,".")+1),$image_types)){ needed to replace $text with $file Scott. Quote Link to comment https://forums.phpfreaks.com/topic/148195-solved-file-type-check/#findComment-777901 Share on other sites More sharing options...
trq Posted March 6, 2009 Share Posted March 6, 2009 Simply checking the file extension does not guarantee the file is an image. Use getimagesize instead. This will return false if the file is not an image, or an array if the file is. Quote Link to comment https://forums.phpfreaks.com/topic/148195-solved-file-type-check/#findComment-777931 Share on other sites More sharing options...
ratcateme Posted March 6, 2009 Share Posted March 6, 2009 i use that for images uploaded but this is for images all ready on the local file system wouldn't it be safe to assume they are all proper images? Scott. Quote Link to comment https://forums.phpfreaks.com/topic/148195-solved-file-type-check/#findComment-777945 Share on other sites More sharing options...
trq Posted March 6, 2009 Share Posted March 6, 2009 Yeah, I guess. Didn't really even look at the post This would be a job for glob. Quote Link to comment https://forums.phpfreaks.com/topic/148195-solved-file-type-check/#findComment-777982 Share on other sites More sharing options...
ratcateme Posted March 6, 2009 Share Posted March 6, 2009 something i have always wondered about glob can you use it to match files with different extensions. so like with one glob could you get files that are .jpg, .jpeg, .gif, .png or would that require 4 glob's joined? Scott. Quote Link to comment https://forums.phpfreaks.com/topic/148195-solved-file-type-check/#findComment-777989 Share on other sites More sharing options...
trq Posted March 6, 2009 Share Posted March 6, 2009 $imgs = glob('{*.jpg,*.jpeg,*.gif,*.png}', GLOB_BRACE); Quote Link to comment https://forums.phpfreaks.com/topic/148195-solved-file-type-check/#findComment-778011 Share on other sites More sharing options...
jakebur01 Posted March 6, 2009 Author Share Posted March 6, 2009 I am having trouble integrating the glob() into my script. I do not know how to use it. $image_path = dirname(__FILE__) . "/gallery/$_SESSION[valid_user]/uploads"; $dir_handle = null; if (!($dir_handle = opendir($image_path))) { //trigger_error('error, path not found'); return; } $html = '<table width="500">'; $file = readdir($dir_handle); $imgs = glob('{*.jpg,*.jpeg,*.gif,*.png}', GLOB_BRACE); while (false !== ($file = readdir($dir_handle))) { if($file==$imgs){ $html .= "<tr><td><center><IMG SRC='/gallery/$_SESSION[valid_user]/uploads/{$file}' width='100' align='top' vspace='2' alt='{$file}' /></center></td><td>{$file}</td></tr>"; $file = readdir($dir_handle); } } closedir($dir_handle); // $html .= '<input type="submit" name="submit" value="Submit" />'; $html .= "</table>"; echo $html; if (!empty($file_ary)) Quote Link to comment https://forums.phpfreaks.com/topic/148195-solved-file-type-check/#findComment-778199 Share on other sites More sharing options...
kickstart Posted March 6, 2009 Share Posted March 6, 2009 Hi In case the image name on the server is in a different case I think you would need to use if(in_array(strtolower(substr($file,strpos($file,".")+1)),$image_types)){ All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/148195-solved-file-type-check/#findComment-778216 Share on other sites More sharing options...
jakebur01 Posted March 6, 2009 Author Share Posted March 6, 2009 That displayed only 3 images. It is missing 13330MarThu2009.jpg, 100_0383.JPG, 100_0388.JPG. The three it displayed were 100_0381.JPG, 100_0384.JPG, 100_0389.JPG. $file = readdir($dir_handle); $image_types = array("jpg","jpeg","gif","png"); while (false !== ($file = readdir($dir_handle))) { if(in_array(strtolower(substr($file,strpos($file,".")+1)),$image_types)){ $html .= "<tr><td><center><IMG SRC='/gallery/$_SESSION[valid_user]/uploads/{$file}' width='100' align='top' vspace='2' alt='{$file}' /></center></td><td>{$file}</td></tr>"; $file = readdir($dir_handle); } } closedir($dir_handle); Quote Link to comment https://forums.phpfreaks.com/topic/148195-solved-file-type-check/#findComment-778229 Share on other sites More sharing options...
kickstart Posted March 6, 2009 Share Posted March 6, 2009 Hi Can see one issue. You have a $file = readdir($dir_handle); at the start and then do it in the loop. The first will get the first file name, and then get the 2nd when the loop starts (so the first would be ignored). However not sure this will be an issue (might find a . or .. directory). Other possibility is that the file name has a space at the end. Not ever had that, bit seems possible and would confuse the script. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/148195-solved-file-type-check/#findComment-778244 Share on other sites More sharing options...
jakebur01 Posted March 6, 2009 Author Share Posted March 6, 2009 Ha.. That got it. Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/148195-solved-file-type-check/#findComment-778254 Share on other sites More sharing options...
trq Posted March 6, 2009 Share Posted March 6, 2009 I am having trouble integrating the glob() into my script. I do not know how to use it. <?php $dir = dirname(__FILE__) . "/gallery/{$_SESSION['valid_user']}/uploads"; foreach (glob("$dir/{*.jpg,*.jpeg,*.gif,*.png}", GLOB_BRACE) as $file) { echo "<tr><td><center><IMG SRC='/gallery/{$_SESSION['valid_user']}/uploads/{$file}' width='100' align='top' vspace='2' alt='{$file}' /></center></td><td>{$file}</td></tr>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/148195-solved-file-type-check/#findComment-778541 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.