AP102 2016/5/25 下午

AP102 2016/5/25 下午 php擷取抓取檔案名稱
php擷取抓取檔案路徑

    <?php 
        echo "<br>basename:";
        echo basename("file://Users/financialbrain/Downloads/學生帳號密碼.pdf");

        echo "<br>basename:";
        echo dirname("file://Users/financialbrain/Downloads/學生帳號密碼.pdf");

    ?>
//結果
basename:學生帳號密碼.pdf
basename:file://Users/financialbrain/Downloads
php列出所有檔案目錄下的資料
<?php
        foreach (glob("*") as $key => $value) {
            echo $value."<br>";
        }
?>

//結果會抓取與這php檔案當前資料夾相同的檔案
//以下這些檔案都是在跟.php檔案同一個資料夾
//假設使用*.png就會抓到所有png檔案
2.php
2016-05-23.txt
2016-05-24.txt
21.html
21.php
3.php
333.php
count.txt
file.php
form.html
php做一個檔案管理系統
<?php
    session_start();
?>
<!doctype html>
<html>
<head>
    <META charset="utf-8">
    <title></title>
    <style>



    </style>
</head>
<body>
    <h2>檔案管理系統</h2>
    <ul>
    <?php
        foreach (glob("*") as $key => $value) {
            # code...
            echo "<li><span>".$value."</span><a href=''>[Delete]</a></li>";
        }
    ?>
    </ul>


</body>
</html>
樣式設計一下
<style>

        li span{
            display: inline-block;
            width:250px;
        }
        li hover{
            background: #aaffff;
        }

</style>
action=delete
action和delete都是自己命名的
然後加上
if($_GET["action"]=="delete"){
        unlink($_GET["file"]);
    }
完成整個排版
<?php
    session_start();
    if($_GET["action"]=="delete"){
        unlink($_GET["file"]);
    }
?>
<!doctype html>
<html>
<head>
    <META charset="utf-8">
    <title></title>
    <style>

        li span{
            display: inline-block;
            width:250px;
        }

        li {
            padding: 2px 0;
        }
        li:hover{
            background: #aaffff;
        }

    </style>
</head>
<body>
    <h2>檔案管理系統</h2>
    <ul>
    <?php
        foreach (glob("*") as $key => $value) {
            # code...
            echo "
            <li>
                <span>".$value."</span>
                <a href='?action=delete'>[Delete]</a>
            </li>";
        }
    ?>
    </ul>


</body>
</html>


<?php
    session_start();

    if($_GET["action"]=="delete"){
        if(is_dir($_GET["file"])==true){
            rmdir($_GET["file"]);
        }else{
            unlink($_GET["file"]);
        }

    }
?>
<!doctype html>
<html>
<head>
    <META charset="utf-8">
    <title></title>
    <style>

        li span{
            display: inline-block;
            width:250px;
        }

        li {
            padding: 2px 0;
        }
        li:hover{
            background: #aaffff;
        }

    </style>
</head>
<body>
    <h2>檔案管理系統</h2>
    <form action="" method="post">
        目錄名稱:<input type="text" name="dirname">
        <input type="submit" value="建立">
    </form>
    <ul>
    <?php
        foreach (glob("*") as $key => $value) {
            # code...
            echo "
            <li>
                <span>".$value."</span>
                <a href='?action=delete'>[Delete]</a>
            </li>";
        }
    ?>
    </ul>


</body>
</html>


可以建立起目錄也可以刪除
<?php
    session_start();
    if($_GET["action"]=="create_dir"){
        mkdir($_POST["dirname"]);
    }
    if($_GET["action"]=="delete"){
        if(is_dir($_GET["file"])==true){
            rmdir($_GET["file"]);
        }else{
            unlink($_GET["file"]);
        }

    }

?>
<!doctype html>
<html>
<head>
    <META charset="utf-8">
    <title></title>
    <style>

        li span{
            display: inline-block;
            width:250px;
        }

        li {
            padding: 2px 0;
        }
        li:hover{
            background: #aaffff;
        }

    </style>
