1 / 17

PHP III. Fájlok, űrlapok

PHP III. Fájlok, űrlapok. Fájlok vizsgálata. if (file_exists(”hello.txt ” )) echo ”File exists ” ; if (is_file(”hello.txt ” )) echo ”File! ” ; if (is_dir(”C:\Windows ” )) echo ”Directory! ” ; // ‘\’ és ‘/’ is megengedett! is_readable(), is_writeable(), is_executable()

Download Presentation

PHP III. Fájlok, űrlapok

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP III.Fájlok, űrlapok

  2. Fájlok vizsgálata • if (file_exists(”hello.txt”)) • echo ”File exists”; • if (is_file(”hello.txt”)) • echo ”File!”; • if (is_dir(”C:\\Windows”)) • echo ”Directory!”; // ‘\\’ és ‘/’ is megengedett! • is_readable(), is_writeable(), is_executable() • filesize(”hello.txt”);

  3. Fájlok olvasása • Megnyitás: $fp = fopen($path, $mode) • $mode: ”r”, ”w”, ”a”, ... (read/write/append) • Bezárás: fclose($fp) • Sor olvasása: $line = fgets($fp, 1024) • Fájl vége: feof($fp) • Olvasás: fread($fp, 16) • Pozícionálás: fseek($fp, 16) • Karakter: $ch = fgetc($fp) • Teljes tartalom: file(), file_get_contents()

  4. Feladatok $path = ”dummy.txt”; if (file_exists($path) && is_readable($path)) { $fp = fopen($path, ”r”); print ”Size: ” . filesize($path) . ” bytes <br/>”; print ”Content: <br/>”; while (!feof($fp)) { $line = fgets($fp, 1024); print htmlspecialchars($line) . ”<br/>”; } fclose($fp); }

  5. Feladatok $lines = file(”hello.txt”); foreach ($lines as $line) echo $line . ‘<br/>’; $content = file_get_contents(”hello.txt”); print $content; // nl2br($content);

  6. Fájlok írás • Szöveg írása: • fwrite($fp, ”Hello world!”); // vagy fputs() • fflush($fp) • Teljes tartalom: • file_put_contents($path, $content, $flag) • Zárolás: flock($fp, $operation) • LOCK_SH: olvashatják, de nem írhatják • LOCK_EX: nem olvashatják, nem írhatják • LOCK_UN: feloldás

  7. Könyvtárak • Létrehozás: mkdir(); • Törlés: rmdir(); • Nyitás: $d = opendir(”mydir”); • Olvasás: $e = readdir($d); • Vizsgálat: is_dir($e); • Bezárás: closedir($d);

  8. Feladat $dirname = ”.”; $dir = opendir($dirname); while ($f = readdir($dir)) echo $f . ’<br/>’; closedir($dir); Hiba: hozzunk létre egy „0” nevű fájlt! Ötlet: while (!is_bool($f = readdir($dir)))

  9. Fájlok, könyvtárak • basename(”/home/a.txt”); // ”a.txt” • dirname(”/home/a.txt”) // ”/home” • Másolás: copy($src, $dest); • Törlés: unlink(”/home/a.txt”) // delete() • Átnevezés: rename(”/home/a.txt”) • Temp fájl: $fp = tmpfile(); • Könyvtár váltás: chdir($dirname) • Aktuális könyvtár: getcwd()

  10. Űrlapok • Kliens oldal (browser): • HTML form method=”post” • Változók a $_POST tömbben • HTML form method=”get” • Változók a $_GET tömbben • index.php?id=15&page=34 • hosszabb szöveget: urlencode(), urldecode() • Szerver oldal (apache+php): • <input type=”text” name=”username” /> • $username = $_POST[’username’];

  11. Űrlapok • A $_SERVER tömb: • ’PHP_SELF’ - aktuális script • ’HTTP_USER_AGENT’ - böngésző • ’REMOTE_ADDR’ - IP cím • ’REQUESTED_METHOD’ - POST v. GET • ’QUERY_STRING’ - GET url változók • ’REQUEST_URI’ - teljes cím • ’HTTP_REFERER’ - a kérelmező oldal

  12. Űrlapok • HTML select • <select name=”people” multipe=”multiple”> • <option>Vladimir</option> • <option>Paul</option> • foreach($_POST[’people’] as $person) • echo $person . ’<br/>’; • Rejtett mezők • <input type=”hidden” name=”click” • Állapotok tárolhatók

  13. Feladatok <?php if (!isset($_POST[’user’]) || !isset($_POST[’pass’])) { ?> <form method=”post” action=”auth.php”> <p>User: <input type=”text” name=”user”></p> <p>Pass: <input type=”password” name=”pass”></p> <p><input type=”submit” value=”Submit”></p> </form> <?php } else { $name = $_POST[’user’]; $pass = $_POST[’pass’]; $acc = file(’access.txt’); if (!strcmp($name, trim($acc[0])) && !strcmp($pass, trim($acc[1]))) echo ’Üdvözöljük ’ . $name; else echo ’Hibás bejelentkezés!<br/>IP: ’ . $_SERVER[’REMOTE_ADDR’]; } ?>

  14. Fájl feltöltés • HTML • <form enctype=”multipart/form-data” ... • <input type=”file” name=”up” • PHP • $_FILES[’up’][’name’] // a.bmp • $_FILES[’up’][’tmp_name’] // /tmp/phpDfsdZ • $_FILES[’up’][’size’] // 6987 • $_FILES[’up’][’type’] // image/bmp • $_FILES[’up’][’error’] // hibakód

  15. Fájl feltöltés • Hiba kódok: • UPLOAD_ERR_OK 0 • UPLOAD_ERR_INI_SIZE 1 • UPLOAD_ERR_FORM_SIZE 2 • UPLOAD_ERR_PARTIAL 3 • UPLOAD_ERR_NO_FILE 4 • Max. méret megadása: • <input type=”hidden” name=”MAX_FILE_SIZE” value=”102400”

  16. Feladatok if (isset($_FILES[’up’])) { $validtypes = array(”image/jpeg”, ”image/gif”); $up = $_FILES[’up’]; if (($up[’error’] == 0) && in_array($up[’type’], $validtypes)) { $src = $up[’tmp_name’]; $name = $up[’name’]; $dest = ’upload/’ . $name; move_uploaded_file($src, $dest); echo ’Feltöltés sikerült: <br/>’; echo ”<a href=\”upload/” . $up[’name’] . ”\”>$name</a>”; } else { echo $up[’type’] . ’nem támogatott!’; } }

  17. Feladatok • Készítsünk könyvtár szerkezet bejáró szkriptet, amely megjeleníti a könyvtárban található fájlok és alkönyvtárak listáját, valamint lehetőséget biztosít alkönyvtárba belépésre, illetve visszalépésre. Fájlok esetén írjuk ki a méretét, könyvtár esetén a DIR szót. • Az aktuális könyvtár útvonalat tárolhatjuk pl. GET változóban.

More Related