Solved

How to get from directory every file which has prefix "_th."?

Posted on 2009-07-12
19
388 Views
Last Modified: 2013-12-13
I've got.
$path[0] = URL.'v/'.$this->u['id'].'/'; $_name = _PATH.'v/'.$this->u['id'].'/';
		
		$i = 0; if($h = opendir($pth)) {
			while(false !== ($f = readdir($h))) {
				if($f <> '.' and $f <> '..' and substr($f,0,4) <> IMG_TH and substr($f,0,8) <> IMG_AV)
					if(is_readable($pth.$f))
						if(!is_dir($pth.$f)) {
							$stat = stat($pth.$f);
							$item['size'] = sprintf("%.2f",$stat['size']/1000);
							$x .= '<tr class=\'v\''; if($this->u['avatar'] == $f) $x .= 'style=\'background-color:#ccc\' '; $x .= 'onclick=\'javascript:_get("pic","'.$f.'");\'>
											<td valign=\'top\'><img src=\''.TPL.'img/dir/file.gif\' alt=\'\' /></td>
											<td valign=\'top\'><div class=\'fl\'><a href=\'#\' onclick=\'javascript:_get("pic","'.$f.'");\'>'.$f.'</a><div class=\'clearfix\' id=\''.$i.'\' style=\'display:none;\'>Do you want to delete picture?&nbsp;&nbsp;&nbsp;&nbsp;<a href=\''.URL.'profile/photo/d/'.$f.'\'>Yes</a>&nbsp;&nbsp;<a href=\''.URL.'profile/photo/\'>No</a></div></div><div class=\'fr\'>'.$item['size'].'kb&nbsp;&nbsp;&nbsp;&nbsp;</div></div class=\'fix\'></div></td>
											<td valign=\'top\'>'._ago($stat['mtime']).' ago</td>
											<td valign=\'top\' class=\'sp4\'><img src=\''.TPL.'img/ico/del2.gif\' class=\'pointer\' id=\'pp'.$i.'\' alt=\'delete picture\' /> <a href=\''.URL.$this->u['id'].'/p/'.substr($f,0,-4).'\'>&rarr;</a></td>
										</tr>
										<script type=\'text/javascript\'>$(document).ready(function(){$("#pp'.$i.'").click(function(){$("#'.$i.'").toggle("slow")})});</script>';
							$i++;
							
							if(!file_exists($_name.IMG_TH.$f)) include_once(URL.DIR_LIB.'thumb/'.INX.PHP.'?f='.$path[0].$f.'&w='.IMG_.'&h='.IMG_.'&id='.$this->u['id'].'&t='.$f.'&pre=th');
						}
			} closedir($h);
		} $x .= '</table></div>'; return $x;

Open in new window

0
Comment
Question by:AndyPSV
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
  • 3
19 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24836273
I think you might want to use scandir() and iterate over the results set.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24836282
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24836291

<?php // RAY_temp_scandir.php
error_reporting(E_ALL);
 
$file_array = scandir( getcwd() );
$my_files = array();
 
foreach ($file_array as $name)
{
    if (substr($name,0,4) == '_th.')
    {
        $my_files[] = $name;
    }
}
 
var_dump($my_files);

Open in new window

0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 6

Expert Comment

by:basic612
ID: 24836302
Use glob to return an array of file names

$files = glob('_th.*');

0
 
LVL 6

Assisted Solution

by:basic612
basic612 earned 100 total points
ID: 24836307
Use glob to return an array of file names

$files = glob('_th.*');

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24836324
@basic612:  Good point.  I always forget about glob(), but it is an easy way to get the information.  

I stopped using it when I had some special characters in the file names and it was having trouble with that.  Also, I'm fairly sure that scandir() and glob() do not touch() the files, but it might be worth verifying that if the date of last use matters to the OP.

Best to all, ~Ray
0
 

Author Comment

by:AndyPSV
ID: 24836388
shit
Fatal error: Call to undefined function: scandir() in C:\Program Files\WebServ\httpd\mod\u\index.php on line 25
0
 

Author Comment

by:AndyPSV
ID: 24836389
anybody knows how to modify the sample above?
0
 
LVL 6

Expert Comment

by:basic612
ID: 24836397
try this one:

$files = glob('_th.*');
print_r($files);

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24836401
Good grief!  What level of PHP are you using?
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24836409
Can you please tell us the values for the defined constants and the undefined variables in that script?
0
 

Author Comment

by:AndyPSV
ID: 24836410
I get:

Array
(
    [0] => C:\Program Files\WebServ\httpd\v/1/_th.AndyPSV.gif
    [1] => C:\Program Files\WebServ\httpd\v/1/_th.AndyPSVavatar.gif
    [2] => C:\Program Files\WebServ\httpd\v/1/_th.AndyPSVavatar.jpg
    [3] => C:\Program Files\WebServ\httpd\v/1/_th.AndyPSVavatar2.gif
    [4] => C:\Program Files\WebServ\httpd\v/1/_th.AndyPSVavatar2_copy.gif
    [5] => C:\Program Files\WebServ\httpd\v/1/_th.images_11.jpg
    [6] => C:\Program Files\WebServ\httpd\v/1/_th.images_5.jpg
    [7] => C:\Program Files\WebServ\httpd\v/1/_th.images_6.jpg
    [8] => C:\Program Files\WebServ\httpd\v/1/_th.images_7.jpg
    [9] => C:\Program Files\WebServ\httpd\v/1/_th.images_8.jpg
)

But, how to get: http://localhost....../
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24836411
"But, how to get: http://localhost....../"

Do you mean how to turn the directory addresses into links?
0
 

Author Comment

by:AndyPSV
ID: 24836413
_PATH = C:\Program Files\WebServ\httpd\
URL = http://localhost/
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24836415
Sorry - the building is being closed, so I have to leave now.  I will look at it again in the morning.  Best regards, ~Ray
0
 

Author Comment

by:AndyPSV
ID: 24836417
Yes, str_replace?
0
 

Author Comment

by:AndyPSV
ID: 24836418
I want to be universal, after uploading to other linux machine etc
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 400 total points
ID: 24836419
Yes, almost certainly.  Use foreach() to iterate over the array.

Gotta go!
0
 

Author Comment

by:AndyPSV
ID: 24836431
$files = glob('v/'.$this->u['id'].'/'.'_th.*');
print_r($files);

Array
(
    [0] => v/1/_th.AndyPSV.gif
    [1] => v/1/_th.AndyPSVavatar.gif
    [2] => v/1/_th.AndyPSVavatar.jpg
    [3] => v/1/_th.AndyPSVavatar2.gif
    [4] => v/1/_th.AndyPSVavatar2_copy.gif
    [5] => v/1/_th.images_11.jpg
    [6] => v/1/_th.images_5.jpg
    [7] => v/1/_th.images_6.jpg
    [8] => v/1/_th.images_7.jpg
    [9] => v/1/_th.images_8.jpg
)

now it's brilliant
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question