Jump to content

Find Similar Named Files on Remote Server


ezpickinz

Recommended Posts

I have an image name (pic01.jpg) and the directory it lives in on the remote server ( I also have permission to be there). I know that in many cases there will be a small stack of images that together make a set like, pic01.jpg, pic01_blue.jpg, pic01_yk7z.jpg, pic01_8m.jpg...

 

I know I can use curl to get a response code but then I'd have to know the name of the file first. I believe this will be a small job using Regex. Trouble is every time I begin to read about Regex my eyes glaze over so I'm really not sure.

 

Your help is greatly appreciated.

 

John

Link to comment
Share on other sites

Well here's what I have so far. This code does function! It still needs some work but I've no work left in me this evening. I'm testing on my local it will work on a remote server.

<?php
function get_url_contents($url){
        $crl = curl_init();
        $timeout = 5;
        curl_setopt ($crl, CURLOPT_URL,$url);
        curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
        $ret = curl_exec($crl);
        curl_close($crl);
        return $ret;
}
$imagename = "VTKBC55";
$namesplit = "_";
$extension = ".jpg";
$imgsArray = array();
$image_dir = 'localhost/phpTest/images/';
$pageHTML = get_url_contents($image_dir);
$doc = new DOMDocument();
@$doc->loadHTML($pageHTML);
$imgTags = $doc->getElementsByTagName('a');
		foreach ($imgTags as $img) {
			$img = pathinfo($img->getAttribute('href'));
	$imgsArray[] = $img['filename'];

echo "FILES: " . $img['filename'] . "<br />";

}



foreach($imgsArray as $img){
				if(preg_match('/'.$imagename.$namesplit.'*?/',$img,$matches)){
				echo "Matched: " . $img . '<br />';
				print_r($matches);
				echo "<br />";

				}
}
?>
Link to comment
Share on other sites

So here's where I'm at now. The trouble is that it will match anything after filename_ even when the extension changes.

 

How can I say:

preg_match('/^'.$imagename.$namesplit.      ' *? $img[extension]  /'        ,$this_img_name,$matches)    ????

		foreach ($imgTags as $img) {
			$img = pathinfo($img->getAttribute('href'));
	$imgsArray[] = array('filename'=>$img['filename'],'extension'=>$img['extension']);

//echo "FILES: " . $img['filename'] . "<br />";

}
print_r($imgsArray);

echo "<br /><br /><br /><br /><br />";

foreach($imgsArray as $img){
			$this_img_name = $img['filename']. $img['extension'];
				if(preg_match('/^'.$imagename.$namesplit.'/',$this_img_name,$matches)){
				echo "Matched: " . $img['filename'] . '.' . $img['extension'] . '<br />';
				print_r($matches);
				echo "<br />";

				}

				
}
Link to comment
Share on other sites

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.