Jump to content

Recommended Posts

This script will search my server for files starting by a specific string, then return the download links. Search query is defined by "f" GET value.

 

Now let's say that i have the following files on my server:

 

/folder/example file number one.zip

/folder/example file number two.zip

/folder/example file number three.zip

 

If i search for "example file" then the script will return 3 results BUT every download links will be "/folder/example file" instead of the FULL filename.

 

This will also create a bug with the filesize() function at the end of the script, since filesize() will look for the size of "/folder/example file" instead of using the full filename

 

Can you help me to fix that ?

$request = $_GET['f'];
$adr = $_SERVER['QUERY_STRING'];
$decode = rawurldecode(substr($adr, 2));

echo "Searching for $decode";

// finding the file on the server
	$root = $_SERVER['DOCUMENT_ROOT'];
	$search = preg_quote(utf8_decode($decode));

		function rsearch($folder, $pattern) {
			$dir = new RecursiveDirectoryIterator($folder);
			$ite = new RecursiveIteratorIterator($dir);
			$files = new RegexIterator($ite, $pattern, RegexIterator::GET_MATCH);
			$fileList = array();
			foreach($files as $file) {
				$fileList = array_merge($fileList, $file);
			}
			return $fileList;
		}
		$resultatss = rsearch($root, '/.*\/'.$search.'/');


foreach ($resultatss as $resultat) {
$downloadlink = str_replace("$root/", "", $resultat);
$pos = strrpos($downloadlink, '/') + 1;
$encodedownloadlink = substr($downloadlink, 0, $pos) . rawurlencode(substr($downloadlink, $pos));

	if (!empty($downloadlink)) {
	echo "download link = http://www.mydomain.com/$encodedownloadlink";
	} else {
	echo "File not found";
	}

	$taillekb = filesize($downloadlink) / 1024;
	echo "<br>Size: $taillekb KB<br>";
}

Thanks a lot!

Link to comment
https://forums.phpfreaks.com/topic/279769-file-search-issue/
Share on other sites

@AbraCadaver: not working :(

 

@Barand: the problem is that the script returns incomplete filenames.

 

If i have a file called "test123456789.zip" on my server and i use the script to search for "test12345" it will return 1 match but the script will print "test12345" instead of printing "test123456789.zip"

 

Not sure how i could use realpath() to get the full filename of "test12345"

Edited by ungovernable
Link to comment
https://forums.phpfreaks.com/topic/279769-file-search-issue/#findComment-1439476
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.