accessing remote Files
?? If you need to access a remote file, you must activate the "allow_url_fopen" option in your PHP configuration file to open the process file using the fopen () function. And also to determine whether the files in other servers access permissions, if you use the PHP protocol to link remote files, can only be opened in "read-only" mode. If you need to access a remote FTP server that has the writable permission turned on for the provided user, you can use write-only or read-only mode to open the file when you use the FTP protocol to link the remote file. However, you cannot use the "readable writable" mode.
?? Accessing remote files using PHP is like accessing a local file, using the same read-write function.
?? $file =fopen ("http://www.ss.com/", "R") or Die ("Open remote file failed!! ”);
?? $file =fopen ("Ftp://user:[email protected]/path/to/file", "w");
Move file pointer
?? ftell--returns the position of the file pointer read/write
?? Syntax: Intftell (Resource handle)
?? Returns the position of the file pointer specified by handle, which is the offset in the file stream. Returns False if there is an error. The file pointer must be valid and must point to a file that was successfully opened by fopen () or Popen ().
?? fseek--positioning in the file pointer
?? Syntax: Intfseek (resource handle, Intoffset [, Intwhence])
?? Sets the location of the file pointer in the file associated with the handle. The new location, measured in bytes from the beginning of the file header, is the position specified by whence plus offset. The value of the whence is defined as:
?? Seek_set-the set position equals the offset byte.
?? Seek_cur-Sets the position to the current position plus offset.
?? Seek_end-Set the position to the end of the file plus offset. (to move to the position before the end of the file, you need to pass a negative value to offset.) )
?? If whence is not specified, the default is Seek_set.
?? Success returns 0; otherwise returns-1. Note the position after the EOF is not counted as an error
rewind--Rewind the position of the file pointer
?? Syntax: Boolrewind (Resource handle)
?? Sets the handle file location pointer to the beginning of the file stream. Returns true if successful, and false if it fails. The file pointer must be valid and point to a file that was successfully opened by fopen ().
Locking mechanism for files
?? flock--Lightweight consultation document lock-up
?? Syntax: Boolflock (Inthandle, intoperation [, Int&wouldblock])
?? PHP supports a lightweight method of locking all files in a consultative manner (that is, all access programs must be locked in the same way, otherwise it will not work).
?? Handle must be a pointer to a file that has already been opened.
?? Operation can be one of the following values:
?? To get a shared lock (read program), set operation to Lock_sh.
?? To obtain an exclusive lock (write program), set operation to LOCK_EX.
?? To release the lock (either share or exclusive), set operation to Lock_un.
?? If you don't want flock () to clog when locked, add LOCK_NB to operation.
?? Returns true if successful, and false if it fails.
Some basic operation functions of the file
?? copy--Copy Files
?? Syntax: boolcopy (string source, String dest)
?? Copy the file from source to Dest. Returns true if successful, and false if it fails.
?? unlink--Deleting files
?? Syntax: Boolunlink (string filename)
?? Delete filename. Similar to the unlink () function of Unix C. Returns true if successful, and false if it fails.
?? ftruncate--truncates a file to a given length
?? Syntax: Boolftruncate (resource handle, intsize)
?? Accepts the file pointer handle as a parameter and intercepts the file size to size. Returns true if successful, and false if it fails.
?? rename--renaming a file or directory
?? Syntax: Boolrename (String oldname, String newname[, resource context])
?? Try renaming the oldname to NewName. Returns true if successful, and false if it fails.
uploading and downloading of filesFile Upload
?? In b/S program file upload has become a common function. The purpose is that customers can upload files to the specified directory on the server (servers) via the browser (Browser).
?? Basic knowledge of file upload in PHP
?? Form
?? Action on uploading a file
The HTML specification specifies that the form header must be used when uploading a file"todo.php"Method="Post"enctype="Multipart/form-data"><input type="Hidden"Name="max_file_size"value="100000">Upload file:<input type="file"Name="UserFile">Submit:<input type="Submit"Value="Submit a query"></form></body>Note Several feature attributes:
?? Post method:
The most common function of the form, passing variables to the target page, when we upload the file, we will set the corresponding properties in the form to complete the file delivery
?? Enctype= "Multipart/form-data"
The server will know that we are going to pass a file so that the server can know that the uploaded file has regular form information.
?? Max_file_size
This field must control the size (in bytes) of the largest delivery file before the File entry field--Can you really control it?
?? <input type= "File" name= "UserFile" >
Set browser file input browse button
php.ini File upload parameter settingsWe set the data passed to the form in the server-side php.ini to further judge
?? file_uploads= on/off Allow file upload
?? upload_max_filesize= 2M maximum size of uploaded files
?? post_max_size= maximum size allowed for 8MPOST data
?? Upload_tmp_dir the temporary directory where the uploaded files are placed
?? The data that the form passes, the file is only a part of it, so when set, upload_max_filesize should be less than post_max_size
Super Global Array $_filesIn a PHP program, the uploaded data that needs to be processed is saved in the global array $_files (Super Global Array)
?? Saves the elements in the $_files array, storing the name of the type= "file" tag of the HTML form name= "UserFile" in the array.
1: The value stored in $_files[' userfile ' [' Name '] is:
?? The name of the file for the client file system
2: The value stored in $_files[' userfile ' [' type '] is:
?? Types of files that are passed by the client
3: Values stored in $_files[' userfile ' [' Size '] are:
?? The size of the file's bytes
4: Value stored in $_files[' userfile ' [' Tmp_name ']
?? Temporary full path stored on the server after the file is uploaded
5: The value stored in $_files[' userfile ' [' Error '] is:
?? File upload error code-php4.2 features added later
Values stored in $_files[' userfile ' [' Error ']The error code returned in $_files[' userfile ' [' Error '] was introduced in the PHP4.2.0 version. Specific as follows:
?? A value of 0: Indicates that no errors have occurred.
?? A value of 1: Indicates that the size of the uploaded file exceeds the agreed value. The maximum file size is specified in the PHP configuration file, which is: Upload_max_filesize.
?? A value of 2: Indicates that the upload file size exceeds the maximum value specified by the Max_file_size element of the HTML form's hidden field property.
?? A value of 3: Indicates that the file is only partially uploaded.
?? A value of 4: Indicates that no files were uploaded.
Constants corresponding to error values?? UPLOAD_ERR_OK: Corresponding value 0
?? Upload_err_ini_size: Corresponding value 1
?? Upload_err_form_size: Corresponding value 2
?? Upload_err_partial: Corresponding value 3
?? Upload_err_no_file: Corresponding value 4
Data Format (MIME)Temporary storage directory after file uploadUploaded files are placed on the server-side temp directory:/tmp directory
Named as a unique, randomly generated temporary file name.
Note: The file will be deleted automatically after the program is finished executing. Before you delete the
To operate like a local file.
?? /tmp directory is the default upload temporary file storage location,
If you need to change this directory:
You can edit the/etc/php.ini file uploads segment
Upload_tmp_dir Property value
Post-upload file processingUse the Is_uploaded_file () function to check if this file is an upload file.
?? You should use the Move_uploaded_file (temporary path/temporary file name, destination path/destination file name) function to copy the uploaded file stored in the temporary directory to the specified file name in the specified directory, if the target exists, it will be overwritten.
?? When the Register_globals property of the configuration file php.ini is set to ON
?? <input Type=file name=myfilename>
?? Global variables will be generated: $myfilename, etc.
Processing page after file upload
PHPif($_files['UserFile']['Error'] >0) {echo'Upload error:';Switch($_files['UserFile']['Error']){ Case 1: Echo ' upload file size exceeds the convention value in PHP config file: upload_max_filesize'; Break; Case 2: Echo'The upload file size exceeds the contract value in the form: Max_file_size'; Break; Case 3: Echo'files are only partially uploaded'; Break; Case 4: Echo'no files were uploaded'; Break;} Exit;}
if ($_files[' userfile ' [' type ']! = ' Text/plain ') {
Echo ' Problem: The file is not a text file. ‘;
Exit
}
$upfile = './uploads/'. $_files[' userfile ' [' name '];
if (Is_uploaded_file ($_files[' userfile ' [' tmp_name '])) {
Determine whether to upload files
if (!move_uploaded_file ($_files[' userfile ' [' tmp_name '], $upfile)) {
Moving files
Echo ' problem: The file cannot be moved to the specified directory. ‘;
Exit
}
}else {
Echo ' problem: Uploading a file is not a legitimate file: ';
echo $_files[' userfile ' [' name '];
Exit
}
echo ' File Upload succeeded! <br><br> ';
?>
Basic operation of the file two with file upload and download (45)