Home > Article > Backend Development > Basic introduction to php file upload
Through PHP, files can be uploaded to the server. Data can be submitted to the server through form or post, but post cannot submit file type data information.
1>php file upload command configuration
##2>$_FILES array
file_uploads=on|off
Determine whether the PHP script on the server can accept file uploads.max_execution_time=integer
Maximum time, in seconds, that a PHP script can execute before registering a fatal error.memory_limit=integer
Set the maximum memory that the script can allocate, in MB. This prevents runaway scripts from monopolizing server memory.upload_max_filesize=integer
Set the maximum size of uploaded files, in MB.upload_tmp_dir=string
Set the uploaded file to be stored in a temporary location on the server before being processed until the file is moved to its final destination.post_max_size=integer
Determines the maximum size, in MB, of information that can be accepted via the POST method.
<form action="upload_file.php" method="post"enctype="multipart/form-data"> <label for="file">上传文件:</label> <input type="hidden" name="MAX_FILE_SIZE" value="1000"/> <input type="file" name="file" id="file" /> <img id="preview"> <br /> <input type="submit" name="submit" value="Submit" /> </form>
ENCTYPE=”multipart/form-data”:This is the fixed writing method, otherwise the file upload will fail.
ACTION=”upload.php”:
Define the program file path to handle uploading.
METHOD=”post”:
Define the transmission method as POST. Generally, Form submission data is set to POST.
<input type=”hidden” name=”MAX_FILE_SIZE” value=”1000000”>
This is a hidden field that defines the upper limit of the uploaded file size. When this value is exceeded, the upload fails. It must be defined in front of the file upload domain. And the value defined here cannot exceed the value set in the upload_max_filesize in the php.ini file, otherwise it will be meaningless. (Note: The value of MAX_FILE_SIZE is just a suggestion for the browser. In fact, it It can be easily bypassed. So don't rely on browser restrictions. In fact, the maximum value for uploaded files in PHP.ini settings will not be invalid, but it is best to add it in the form. MAX_FILE_SIZE because it saves users the trouble of spending time waiting for a large file to be uploaded only to discover that the file is too big)
. \
This is the file upload domain. The Type attribute must be set to file, but the Name attribute can be customized. This value will be used in the code file.<?php print_r($_FILES);?>
$_FILES super global variable, which stores various upload-related information that is crucial for files uploaded to the server through PHP scripts.3>Upload function1. The value stored in the
3. The value stored in the _FILES["file"]["type"] variable is the MIME type of the file, for example: text/plain or image/gif.
_FILES["file"]["name"] variable is the file name in the user system.
5. The value stored in the $_FILES["file"]["error"] variable will be any error code related to the file upload. This is a new feature added in PHP4.2.0.
error provides some array constants respectively:
+ 0: Indicates no error occurred.
+ 1: Indicates that the size of the uploaded file exceeds the agreed value. The maximum file size is specified in the PHP configuration file, the directive is upload_max_filesize.
+ 2: Indicates that the uploaded file size exceeds the maximum value specified by the MAX_FILE_SIZE element of the HTML form.
+ 3: Indicates that the file was only partially uploaded.
+ 4: Indicates that no files are uploaded.
PHP also provides two functions specifically for the file upload process: is_uploaded_file() and move_uploaded_file().
//确定是否上传文件if (is_uploaded_file($_FILES["file"]["tmp_name"])) { echo '已经上传到临时文件夹'; $filename = "upload".time()."png"; //移动上传文件(将文件移动到指定文件夹) if (!move_uploaded_file($_FILES["file"]["tmp_name"],img/,$filename)) { echo '移动失败'; exit; }else{ echo "移动成功"; } } else { echo '失败'; }2. File Directory
1>Directory operation
+ Get the current file path
1. __FILE__
Current file path + current file name
2. __DIR__
Current file path
3. dirname( __FILE__)
Current file path
4. basename(__FILE__)
Current file name
5. pathinfo(__FILE__)
Associative array of information about the path, including: directory name, base name and extension
6. realpath(__FILE__)
Absolute path (the absolute path can only be obtained if the file does exist in the current project, and only the corresponding file path information in the current file can be read)
__FILE__ and __DIR__ are for the current file, dinrname() and basename() are for any file path2>Disk, directory and file size calculation
1. File size
filesize($path)
Calculate the file size in bytes.
$file = __FILE__;echo round(filesize($file)/1024).'KB';
disk_free_space()
The available space of the disk partition where the specified directory is located.
$drive = 'C:';echo round(disk_free_space($drive)/1024/1024/1024,2).'GB';
磁盘的总容量
disk_total_space()
指定的目录所在磁盘分区的总容量。
$drive = 'C:';echo round(disk_total_space($drive)/1024/1024/1027,2).'GB';
相关推荐:
The above is the detailed content of Basic introduction to php file upload. For more information, please follow other related articles on the PHP Chinese website!