search
HomeBackend DevelopmentPHP Tutorialphp file upload_PHP tutorial
php file upload_PHP tutorialJul 20, 2016 am 11:07 AM
phpuploaduseCanexistconstructionyouTutorialdocumentusemanageneedfirst

php Tutorial File Upload

Before you can use PHP to manage your uploads, you first need to build an HTML form that serves as the user interface for uploading files

. Have an example look below and save an editable HTML code.




< ;input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
Choose a file to upload:



When there are some rules that need to be constructed Follow the HTML form. First, make sure the form uses the POST method. Second, the form

requires the following attributes: character encoding = "multiple/form-data". It specifies the content type used when submitting

information to the server. Without these requirements, your file cannot be uploaded.

Another thing to note is that the hidden form field named MAX_FILE_SIZE sets the value. Some web browsers

actually reflect this field and will not allow users to upload files larger than this number (in bytes). You should

set this value to match the maximum upload size, set in the php.ini file. This is a set with

upload_max_filesize, the default value is 2MB. But it still doesn't guarantee that your script won't deliver files that are

inches larger. The danger is that an attacker will try to send you a request for several large files and fill out the file system, which is where PHP stores the decoded files. Set the post_max_size directive in php.ini to the maximum

file size you want (must be greater than upload_max_filesize). The default value is 10MB. This directive controls the maximum size of POST data allowed within a

request. Also, make sure that file_uploads is set to On in your php.ini file

.

At least, there is a look at the input tag attribute: type="file". It is used to specify the input

element for file selection control. This provides a file URI in place that you need to type into a "Browse" button and can be used as an alternative to the

URI input.

After the user enters a file URI and clicks the submit button a copy of the file will be sent to the server and the user will

be redirected to upload.php. This PHP file will handle form data.

Back to top

Handling form data (PHP code)

When the file is uploaded and PHP creates a temporary copy of the file and sets up the superglobal variable $_FILES Array,

contains information about the file. For each file, there are 5 data. We have uploaded the field named

as 'uploaded_file', so the following data will be present:

Variable $_FILES ["uploaded_file"] ["name"] The original of the file uploaded from the user's machine Name

Variable $_FILES["uploaded_file"]["type"] The MIME type of the uploaded file (if the browser


provides the type)

Variable $_FILES["uploaded_file"][" size"] The size of the uploaded file in bytes

variable $_FILES["uploaded_file"] ["tmp_name"], where the file is temporarily stored on the server

variable $_FILES ["uploaded_file"] ["error"] Error code Result from file upload

The following example accepts an uploaded file and saves it in the upload directory. It allows uploading only JPEG

images under 350Kb. The code itself is fairly self-explanatory, but we'll give it some explanation. There is an example in the look and save

save this PHP code as upload.php.

//Сheck that we have a file
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error']

== 0)) { //Check if the file is JPEG image and it's size is less than 350Kb

$filename = basename($_FILES['uploaded_file']['name' ]);
$ext = substr($filename, strrpos($filename, '.') + 1);
if (($ext == "jpg") && ($_FILES["uploaded_file"] ["type"] == "image/jpeg")

&&

($_FILES["uploaded_file"]["size"] //Determine the path to which we want to save this file
$newname = dirname(__FILE__).'/upload/'.$filename;
//Check if the file with the same name is already exists on the

server if (!file_exists($newname)) {

//Attempt to move the uploaded file to it's new place
if ((move_uploaded_file($_FILES['uploaded_file']

['tmp_name'],$newname))) {
                                                                     echo "It's done! The file has been saved as: ".$newname; occurred during file upload!";
       }
       } else {
               echo "Error: File ".$_FILES["uploaded_file"]["name"]." already

exists" ;

}

} else {
echo "Error: Only .jpg images under 350Kb are accepted for upload";
}
} else {
echo "Error: No file uploaded ";
}
?>
Before uploading the file you need to determine if the file is actually uploading anything. After that, we check the files uploaded on

, JPEG images, and their sizes are less than 350Kb. Next, we determine the path, which is the name of these files where we want to save this file and check if there is already a server. When all checks pass,

we copy the file to a permanent location using the move_upload_file() function. This feature also confirms that the

file you want to process is a legitimate file as a result of the user upload. If the file is uploaded successfully, the corresponding

message will appear.

Note: Make sure PHP has allowed reading and writing of temporary files in the directory where you want to copy the files.

This example is actually very simple. It is proposed to demonstrate how to use PHP to upload files. For example, you can add

new conditions and allow uploading of GIF and PNG images, or any other kind of files you need. If you are new to this tutorial

using PHP might be a good place to start.


http://www.bkjia.com/PHPjc/444978.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/444978.htmlTechArticlephp tutorial file upload Before you can use PHP to manage your uploads, you first need to build the HTML as the user interface File uploaded via form. Have an example look below and save one...
Statement
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!