Skip to content

Commit 7901330

Browse files
committed
Images now support unique file names
System can now read unique image file names to renew cached images when uploading a new one. #480
1 parent eef549b commit 7901330

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

app/Functions/functions.php

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,47 @@
11
<?php
22

3-
function findFile($name){
4-
$directory = base_path('/assets/linkstack/images/');
3+
function findFile($name)
4+
{
5+
$directory = base_path("/assets/linkstack/images/");
56
$files = scandir($directory);
67
$pathinfo = "error.error";
7-
foreach($files as $file) {
8-
if (strpos($file, $name.'.') !== false) {
9-
$pathinfo = $name. "." . pathinfo($file, PATHINFO_EXTENSION);
10-
}}
8+
$pattern = '/^' . preg_quote($name, '/') . '(_\w+)?\.\w+$/i';
9+
foreach ($files as $file) {
10+
if (preg_match($pattern, $file)) {
11+
$pathinfo = $file;
12+
break;
13+
}
14+
}
1115
return $pathinfo;
1216
}
1317

14-
function findAvatar($name){
15-
$directory = base_path('assets/img');
16-
$files = scandir($directory);
17-
$pathinfo = "error.error";
18-
foreach($files as $file) {
19-
if (strpos($file, $name.'.') !== false) {
20-
$pathinfo = "assets/img/" . $name. "." . pathinfo($file, PATHINFO_EXTENSION);
21-
}}
22-
return $pathinfo;
18+
function findAvatar($name)
19+
{
20+
$directory = base_path("assets/img");
21+
$files = scandir($directory);
22+
$pathinfo = "error.error";
23+
$pattern = '/^' . preg_quote($name, '/') . '(_\w+)?\.\w+$/i';
24+
foreach ($files as $file) {
25+
if (preg_match($pattern, $file)) {
26+
$pathinfo = "assets/img/" . $file;
27+
break;
28+
}
29+
}
30+
return $pathinfo;
2331
}
2432

25-
function findBackground($name){
26-
$directory = base_path('assets/img/background-img/');
33+
function findBackground($name)
34+
{
35+
$directory = base_path("assets/img/background-img/");
2736
$files = scandir($directory);
2837
$pathinfo = "error.error";
29-
foreach($files as $file) {
30-
if (strpos($file, $name.'.') !== false) {
31-
$pathinfo = $name. "." . pathinfo($file, PATHINFO_EXTENSION);
32-
}}
38+
$pattern = '/^' . preg_quote($name, '/') . '(_\w+)?\.\w+$/i';
39+
foreach ($files as $file) {
40+
if (preg_match($pattern, $file)) {
41+
$pathinfo = $file;
42+
break;
43+
}
44+
}
3345
return $pathinfo;
3446
}
3547

0 commit comments

Comments
 (0)