Programming file programming the necessity of file programming ① In website development, we need to use the upload and download of files, that is, the use of classical file programming. the basic concept of a file is to save data (binary data, text data). From a programmer's perspective, a file is a data source. during file stream operations, we use
Programming file programming the necessity of file programming ① In website development, we need to use the upload and download of files, that is, the use of classical file programming. the basic concept of a file is to save data (binary data, text data). From a programmer's perspective, a file is a data source. during file stream operations, we use
File Programming
File Programming
Necessity of file Programming
① In website development, we need to upload and download files, which is a classic file Programming Application.
Basic concepts of Files
Files are data storage (binary data, text data). From the programmer's perspective, files are data sources.
File stream
In the process of file operations, we operate on the concept of stream.
From this figure, we can see that if your data flows from the Program (memory) to the file (Disk), it is called the output stream. Otherwise, it is called the input stream.
Basic usage of php File Operations
① How to obtain the file information.
1.1 fopen open a file
Basic usage
Fopen (file path [relative path/absolute path], open mode );
Mode:
Code:
// 1 file information
// Open the file
$ File_path = "test.txt ";
// This function returns a pointer to the object
If ($ fp = fopen ($ file_path, "r ")){
$ File_info = fstat ($ fp );
Echo"
"; print_r($file_info);
echo "
";
// Check out
Echo"
The file size is {$ file_info ['SIZE']} ";
Echo"
Last file modification time ". date (" Y-m-dH: I: s) ", $ file_info ['mtime']);
Echo"
Last file access time ". date (" Y-m-dH: I: s) ", $ file_info ['atime ']);
Echo"
Last file change time ". date (" Y-m-dH: I: s) ", $ file_info ['ctime']);
} Else {
Echo "failed to open the file ";
}
// Close the file !!! Important
Fclose ($ fp );
// *** Method 2: obtain file information
Echo"
". Filesize ($ file_path );
Echo"
". Date (" Y-m-d H: I: s ", fileatime ($ file_path ));
Echo"
". Filectime ($ file_path );
Echo"
". Filemtime ($ file_path );
?>
Fstat Function
This function returns several messages.
[Atime] => 1316831240 [last time this file was accessed]
[Mtime] => 1316831237 [last time the file content was modified]
[Ctime] => 1316831228 [last modification to the file owner/File Group]
② How to read files
// Read the file
// 1. File
// Open the file
$ File_path = "test.txt ";
// This function returns a pointer to the object
// First determine whether the file exists
// ************************ First method of reading ********** **********
/* If (file_exists ($ file_path )){
// Open the file
$ Fp = fopen ($ file_path, "a + ");
// Read the content and enter
// ***** First method of reading **************
$ Con = fread ($ fp, filesize ($ file_path ));
Echo "file content is:
";
// By default, after the content is output to the webpage, it does not wrap because the webpage
// Do not recognize \ r \ n as a line break, \ r \ n->
$ Con = str_replace ("\ r \ n ","
", $ Con );
Echo $ con;
} Else {
Echo "the file does not exist! ";
}
Fclose ($ fp );*/
// ************* Method 2: A function ****************
/* $ Con = file_get_contents ($ file_path );
// Replace
$ Con = str_replace ("\ r \ n ","
", $ Con );
Echo $ con ;*/
// ************* The third method for reading objects cyclically )*********
$ Fp = fopen ($ file_path, "a + ");
// We set to read 1024 bytes at a time
$ Buffer = 1024;
$ Str = "";
// Read at the same time to determine whether the object has reached the end
While (! Feof ($ fp )){
// Read
$ Str = fread ($ fp, $ buffer );
}
$ Str = str_replace ("\ r \ n ","
", $ Str );
Echo $ str;
Fclose ($ fp );
?>
Actual usage of File Reading
When we connect to the database, we can configure the user name, password, and host to an external file, and then get it in real time when php is running.
Db. ini
Host = 192.168.1.23
User = admin
Password = 12345
Dbname = test
$ Arr1 = parse_ini_file ("db. ini ");
Print_r ($ arr1 );
?>
③ How to write files
// How to write files
// 1. Traditional Method
/* $ File_path = "test.txt ";
If (file_exists ($ file_path )){
// If append content is used, use a + append
// If it is completely new to the file, use w + write
$ Fp = fopen ($ file_path, "w + ");
$ Con = "\ r \ n hello! ";
For ($ I = 0; $ I <10; $ I ++ ){
Fwrite ($ fp, $ con );
}
} Else {
}
Echo "add OK ";
Fclose ($ fp );*/
// 2. Method 2: write files
$ File_path = "test.txt ";
$ Con = "Hello Beijing! \ R \ n ";
For ($ I = 0; $ I <10; $ I ++ ){
$ Con. = "Hello Beijing! \ R \ n ";
}
File_put_contents ($ file_path, $ con, FILE_APPEND );
Echo "OK ";
?>
④ Copy an object (image)
// Copy the image
$ File_path = iconv ("UTF-8", "gb2312", "C: \ Documentsand Settings \ All Users \ Documents \ My Pictures \ sample image \ Winter.jpg ");
If (! Copy ($ file_path, "d: \ bb.jpg ")){
Echo "error ";
} Else {
Echo "OK ";
}
?>
⑤ File Download task...
See our http course.
Supplement
⑥ File creation, deletion/Folder creation and deletion.
// Create and delete files and folders.
// 1. Create a folder d:/shunping
/* If (! Is_dir ("d:/shunping2 ")){
If (mkdir ("d:/shunping2 ")){
Echo "create folder OK ";
} Else {
Echo "create folder err ";
}
} Else {
Echo "this folder has ";
}*/
// 2. Can I create multiple files (levels) at a time and create OK?
/* $ Path = "d:/shunping3/aaa/bbb/cccc/ddd ";
If (! Is_dir ($ path )){
If (mkdir ($ path, 0777, true )){
Echo "create folder OK ";
} Else {
Echo "create folder err ";
}
} Else {
Echo "this folder has ";
}*/
// 3. delete a folder
// If the folder contains files or directories, OK cannot be deleted.
/* If (rmdir ("d:/shunping2 ")){
Echo "delete folder OK ";
} Else {
Echo "err ";
}*/
// 4. Create a file
// In the d:/shunping3 directory, create a file and write it to hello
/* $ File_path = "d:/shunping3/aa.txt ";
$ Fp = fopen ($ file_path, "w + ");
Fwrite ($ fp, "hello, world ");
Fclose ($ fp );
Echo "Create File OK ";*/
// 5. delete an object
$ File_path = "d:/shunping3/aa.txt ";
If (is_file ($ file_path )){
If (unlink ($ file_path )){
Echo "delete OK ";
} Else {
Echo "delete error ";
}
} Else {
Echo "the file does not exist ";
}
?>
PHP file programming-File Upload
Code:
Upload. php: This is an interface for uploading files.
To upload a file, we need
Code:
Details of file upload:
(1) how to control the size of the File Uploaded by the user <2 m
(2) how to control the file types uploaded by users
(3) how to prevent image coverage problems
(4) how to prevent identical file names uploaded by the same user
Upload. php
Uploadprocess. php
// 1. the user who receives the submitted file
$ Username = $ _ POST ['username'];
$ Fileintro =$ _ POST ['fileintro'];
// Echo $ username. $ fileintro;
// Here we need to use $ _ FILE
/* Echo"
"; print_r($_FILES);
echo "
";*/
// Obtain the file size
$ File_size = $ _ FILES ['myfile'] ['SIZE'];
If ($ file_size> 2*1024*1024 ){
Echo "the file is too large to upload files larger than 2 MB ";
Exit ();
}
// Obtain the object type
$ File_type = $ _ FILES ['myfile'] ['type'];
If ($ file_type! = 'Image/jpg '& $ file_type! = 'Image/pjpeg '){
Echo "the file type can only be jpg ";
Exit ();
}
// Determine whether to upload OK
If (is_uploaded_file ($ _ FILES ['myfile'] ['tmp _ name']) {
// Transfer the file to the desired directory
$ Uploaded_file = $ _ FILES ['myfile'] ['tmp _ name'];
// We dynamically create a folder for each user
$ User_path = $ _ SERVER ['document _ root']. "/file/up/". $ username;
// $ User_path = iconv ("UTF-8", "gb2312", $ user_path );
// Determine whether the user has a folder
If (! File_exists ($ user_path )){
Mkdir ($ user_path );
}
// $ Move_to_file = $ user_path. "/". $ _ FILES ['myfile'] ['name'];
$ File_true_name = $ _ FILES ['myfile'] ['name'];
$ Move_to_file = $ user_path. "/". time (). rand (1,1000). substr ($ file_true_name, strrpos ($ file_true_name ,"."));
If (move_uploaded_file ($ uploaded_file, iconv ("UTF-8", "gb2312", $ move_to_file ))){
Echo $ _ FILES ['myfile'] ['name']. "Upload OK ";
} Else {
Echo "Upload Failed ";
}
} Else {
Echo "Upload Failed ";
}
?>
Php Drawing Technology
Php coordinate system:
Basic Steps for php plotting
① Create a canvas
② Draw various figures
③ Output graphics (1. output to page 2. output to a file to create a new image)
④ Destroy graphics (release memory resources .)
? If you want to use php to draw images, You need to enable the gd2 Library
In the php. ini file
Extension = php_gd2.dll
? Our website is developed and commonly used image formats (jpg/jpeg, gif, png bmp WBMP, XBM)
Jpg/jpeg images are used in many websites, with a high compression rate. images are clear. images are relatively small.
Gif: supports animation. It supports 256 colors, because a large number of gif files flash.
Png: supports lossless compression and high fidelity. Large files
Basic usage of php plotting