Basic introduction to php file upload

小云云
Release: 2023-03-20 17:12:01
Original
1683 people have browsed it

1. 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

  1. file_uploads=on|off

     Determine whether the PHP script on the server can accept file uploads.

  2. max_execution_time=integer
     Maximum time, in seconds, that a PHP script can execute before registering a fatal error.

  3. memory_limit=integer

     Set the maximum memory that the script can allocate, in MB. This prevents runaway scripts from monopolizing server memory.

  4. upload_max_filesize=integer
     Set the maximum size of uploaded files, in MB.

  5. 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.

  6. post_max_size=integer
     Determines the maximum size, in MB, of information that can be accepted via the POST method.

##2>$_FILES array

<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>
Copy after login
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);?>
Copy after login
$_FILES super global variable, which stores various upload-related information that is crucial for files uploaded to the server through PHP scripts.

1. The value stored in the
_FILES["file"]["name"] variable is the file name in the user system.

3. The value stored in the _FILES["file"]["type"] variable is the MIME type of the file, for example: text/plain or image/gif.
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.

3>Upload function

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 '失败';
}
Copy after login
2. File Directory


Organize related data into entities such as files and directories. Programmers need to have a way to obtain important details about files and directories, such as their locations. , size, last modified time, last access time and other identifying information.

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 path

2>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';
Copy after login
  1. Disk free space size

    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';
Copy after login
  1. 磁盘的总容量
     disk_total_space()
     指定的目录所在磁盘分区的总容量。

$drive = 'C:';echo round(disk_total_space($drive)/1024/1024/1027,2).'GB';
Copy after login

相关推荐:

php文件上传简单实例分享

php文件上传类及PHP封装的多文件上传类分享

HTML5 Ajax文件上传进度条如何显示

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template