We must use the #_FILE variable when uploading files in PHP. This automatic global variable $_FILES is supported starting from PHP 4.1.0 version. Prior to this, starting with version 4.0.0, PHP supported the $HTTP_POST_FILES array. These arrays will contain all the information about the files you uploaded, of which we recommend using $_FILES.
If the PHP setting option register_globals is on, the related variable names will also exist. Starting from PHP version 4.2.0, the default value of register_globals is set to off.
We assume the name of the file upload field is userfile. The name can be whatever you want.
$_FILES['userfile']['name']
The original name of the client machine file.
$_FILES['userfile']['type']
The MIME type of the file, which requires browser support for this information, such as "image/gif".
$_FILES['userfile']['size']
The size of the uploaded file, in bytes.
$_FILES['userfile']['tmp_name']
The temporary file name stored on the server after the file is uploaded.
$_FILES['userfile']['error']
Error code related to the file upload. ['error'] was added in PHP 4.2.0.
Handling function:
move_uploaded_file()
(PHP 4 >= 4.0.3, PHP 5)
move_uploaded_file -- Move the uploaded file to a new location
Description
bool move_uploaded_file (string filename, string destination)
This function checks and ensures that the file specified by filename is a legal upload file (that is, uploaded through PHP's HTTP POST upload mechanism). If the file is legal, it is moved to the file specified by destination.
If filename is not a valid uploaded file, no operation will occur and move_uploaded_file() will return FALSE.
If filename is a valid uploaded file but cannot be moved for some reason, no action will occur and move_uploaded_file() will return FALSE. A warning is also issued.
If the target file already exists, it will be overwritten.
Example:
The code is as follows | Copy code | ||||
if(move_uploaded_file($_FILES["magfile" ]["tmp_name"], $uploaddir))
echo "Update OK!"; |
bool copy (string source, string dest)
Copy files from source to dest. Returns TRUE on success, FALSE on failure.
代码如下 | 复制代码 |
The code is as follows | Copy code |
The code is as follows | Copy code |
if($_FILES['picurl']['size'] > 0){ If(move_uploaded_file ($_FILES['picurl']['tmp_name'], $_FILES['picurl']['name'])){ echo "Picture uploaded successfully"; } } |
Other non-File type forms can still be received using $_POST['name'].
Easy implementation of uploading images in php
代码如下 | 复制代码 |
if($_GET['action'] == 'upfile') { $target_path = 'temp_'.$_FILES['photo']['name']; echo '上传的临时文件:' .$_FILES['photo']['tmp_name'] . ' '; echo '上传的目标文件:' .$target_path . ' '; echo $_SERVER["SCRIPT_FILENAME"] . ' '; echo $_SERVER["OS"] . ' '; //测试函数: move_uploaded_file //也可以用函数:copy move_uploaded_file($_FILES['photo']['tmp_name'], $target_path); echo "Upload result:"; if(file_exists($target_path)) { if($_SERVER["OS"]!="Windows_NT"){ @chmod($target_path,0604); } echo 'Succeed! '; } else { echo 'Failed!'; } exit; } ?> Registration |
The above code is only suitable for learning. If you want to use it on the current server, we must write it as follows
The code is as follows | Copy code |
/***************************************************** ****************************** Parameter description: Instructions for use: //Upload file type list $max_file_size=2000000; //Upload file size limit, unit BYTE
if ($_SERVER['REQUEST_METHOD'] == 'POST') $file = $_FILES["upfile"]; if(!in_array($file["type"], $uptypes)) if(!file_exists($destination_folder)) $filename=$file["tmp_name"]; if(!move_uploaded_file ($filename, $destination)) $pinfo=pathinfo($destination); if($watermark==1) imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]); switch($watertype) switch ($iinfo[2]) // 更多原格式文件 if($imgpreview==1) |