首页  >  文章  >  后端开发  >  用 PHP 上传文件

用 PHP 上传文件

WBOY
WBOY原创
2024-08-29 13:07:39844浏览

在PHP中,用户可以使用文件上传功能上传文件,并且必须通过表单提交的文件很容易附加和上传。用户可以上传多种类型的文件,可以是文档形式、图像形式、pdf 形式等。这些类型的文件带有扩展名,即 .docx、.jpeg、.pdf 等。这种文件由表单并设置文件大小,以便上传的大小不得超过该大小。对于过去手动输入数据的用户来说,这是一项高级功能,现在选择了此选项。

如何用 PHP 创建和上传文件?

使用 PHP,可以非常轻松地使用表单将文件上传到服务器,并且与其他方法相比,数据也很安全。配置文件“php.ini”文件有一个必须为要上传的文件设置的变量,该变量称为“file_uploads”,应将其设置为ON以启用上传功能。我们需要执行几个步骤才能在服务器中上传文件。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

在使用表单将文件上传到服务器之前,需要进行一些检查。这些检查称为上传文件的验证。

以下是开发人员编码以验证表单的一些要点:

用 PHP 上传文件

1.文件_上传

要上传的文件,此变量的值应为 ON。如果它不为 ON,则文件无法上传到服务器。因此,它应该始终处于开启状态。

2.上传最大尺寸

该指令用于配置可以使用表单上传到服务器的文件的最大大小。这是一种检查,以查看用户上传的文件大小。文件的默认大小设置为2M(2兆字节),我们可以使用.htaccess文件覆盖这种设置,开发人员可以在其中增加文件的大小。以今天的标准来看,两兆字节并不算多,所以我们可能必须增加它。如果您在尝试上传文件时收到错误消息,指出文件大小超过 upload_max_filesize,则需要增加该值。如果这样做,请务必同时增加 post_max_size。

3.上传_tmp_dir

它设置一个临时目录,用于存储用户上传的文件。大多数情况下,但我们不必担心这个设置。如果我们不设置,系统默认会自动设置可以使用的temp目录。

4. Post_max_size

post_max_size指令允许我们设置POST方法上传的数据的最大大小。由于文件是通过 POST 请求上传的,因此该值必须大于我们为 upload_max_filesize 设置的值。例如,如果 upload_max_filesize 为 20M(20 MB),我们可能需要将 post_max_size 设置为 24M。

5.最大文件上传数

它允许您设置用户一次可以上传的最大文件数。默认每次用户数量为 20。

6.最大输入时间

这是允许脚本解析用户输入数据的秒数。如果我们要处理大尺寸的文件上传,我们应该将其设置为合理的值。 60(60 秒),对于大多数应用程序来说都是一个不错的值。

7.内存_限制

内存限制指令指示脚本可以在服务器中消耗的最大内存量。如果我们在上传大文件期间遇到任何问题,我们需要将该指令的值设置为大于为 post_max_size 指令设置的值。默认情况下,该值设置为 128M(128 兆字节),因此除非我们有非常大的 post_max_size 和 upload_max_filesize,否则我们不必担心。

8.最大执行时间

该指令用于允许脚本在服务器中运行的最大秒数。如果我们在上传大文件的过程中遇到任何问题,我们可以考虑将值增加到更多秒,例如 60(1 分钟),这对于大多数应用程序来说应该很有效。

用 PHP 上传文件的示例

下面给出的是提到的示例::

示例#1

代码:

<!DOCTYPE html>
<html>
<body>
<form action="uploadimage.php" method="POST" enctype="multipart/form-data">
Select any image to upload:
<input type="File" name="FileUpload" id="FileUpload">
<input type="submit" value="Upload" name="SUBMIT">
</form>
</body>
</html>

输出:

用 PHP 上传文件

示例#2

代码:

<!DOCTYPE html>
<html lang="en">
<head>
<title>Photo Upload Form</title>
</head>
<body>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<h1>Upload File</h1>
<label for="fileSelect">Filename:</label>
<input type="file" name="photo" id="FileSelect">
<input type="submit" name="SUBMIT" value="Upload Photo">
<p><strong>Note:</strong> Only .jpg, .jpeg, .gif, .png formats allowed to a max size of 2 MB larger than that cannot not be uploaded.</p>
</form>
</body>
</html>

输出:

用 PHP 上传文件

示例#3

代码:

<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="POST" enctype="multipart/form-data">
Select a file to upload:
<input type="file" name="FileToUpload"/>
<input type="submit" value="Upload" name="submit"/>
</form>
</body>
</html>

输出:

用 PHP 上传文件

示例#4

代码:

<?php
$target_path = "c:/";
$target_path = $target_path.basename( $_FILES['fileToUpload']['name']);
if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_path)) {
echo "File has been uploaded successfully!";
}
else
{
echo "Sorry, file not uploaded, please check and try again!";
}
?>

Output:

用 PHP 上传文件

In the above examples, the user can see the screen that is present in the snapshots. Users will attach the document by clicking the “choose file” option. The file will get attached once the user selects the file from his local machine and clicks on the Upload button to submit the documents to the server. The user will then be prompted a message stating that the file has been uploaded successfully.

Conclusion

In this article, we discussed how a user can upload a file to the server using the form and how an uploaded file can be validated in various forms, and the server restrictions for uploading a file. The user might not understand the process of the backend but the developer has to code in such a way that the document uploaded by the user should be correct and the data is secured.

以上是用 PHP 上传文件的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:PHP Open File下一篇:String in PHP