Jump to content

Recommended Posts

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;

 

 

Link to comment
https://forums.phpfreaks.com/topic/148195-solved-file-type-check/
Share on other sites

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.

 

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>';
}

			?>

 

 

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))

 

 

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);

 

 

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

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>";
}

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.