</head>
<body>
    <h2>檔案管理系統</h2>
    <form action="?action=create_dir" method="post">
        目錄名稱:<input type="text" name="dirname">
        <input type="submit" value="建立">
    </form>
    <ul>
    <?php
        foreach (glob("*") as $key => $value) {
            # code...
            echo "
            <li>
                <span>".$value."</span>
                <a href='?action=delete&file=".$value."''>[Delete]</a>
            </li>";
        }
    ?>
    </ul>


</body>
</html>

可以針對檔案夾進行瀏覽
<?php
    session_start();
    if($_GET["action"]=="create_dir"){
        mkdir($_POST["dirname"]);
    }
    if($_GET["action"]=="delete"){
        if(is_dir($_GET["file"])==true){
            rmdir($_GET["file"]);
        }else{
            unlink($_GET["file"]);
        }

    }

?>
<!doctype html>
<html>
<head>
    <META charset="utf-8">
    <title></title>
    <style>

        li span{
            display: inline-block;
            width:250px;
        }

        li {
            padding: 2px 0;
        }
        li:hover{
            background: #aaffff;
        }

    </style>
</head>
<body>
    <h2>檔案管理系統</h2>
    <form action="?action=create_dir" method="post">
        目錄名稱:<input type="text" name="dirname">
        <input type="submit" value="建立">
    </form>
    <ul>
    <?php
        foreach (glob("/*") as $key => $value) {

            echo "
            <li>
                <span>$value</span>
            ";
            if(is_dir($value)==true){
                echo "<a href=''>[瀏覽]<a>";
            }
            echo "
                <a href='?action=delete&file=".$value."'>[Delete]</a>  
            </li>
            ";
        }
    ?>
    </ul>
</body>
</html>

點擊瀏覽可以持續進入
<?php
        foreach (glob($_GET["dir"]."/*") as $key => $value) {

            echo "
            <li>
                <span>".basename($value)."</span>
            ";
            if(is_dir($value)==true){
                echo "<a href='?dir=$value'>[瀏覽]<a>";
            }
            echo "
                <a href='?action=delete&file=".$value."'>[Delete]</a>  
            </li>
            ";
        }
?>
無法上一頁
只能一直往下
而上一頁的功能就是只取出路徑
echo "<a href='?dir=".dirname($_GET["dir"])."'>上一頁</a>";
關鍵在於取出路徑名稱
.dirname($_GET["dir"]).

json格式
<?php
    session_start();
?>
<!doctype html>
<html>
<head>
    <META charset="utf-8">
    <title></title>

</head>
<body>
    <?php
        $xx=array(
            "A"=>"1",
            "B"=>"2",
            "C"=>array(1,2,3,4,5)
        );
        echo $xx;
        echo "<br>";
        echo json_encode($xx);
        echo "<br>";
        $nx =json_decode('{"A":"1","B":"2","C":[1,2,3,4,5]}');
        print_r($nx);


    ?>
</body>
</html>

    $nx =json_decode('{"A":"1","B":"2","C":[1,2,3,4,5]}',true);//強迫用陣列解讀


urlencode
<a href="?action=?aa=10&bb=20">Link</a>
    <a href="?action=<?php echo urlencode("?aa=10&bb=20");?>")>Link使用(urlencode)</a>
<?php
        print_r($_GET);
?>

urldecode
<a href="?action=?aa=10&bb=20">Link</a>
    <a href="?action=<?php echo urlencode("?aa=10&bb=20");?>")>Link使用(urlencode)</a>
    <?php
        print_r($_GET);
        echo"<br>";
        echo urldecode("%3Faa%3D10%26bb%3D20");
        echo "<br>";
?>

php加密
<?php
        echo md5("123");

?>
標頭檔
<?php
    header("content-type:image/jpeg");

    /*
        image/jpeg jpg
        image/png  png
        video/mpeg mp4

    */
?>
藏在格式內執行程式碼

<?php
    header("content-type:image/jpeg");
    header("content-disposition:attachment; filename=999.jpg");

    echo file_get_contents("1.jpg");

    $count=file_get_contents("count.txt");

    $count++;

    file_put_contents("count.txt", $count);

?>

Comments

Popular posts from this blog

MEGA 暫存檔案刪除

XAMPP 使用多PORT來執行不同網頁

IOS GCD多執行緒