Содержание
- 2. Files and PHP File Handling Data Storage Though slower than a database Manipulating uploaded files From
- 3. Open/Close a File A file is opened with fopen() as a “stream”, and PHP returns a
- 4. File Open Modes
- 5. File Open/Close Example // open file to read $toread = fopen(‘some/file.ext’,’r’); // open (possibly new) file
- 6. Now what..? If you open a file to read, you can use more in-built PHP functions
- 7. Reading Data There are two main functions to read data: fgets($handle,$bytes) Reads up to $bytes of
- 8. Reading Data We need to be aware of the End Of File (EOF) point.. feof($handle) Whether
- 9. Data Reading Example $handle = fopen('people.txt', 'r'); while (!feof($handle)) { echo fgets($handle, 1024); echo ' ';
- 10. Data Reading Example $handle = fopen('people.txt', 'r'); while (!feof($handle)) { echo fgets($handle, 1024); echo ' ';
- 11. Data Reading Example $handle = fopen('people.txt', 'r'); while (!feof($handle)) { echo fgets($handle, 1024); echo ' ';
- 12. Data Reading Example $handle = fopen('people.txt', 'r'); while (!feof($handle)) { echo fgets($handle, 1024); echo ' ';
- 13. File Open shortcuts.. There are two ‘shortcut’ functions that don’t require a file to be opened:
- 14. Writing Data To write data to a file use: fwrite($handle,$data) Write $data to the file.
- 15. Data Writing Example $handle = fopen('people.txt', 'a'); fwrite($handle, “\nFred:Male”); fclose($handle);
- 16. Data Writing Example $handle = fopen('people.txt', 'a'); fwrite($handle, '\nFred:Male'); fclose($handle); $handle = fopen('people.txt', 'a'); Open file
- 17. Other File Operations Delete file unlink('filename'); Rename (file or directory) rename('old name', 'new name'); Copy file
- 18. Dealing With Directories Open a directory $handle = opendir('dirname'); $handle 'points' to the directory Read contents
- 19. Directory Example $handle = opendir('./'); while(false !== ($file=readdir($handle))) { echo "$file "; } closedir($handle);
- 20. Directory Example $handle = opendir('./'); while(false !== ($file=readdir($handle))) { echo "$file "; } closedir($handle); Open current
- 21. Directory Example $handle = opendir('./'); while(false !== ($file=readdir($handle))) { echo "$file "; } closedir($handle); Whilst readdir()
- 22. Directory Example $handle = opendir('./'); while(false !== ($file=readdir($handle))) { echo "$file "; } closedir($handle); Close the
- 23. Other Directory Operations Get current directory getcwd() Change Directory chdir('dirname'); Create directory mkdir('dirname'); Delete directory (MUST
- 25. Скачать презентацию