move_uploaded_file()function 是 PHP 中的内置函数,用于更改文件目标。该函数仅在通过 PHP POST 函数上传文件时起作用。如果文件有效,则会上传。
用法:
move_uploaded_file( string $from, string $to ): bool
Parameters: 该函数接受两个参数,如下所述。
- $from: 该参数指定文件的临时位置。这是临时存储您上传的文件的位置。
- $to: 此参数指定文件存储位置的目标。
返回值:如果函数成功更改文件位置,则此函数返回 true,否则此函数将返回 false。
程序1:下面的程序演示了move_uploaded_file()函数。
PHP
/* upload.php */
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (
isset($_FILES["file_upload"]) &&
$_FILES["file_upload"]["error"] == UPLOAD_ERR_OK
) {
// Add name of upload directory
$uploadDirectory = "./uploads/";
if (move_uploaded_file(
$_FILES["file_upload"]["tmp_name"],
$uploadDirectory . $_FILES["file_upload"]["name"]
)) {
echo "File uploaded successfully!";
} else {
echo "Error moving file.";
}
} else {
echo "Error uploading file.";
}
}
?>
HTML
<!-- index.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>Simple File Upload</title>
</head>
<body>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file_upload">
Choose a file:
</label>
<input type="file" name="file_upload"
id="file_upload">
<input type="submit" value="Upload File">
</form>
</body>
</html>
输出:
程序2:下面的程序演示了move_uploaded_file()函数。在此示例中,我们仅上传特定扩展。
PHP
/* upload.php */
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (
isset($_FILES["file_upload"]) &&
$_FILES["file_upload"]["error"] == UPLOAD_ERR_OK
) {
$uploadDirectory = "uploads/";
$originalFileName =
basename($_FILES["file_upload"]["name"]);
$fileExtension = pathinfo(
$originalFileName, PATHINFO_EXTENSION);
$allowedExtensions = ["jpg", "jpeg", "png"];
if (in_array($fileExtension, $allowedExtensions)) {
if (
move_uploaded_file(
$_FILES["file_upload"]["tmp_name"],
$uploadDirectory . $originalFileName
)
) {
echo "File uploaded successfully!";
} else {
echo "Error moving file.";
}
} else {
echo "Error: Only JPG, JPEG, and "
. "PNG files are allowed.";
}
} else {
echo "Error uploading file.";
}
}
?>
HTML
<!-- index.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>File Upload Example</title>
</head>
<body>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file_upload">
Choose a file (only JPG, JPEG, and PNG):
</label>
<input type="file" name="file_upload"
id="file_upload" accept=".jpg, .jpeg, .png">
<input type="submit" value="Upload File">
</form>
</body>
</html>
输出:
参考: https://www.php.net/manual/en/function.move-uploaded-file.php
相关用法
- PHP move_uploaded_file()用法及代码示例
- PHP money_format()用法及代码示例
- PHP math acos()用法及代码示例
- PHP mt_getrandmax()用法及代码示例
- PHP method_exists()用法及代码示例
- PHP mysqli_affected_rows()用法及代码示例
- PHP mysqli_autocommit()用法及代码示例
- PHP mysqli_begin_transaction()用法及代码示例
- PHP mysqli_change_user()用法及代码示例
- PHP mysqli_character_set_name()用法及代码示例
- PHP mysqli_commit()用法及代码示例
- PHP mysqli_connect_errno()用法及代码示例
- PHP mysqli_connect_error()用法及代码示例
- PHP mysqli_debug()用法及代码示例
- PHP mysqli_dump_debug_info()用法及代码示例
- PHP mysqli_error_list()用法及代码示例
- PHP mysqli_field_count()用法及代码示例
- PHP mysqli_get_charset()用法及代码示例
- PHP mysqli_get_client_info()用法及代码示例
- PHP mysqli_get_client_stats()用法及代码示例
- PHP mysqli_get_client_version()用法及代码示例
- PHP mysqli_get_connection_stats()用法及代码示例
- PHP mysqli_get_host_info()用法及代码示例
- PHP mysqli_get_proto_info()用法及代码示例
- PHP mysqli_get_server_info()用法及代码示例
注:本文由纯净天空筛选整理自neeraj3304大神的英文原创作品 PHP move_uploaded_file() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。