Changeset 16770 for branches/feature-module-update
- Timestamp:
- 2007/11/08 23:54:03 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/feature-module-update/data/class/util/SC_Utils.php
r16764 r16770 1390 1390 static $count = 0; 1391 1391 $count++; // 無限ループ回避 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1392 $dir = dirname($path); 1393 if(ereg("^[/]$", $dir) || ereg("^[A-Z]:[\\]$", $dir) || $count > 256) { 1394 // ルートディレクトリで終了 1395 return; 1396 } else { 1397 if(is_writable(dirname($dir))) { 1398 if(!file_exists($dir)) { 1399 mkdir($dir); 1400 GC_Utils::gfPrintLog("mkdir $dir"); 1401 } 1402 } else { 1403 SC_Utils::sfMakeDir($dir); 1404 if(is_writable(dirname($dir))) { 1405 if(!file_exists($dir)) { 1406 mkdir($dir); 1407 GC_Utils::gfPrintLog("mkdir $dir"); 1408 } 1409 } 1410 } 1411 } 1412 return; 1413 1413 } 1414 1414 … … 1465 1465 // 指定したフォルダ内のファイルを全て削除する 1466 1466 function sfDelFile($dir){ 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1467 if(file_exists($dir)) { 1468 $dh = opendir($dir); 1469 // フォルダ内のファイルを削除 1470 while($file = readdir($dh)){ 1471 if ($file == "." or $file == "..") continue; 1472 $del_file = $dir . "/" . $file; 1473 if(is_file($del_file)){ 1474 $ret = unlink($dir . "/" . $file); 1475 }else if (is_dir($del_file)){ 1476 $ret = SC_Utils::sfDelFile($del_file); 1477 } 1478 1479 if(!$ret){ 1480 return $ret; 1481 } 1482 } 1483 1484 // 閉じる 1485 closedir($dh); 1486 1487 // フォルダを削除 1488 return rmdir($dir); 1489 } 1490 1490 } 1491 1491 … … 1640 1640 } 1641 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1642 /* 1643 * 関数名:sfGetFileList() 1644 * 説明 :指定パス配下のディレクトリ取得 1645 * 引数1 :取得するディレクトリパス 1646 */ 1647 function sfGetFileList($dir) { 1648 $arrFileList = array(); 1649 $arrDirList = array(); 1650 1651 if (is_dir($dir)) { 1652 if ($dh = opendir($dir)) { 1653 $cnt = 0; 1654 // 行末の/を取り除く 1655 while (($file = readdir($dh)) !== false) $arrDir[] = $file; 1656 $dir = ereg_replace("/$", "", $dir); 1657 // アルファベットと数字でソート 1658 natcasesort($arrDir); 1659 foreach($arrDir as $file) { 1660 // ./ と ../を除くファイルのみを取得 1661 if($file != "." && $file != "..") { 1662 1663 $path = $dir."/".$file; 1664 // SELECT内の見た目を整えるため指定文字数で切る 1665 $file_name = SC_Utils::sfCutString($file, FILE_NAME_LEN); 1666 $file_size = SC_Utils::sfCutString(SC_Utils::sfGetDirSize($path), FILE_NAME_LEN); 1667 $file_time = date("Y/m/d", filemtime($path)); 1668 1669 // ディレクトリとファイルで格納配列を変える 1670 if(is_dir($path)) { 1671 $arrDirList[$cnt]['file_name'] = $file; 1672 $arrDirList[$cnt]['file_path'] = $path; 1673 $arrDirList[$cnt]['file_size'] = $file_size; 1674 $arrDirList[$cnt]['file_time'] = $file_time; 1675 $arrDirList[$cnt]['is_dir'] = true; 1676 } else { 1677 $arrFileList[$cnt]['file_name'] = $file; 1678 $arrFileList[$cnt]['file_path'] = $path; 1679 $arrFileList[$cnt]['file_size'] = $file_size; 1680 $arrFileList[$cnt]['file_time'] = $file_time; 1681 $arrFileList[$cnt]['is_dir'] = false; 1682 } 1683 $cnt++; 1684 } 1685 } 1686 closedir($dh); 1687 } 1688 } 1689 1690 // フォルダを先頭にしてマージ 1691 return array_merge($arrDirList, $arrFileList); 1692 } 1693 1694 /* 1695 * 関数名:sfGetDirSize() 1696 * 説明 :指定したディレクトリのバイト数を取得 1697 * 引数1 :ディレクトリ 1698 */ 1699 function sfGetDirSize($dir) { 1700 if(file_exists($dir)) { 1701 // ディレクトリの場合下層ファイルの総量を取得 1702 if (is_dir($dir)) { 1703 $handle = opendir($dir); 1704 while ($file = readdir($handle)) { 1705 // 行末の/を取り除く 1706 $dir = ereg_replace("/$", "", $dir); 1707 $path = $dir."/".$file; 1708 if ($file != '..' && $file != '.' && !is_dir($path)) { 1709 $bytes += filesize($path); 1710 } else if (is_dir($path) && $file != '..' && $file != '.') { 1711 // 下層ファイルのバイト数を取得する為、再帰的に呼び出す。 1712 $bytes += SC_Utils::sfGetDirSize($path); 1713 } 1714 } 1715 } else { 1716 // ファイルの場合 1717 $bytes = filesize($dir); 1718 } 1719 } 1720 // ディレクトリ(ファイル)が存在しない場合は0byteを返す 1721 if($bytes == "") $bytes = 0; 1722 1723 return $bytes; 1724 } 1725 1726 /* 1727 * 関数名:sfDeleteDir() 1728 * 説明 :指定したディレクトリを削除 1729 * 引数1 :削除ファイル 1730 */ 1731 function sfDeleteDir($dir) { 1732 $arrResult = array(); 1733 if(file_exists($dir)) { 1734 // ディレクトリかチェック 1735 if (is_dir($dir)) { 1736 if ($handle = opendir("$dir")) { 1737 $cnt = 0; 1738 while (false !== ($item = readdir($handle))) { 1739 if ($item != "." && $item != "..") { 1740 if (is_dir("$dir/$item")) { 1741 sfDeleteDir("$dir/$item"); 1742 } else { 1743 $arrResult[$cnt]['result'] = @unlink("$dir/$item"); 1744 $arrResult[$cnt]['file_name'] = "$dir/$item"; 1745 } 1746 } 1747 $cnt++; 1748 } 1749 } 1750 closedir($handle); 1751 $arrResult[$cnt]['result'] = @rmdir($dir); 1752 $arrResult[$cnt]['file_name'] = "$dir/$item"; 1753 } else { 1754 // ファイル削除 1755 $arrResult[0]['result'] = @unlink("$dir"); 1756 $arrResult[0]['file_name'] = "$dir"; 1757 } 1758 } 1759 1760 return $arrResult; 1761 } 1762 1763 /* 1764 * 関数名:sfGetFileTree() 1765 * 説明 :ツリー生成用配列取得(javascriptに渡す用) 1766 * 引数1 :ディレクトリ 1767 * 引数2 :現在のツリーの状態開いているフォルダのパスが | 区切りで格納 1768 */ 1769 function sfGetFileTree($dir, $tree_status) { 1770 1771 $cnt = 0; 1772 $arrTree = array(); 1773 $default_rank = count(split('/', $dir)); 1774 1775 // 文末の/を取り除く 1776 $dir = ereg_replace("/$", "", $dir); 1777 // 最上位層を格納(user_data/) 1778 if(sfDirChildExists($dir)) { 1779 $arrTree[$cnt]['type'] = "_parent"; 1780 } else { 1781 $arrTree[$cnt]['type'] = "_child"; 1782 } 1783 $arrTree[$cnt]['path'] = $dir; 1784 $arrTree[$cnt]['rank'] = 0; 1785 $arrTree[$cnt]['count'] = $cnt; 1786 // 初期表示はオープン 1787 if($_POST['mode'] != '') { 1788 $arrTree[$cnt]['open'] = lfIsFileOpen($dir, $tree_status); 1789 } else { 1790 $arrTree[$cnt]['open'] = true; 1791 } 1792 $cnt++; 1793 1794 sfGetFileTreeSub($dir, $default_rank, $cnt, $arrTree, $tree_status); 1795 1796 return $arrTree; 1797 } 1798 1799 /* 1800 * 関数名:sfGetFileTree() 1801 * 説明 :ツリー生成用配列取得(javascriptに渡す用) 1802 * 引数1 :ディレクトリ 1803 * 引数2 :デフォルトの階層(/区切りで 0,1,2・・・とカウント) 1804 * 引数3 :連番 1805 * 引数4 :現在のツリーの状態開いているフォルダのパスが | 区切りで格納 1806 */ 1807 function sfGetFileTreeSub($dir, $default_rank, &$cnt, &$arrTree, $tree_status) { 1808 1809 if(file_exists($dir)) { 1810 if ($handle = opendir("$dir")) { 1811 while (false !== ($item = readdir($handle))) $arrDir[] = $item; 1812 // アルファベットと数字でソート 1813 natcasesort($arrDir); 1814 foreach($arrDir as $item) { 1815 if ($item != "." && $item != "..") { 1816 // 文末の/を取り除く 1817 $dir = ereg_replace("/$", "", $dir); 1818 $path = $dir."/".$item; 1819 // ディレクトリのみ取得 1820 if (is_dir($path)) { 1821 $arrTree[$cnt]['path'] = $path; 1822 if(sfDirChildExists($path)) { 1823 $arrTree[$cnt]['type'] = "_parent"; 1824 } else { 1825 $arrTree[$cnt]['type'] = "_child"; 1826 } 1827 1828 // 階層を割り出す 1829 $arrCnt = split('/', $path); 1830 $rank = count($arrCnt); 1831 $arrTree[$cnt]['rank'] = $rank - $default_rank + 1; 1832 $arrTree[$cnt]['count'] = $cnt; 1833 // フォルダが開いているか 1834 $arrTree[$cnt]['open'] = lfIsFileOpen($path, $tree_status); 1835 $cnt++; 1836 // 下層ディレクトリ取得の為、再帰的に呼び出す 1837 sfGetFileTreeSub($path, $default_rank, $cnt, $arrTree, $tree_status); 1838 } 1839 } 1840 } 1841 } 1842 closedir($handle); 1843 } 1844 } 1845 1846 /* 1847 * 関数名:sfDirChildExists() 1848 * 説明 :指定したディレクトリ配下にファイルがあるか 1849 * 引数1 :ディレクトリ 1850 */ 1851 function sfDirChildExists($dir) { 1852 if(file_exists($dir)) { 1853 if (is_dir($dir)) { 1854 $handle = opendir($dir); 1855 while ($file = readdir($handle)) { 1856 // 行末の/を取り除く 1857 $dir = ereg_replace("/$", "", $dir); 1858 $path = $dir."/".$file; 1859 if ($file != '..' && $file != '.' && is_dir($path)) { 1860 return true; 1861 } 1862 } 1863 } 1864 } 1865 1866 return false; 1867 } 1868 1869 /* 1870 * 関数名:lfIsFileOpen() 1871 * 説明 :指定したファイルが前回開かれた状態にあったかチェック 1872 * 引数1 :ディレクトリ 1873 * 引数2 :現在のツリーの状態開いているフォルダのパスが | 区切りで格納 1874 */ 1875 function lfIsFileOpen($dir, $tree_status) { 1876 $arrTreeStatus = split('\|', $tree_status); 1877 if(in_array($dir, $arrTreeStatus)) { 1878 return true; 1879 } 1880 1881 return false; 1882 } 1883 1884 /* 1885 * 関数名:sfDownloadFile() 1886 * 引数1 :ファイルパス 1887 * 説明 :ファイルのダウンロード 1888 */ 1889 function sfDownloadFile($file) { 1890 // ファイルの場合はダウンロードさせる 1891 Header("Content-disposition: attachment; filename=".basename($file)); 1892 Header("Content-type: application/octet-stream; name=".basename($file)); 1893 Header("Cache-Control: "); 1894 Header("Pragma: "); 1895 echo (sfReadFile($file)); 1896 } 1897 1898 /* 1899 * 関数名:sfCreateFile() 1900 * 引数1 :ファイルパス 1901 * 引数2 :パーミッション 1902 * 説明 :ファイル作成 1903 */ 1904 function sfCreateFile($file, $mode = "") { 1905 // 行末の/を取り除く 1906 if($mode != "") { 1907 $ret = @mkdir($file, $mode); 1908 } else { 1909 $ret = @mkdir($file); 1910 } 1911 1912 return $ret; 1913 } 1914 1915 /* 1916 * 関数名:sfReadFile() 1917 * 引数1 :ファイルパス 1918 * 説明 :ファイル読込 1919 */ 1920 function sfReadFile($filename) { 1921 $str = ""; 1922 // バイナリモードでオープン 1923 $fp = @fopen($filename, "rb" ); 1924 //ファイル内容を全て変数に読み込む 1925 if($fp) { 1926 $str = @fread($fp, filesize($filename)+1); 1927 } 1928 @fclose($fp); 1929 1930 return $str; 1931 } 1932 1932 1933 1933 /**
Note: See TracChangeset
for help on using the changeset viewer.