現在、PHP ページにデータベースのファイル名を表示しています。ただし、サーバーのフォルダーの一部のファイル名は大文字と小文字が異なります。そのため、データベースでは image1.jpg と表示され、サーバー上のファイル名は大文字で "image1.JPG" と表示される場合があります。これは、一部のファイルでランダムです。これらのファイルは表示されません。関数を使用して表示できるようにする方法はありますか。ここでは、1000 以上のファイルについて話しています。したがって、どんな助けでも大歓迎です。
1506 次
3 に答える
5
カスタムfile_exists()関数を実行して、画像の拡張子がどのような場合かを確認します。
このカスタム関数を使用して、正しい大文字小文字をチェックします(小文字を渡し、1を返す場合は小文字を使用し、2を返す場合は大文字を使用します)。
function file_exists_case($strUrl)
{
$realPath = str_replace('\\','/',realpath($strUrl));
if(file_exists($strUrl) && $realPath == $strUrl)
{
return 1; //File exists, with correct case
}
elseif(file_exists($realPath))
{
return 2; //File exists, but wrong case
}
else
{
return 0; //File does not exist
}
}
ただし、時間があれば、実際にファイル名拡張子をすべて小文字にする必要があります。
これを行う方法は、次のディレクトリでglob()を実行することです :http: //php.net/manual/en/function.glob.php そしてstrtolower()を使用してすべてのファイル拡張子を小文字に変更します:http:// php.net/manual/en/function.strtolower.php
于 2013-01-27T02:15:13.270 に答える
1
拡張子を小文字に変換するオプションがあるかどうかはわかりません。しかし、大文字にする特定の拡張機能に依存するシステムが他にない場合は、次のように実行できます。
find . -name '*.*' -exec sh -c '
a=$(echo {} | sed -r "s/([^.]*)\$/\L\1/");
[ "$a" != "{}" ] && mv "{}" "$a" ' \;
于 2013-01-27T04:35:15.490 に答える
0
file_exists
チェックを行うために使用します。そして、それを拡張して、直面している問題を補います。replace_extension()
ここに示されている関数を使用しています。
<?php
// Full path to the file.
$file_path = '/path/to/the/great.JPG';
// Call to the function.
echo check_if_image_exists($file_path, $file_ext_src);
// The function itself.
function check_if_image_exists ($file_path) {
$file_ext_src = end(explode('.', $file_path));
if (file_exists($file_path)) {
return TRUE;
}
else {
if (ctype_lower($file_ext_src)) {
$file_ext_new = strtoupper($file_ext_src); // If lowercase make it uppercase.
}
else if (ctype_upper($file_ext_src)) {
$file_ext_new = strtolower($file_ext_src); // If uppercase make it lowercase.
}
// Now create a new filepath with the new extension & check that.
$file_path_new = replace_extension($file_path, $file_ext_new);
if (file_exists($file_path_new)) {
return TRUE;
}
else {
return FALSE;
}
}
}
// Nice function taken from elsewhere.
function replace_extension($filename, $new_extension) {
$info = pathinfo($filename);
return $info['filename'] . '.' . $new_extension;
}
?>
于 2013-01-27T02:32:42.603 に答える