Please note that I want the compartment number to change.
<?php
$compartment = "1";
/* HERE I NEED SOME SCRIPT TO FIND THE EXTENSION OF THE FILE NAME $compartment AND TO SAVE THAT AS A VARIABLE NAMED 'EXTENSION'.*/
if (file_exists($compartment.$extension)) {
echo "$compartment.$extension exists!
} else {
echo "No file name exists that is called $compartment. Regardless of extension."
}
?>
<?php
$compartment = "2";
/* HERE I NEED SOME SCRIPT TO FIND THE EXTENSION OF THE FILE NAME $compartment AND TO SAVE THAT AS A VARIABLE NAMED 'EXTENSION'.*/
if (file_exists($$compartment.$extension)) {
echo "$compartment.$extension exists!
} else {
echo "No file name exists that is called $compartment. Regardless of extension."
}
?>
Thank You!
You need glob().
$compartment = "2";
$files = glob("/path/to/files/$compartment.*"); // Will find 2.txt, 2.php, 2.gif
// Process through each file in the list
// and output its extension
if (count($files) > 0)
foreach ($files as $file)
{
$info = pathinfo($file);
echo "File found: extension ".$info["extension"]."<br>";
}
else
echo "No file name exists called $compartment. Regardless of extension."
by the way, what you are doing above is crying for a loop. Don' repeat your code blocks, but wrap one of them into this:
$compartments = array(1, 3, 6, 9); // or whichever compartments
// you wish to run through
foreach ($compartments as $compartment)
{
..... insert code here .......
}
Look up:
glob — Find pathnames matching a pattern
fnmatch — Match filename against a pattern
pathinfo — Returns information about a file path
Related
I want to rename the latest added file from a folder, but somehow my code isn't working. Could please help!
For example, if the latest file is "file_0202.json"
And I want to Change it to "file.json"
Here is my Code
<?php
$files = scandir('content/myfiles', SCANDIR_SORT_DESCENDING);
$selected_file = $files[0];
$new_filename = preg_replace('/_[^_.]*\./', '.', $selected_file);
if(rename($selected_file, $new_filename, ))
{
echo 'renamed';
}
else {
echo 'can not rename';
}
?>
It's better if you use glob(). glob() returns the path and filename used.
Then you have to sort by the file that was last changed. You can use usort and filemtime for that.
$files = glob('content/myfiles/*.*');
usort($files,function($a,$b){return filemtime($b) <=> filemtime($a);});
$selected_file = $files[0];
$new_filename = preg_replace('/_[^_.]*\./', '.', $selected_file);
if(rename($selected_file, $new_filename))
{
echo 'renamed';
}
else {
echo 'can not rename';
}
Instead of *.* you can restrict the searched files if necessary. As an example *.json . Be careful with your regular expression so that it doesn't change a path.
if(unlink('./'.date('m-d-Y').'/'.$file))
{
echo "file named $file has been deleted successfully";
}
else
{
echo "file is not deleted";
}
I have the code above to delete my files.
However, is it possible to only let it delete files that contain ".5010."
Can I do something like "%.5010.%" or something, also if its more than one file do I need to put it in a while or a foreach loop? Because now it deletes all my files.
You can use glob() to find the pathnames matching a pattern.
So your code would look something like this,
$path="./".date('m-d-Y')."/*".$file."*";
$files = glob($path); // this will return multiple files.
foreach ($files as $file) {
unlink($file);
}
glob() returns an array containing the matched files/directories, an empty array if no file matched or FALSE on error.
You can loop on file with glob and after use preg_match to select files who match a pattern
<?php
foreach(glob('path/to/dir/*.*') as $file) {
if (preg_match('*.5010.*', $file)) {
unlink($file)
}
}
I have 100 files and I am scanning them and picking the correct file out of them.
I am using the following code:
$dir = 'myDir';
$files1 = scandir($dir);
$scanned_directory = array_diff($files1, array('..', '.'));
foreach ($scanned_directory as $key => $value) {
$onlyname=explode(".", $value);
if($onlyname[0]== $name){
// echo "file found";
break;
}else{
//echo "<h2>Not Found. Please Try Later</h2>";
}
}
The problem with this is that if the file is the 10th file I get 9x not found, before I get the file found message.
What is the proper way to display error message if no file is found?
I simplified your code a bit.
First of all I get all files from your directory into an array with glob(). Then I simply grab all files which have the name $name with preg_grep() and check with count() if there is at least 1 file with that specific name.
<?php
$dir = "myDir";
$files = glob($dir . "/*.*");
if(count(preg_grep("/^$name\..*$/", array_map("basename", $files))) > 0)
echo "file found";
else
echo "<h2>Not Found. Please Try Later</h2>";
?>
I've been making a function to scan files in a directory and scan for a specific file in the one of the folders in the scanned directory. To put it in simple terms,
Scanned Director -> Scan Folder in Director for specific file
Here's the code I've been using.
function __construct(){
$name = array_diff(scandir($_SERVER['CONTEXT_DOCUMENT_ROOT']."/apps/"), array('..', '.'));
foreach($name as $key)
{
$this->installed = array($key);
while($this->check($key)){}
}
}
function check($name){
if(file_exists($_SERVER['CONTEXT_DOCUMENT_ROOT']."/apps/".$name."/index.php"))
{
echo "YES";
}
else
{
echo "NO";
}
}
but the output returns "NO" even if the file is already in the folder. Is there a solution to this?
Please check the below points again and proceed.
Please check the file path
Your folder and file must have permission to read.
file_exists($filename) function will return FALSE for symlinks pointing to non-existing files.
Your $name variable returns an array you have used that in the file_exists($filename)
I have made some modification in the code and i have checked its working fine here.
$name = array_diff(scandir($_SERVER['CONTEXT_DOCUMENT_ROOT']."/apps/"), array('..', '.'));
foreach($name as $key) {
$this->installed = array($key);
while($this->check($key)){}
if(file_exists($_SERVER['CONTEXT_DOCUMENT_ROOT']."/apps/".$key."/index.php")){
echo "YES";
}else{
echo "NO";
}
}
I'm trying to find if a file exists. I know the name of it but I do not know the extension. What could I do PHP wise so that the file exists function checks for the file without knowing it's extension?
file_exists('image_storage/ses_' . $session_user_id . 'need to put something here for the
extension' );
You could use PHP's glob function to get a list of files that match a given pattern:
$files = glob('image_storage/ses_' . $session_user_id . '.*');
if (count($files) > 0) {
// check your files with a loop
foreach ($files as $file) {
// do whatever you want; this file exists =]
}
}
You won't need to check if the file exists with glob; if it returns it in the array, it should exist.
If you are in linux you can do this..
$ret = exec("ls image_storage/ses_" . $session_user_id."*");
if(!empty($ret))
{
//file exists..
}