php scandir for given $q search query - php

i have search values like "search value" (defined as $q) and .txt files called "search value.txt" in /mydir/ directory. How can i scandir /mydir/ for the search value ($q) and put the found .txt file with php include command into page? Or is it a better way to put $q value into php file_get_contents php code (i mean put them together like a txt filename like (q$.txt - searchvalue.txt somehow) and pull the content of the .txt file into page? If yes, how? Thanks in advance.

Suppose you have this directory structure :
mydir
--file1.tx
--file2.tx
index.php
index.php
$filepath = glob("mydir/*.txt");//read all txt files in data directory
//print_r($filepath);//debug purpose
$search = 'file1';
// Browse all files and search the $search string in filenames
foreach ($filepath as $filename) {
// Give us file name only without the extension
$fileNameOnly = basename($filename, '.txt');
// Check if a file name only contains exactly our keyword $search
$isExactMatch = preg_match("/\b".$search."\b/iu", $fileNameOnly);
if ( $isExactMatch ) {
// Include the .txt file as result
require $filename;
}
}
Tested, it does the trick.

Related

PhP get content of all files in directory

I have many files in a directory that look similar to:
personalchat.spud.3353.1789
personalchat.guest.3355.1789
personalchat.ken.3355.1789
each file essentially has a few lines of html inside, and wanting to open all files, pull the content to the screen. I can do it by hardcoding a single filename but not all at once or by using a * for filename
Code:
$names=file('/var/www/html/web/www/chat.support/status/personalchat*');
foreach($names as $name)
{
echo $name.'<br>';
}
##UPATE 2
I tried changing to an array but it only displays the name of the files not the content of each:
I am able to list the files as an array:
personalchat.mick.3350.1733
personalchat.guest.3352.1739
personalchat.test.3351.1736
But not able to display the name of each file but not the content:
$mydir = '/var/www/html/web/www/chat.support/status/';
$myfiles = array_diff(scandir($mydir), array('.', '..'));
foreach($myfiles as $name)
{
echo $name.'<br>';
}
Thanks
file() function reads 1 file. So you need an array of files to read with file().
May be you can try to create such array using scandir() first, and then read the file 1 by one.

Search for file in folder using php

I am storing user uploaded files like pdf images and txt files in separate folders using my php script i want to retrieve the file names from the folder upload and give the pdf and txt in a group and also way to search for specific file.
I also need to rename the file before to $ja variable
$ja
$da = date("dmY");
$ja = $uid.$da;
move_uploaded_file($mi, $uploadpath)
also used this code which i found in stack
Example 01:
<?php
// read all files inside the given directory
// limited to a specific file extension
$files = glob("./ABC/*.txt");
?>
Example 02:
<?php
// perform actions for each file found
foreach (glob("./ABC/*.txt") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}
?>
the scandir(); function will help you
<?php
$dir = '/tmp';
$files1 = scandir($dir);
print_r($files1);
?>
http://www.php.net/manual/en/function.scandir.php
now you have array of all files in a location you specified you can use array functions to get your work done
you can try listing the directory with scandir, and then filter as you want in the php array of filenames you will get

PHP : List Files of Type in Directory and Link to them

I've got the code
$directory = "C:/My file path";
$phpfiles = glob($directory . "*.html");
foreach($phpfiles as $phpfiles)
{
echo $phpfiles;
}
But how would I change it so that it doesn't just list the files, but actually links to them?
First of all, don't use same variable names at foreach(). You can link to files, like this.
foreach($phpfiles as $phpfile)
{
echo "<a href=$phpfile>".basename($phpfile)."</a>";
}
$phpfile containing full path of file (for example : /home/eray/Desktop/test.html)
basename() is returning just file name from path . basename($phpfile)'s output is test.html . If you want to print just test (without .html extension) , you can use this : basename($phpfile, ".html") Thanks, #aSeptik.
Assuming that the links are accessible via a web server you'll need a different root path for web access than you have on your computer. Also, your foreach is wrong. The second variable needs to be singular (well, at least different than the first). So assuming your web server sees the file path as a valid site path:
$rootPath = "/MyFilePath";
foreach ($phpfiles as $phpfile)
{
echo "$phpfile";
}
$files = glob("*.html");
echo '<ul>'.implode('', array_map('sprintf', array_fill(0, count($files), '<li>%s</li>'), $files, $files)).'</ul>';
This is ok "eray"
$phpfile containing full path of file (for example :
/home/eray/Desktop/test.html) basename() is returning just file name
from path . basename($phpfile)'s output is test.html . If you want to
print just test (without .html extension) , you can use this :
basename($phpfile, ".html") Thanks, #aSeptik.
how to remove. php extension and in the link.
exaple:
http//example.com/dir1/file.php **
(with out .php on end).
Thanks

How can I replace multiple PHP files in different directories with an updated PHP file using PHP

How can I replace multiple PHP files in different directories with the same PHP file using PHP for example, How do I replace all the index.php files in Example 1 with the index.php file in Example 2
Example 1 url values
/files/user-2/a/index.php
/files/user-12/a/index.php
/files/user-23/a/index.php
/files/user-232/a/index.php
/files/user-2232/a/index.php
Example 2 url values
/files/user-2/a/index.php
/files/user-12/a/index.php
/files/user-23/a/index.php
/files/user-232/a/index.php
/files/user-2232/a/index.php
You should use the rename function ( http://php.net/manual/en/function.rename.php )
To use it, begin by parsing the dir content :
function renameFilesInDir($dirName) {
$dir = opendir($dirName);
while ($file = readdir($dir)) {
if (is_file($dirName.$file)) {
// Rename the File
}
}
closedir($dir);
}
Good Luck !

How we can read zip file and get information of files or folders contains without unzipping in PHP?

What I actually wanted to do is read zip file and then if it does contain folder then refuse it with some message.
I want user should upload zip file with files only without any directory structure.
So I want to read zip file contains and check file structure.
I am trying with following code snippet.
$zip = zip_open('/path/to/zipfile');
while($zip_entry = zip_read($zip)){
$filename = zip_entry_name($zip_entry);
//#todo check whether file or folder.
}
I have sorted out.
I am now checking filename as strings wherever I am getting string ending with "/" that am treating as directory else as file.
can't you parse path of $filename? something like $dirName = pathinfo($filename, PATHINFO_DIRNAME)

Categories