How to upload image files and replace them in PHP_PHP Tutorial

WBOY
Release: 2016-07-12 08:57:33
Original
1084 people have browsed it

How to upload image files and replace them in php,

First create two files: change.html and change.php

The form code of the change.html file is as follows:



change file example.

< /head>










There are a few things to pay attention to here. First, look at this sentence
,
Here we use the POST method. Some browsers also support the PUT method. Of course, this requires modifying the script, which I do not recommend. enctype="multipart/form-data must be set in the form, so that the server knows that the uploaded file contains regular form information. Remember, this must be set. In addition, a hidden field is needed to restrict uploads The maximum length of the file: , where name must be set to MAX_FILE_SIZE , its value is the maximum length of the uploaded file, the unit is B, here I limit it to 2M. Look at this sentence again: , type="file" specifies the file type. In this way, a basic file upload interface is completed. Next, let’s talk about how to use PHP to process uploaded files. In addition, set it in your php.ini The maximum length of the uploaded file may affect your actual upload. Please modify it according to the actual situation. In addition, the PHP upload is first uploaded to the temporary directory and then moved to the specified directory. The temporary directory can be modified as needed, or Use default value...

The following is the code for the form submission change.php file. Let’s take a look at what this file has:

header("content-type:text/html;charset=utf-8");

/**
* @param string $oldfile The file name that needs to be replaced (including the specific path name)
*/
function changeFile($oldfile){
$newfile = $_FILES['file']['name'];//Get the uploaded file name
$ fileclass = substr(strrchr($newfile, '.'), 1);//Get the extension of the uploaded file and use it for judgment
$type = array("jpg", "gif", "bmp", "jpeg ", "png");//Set the type of files allowed to be uploaded
if(in_array(strtolower($fileclass), $type)){
if(file_exists($oldfile)){
unlink( $oldfile);
}

if(is_uploaded_file($_FILES['file']['tmp_name'])){//Must be uploaded through PHP's HTTP POST upload mechanism
if(move_uploaded_file($_FILES['file'][ 'tmp_name'], $oldfile)){
//Output image preview
echo "

Your file has been uploaded. Upload image preview:

";
}
}else{
echo "
Upload failed, the file is larger than 2M, please upload again < /center>";
}
}else{
$text = implode(",", $type);
echo "
You can only upload the following types of files:", $text, "

";
// echo "<script>alert('You can only upload the following types of files: $text')</script>";
}
}

changeFile("./files/1.png");

You may be a little dizzy when you first read these~~, but if you look at it slowly, you will find that this thing is actually SO EASY! ! Let’s talk about the principle first. This program takes uploading images as an example. It first determines whether the file type is in image format. If so, it uploads the file. Then it uploads the file to and replaces the specified file. If the upload is successful, a preview of the uploaded image is output. Here are some explanations of some functions in the program. Let’s look at substr(strrchr($newfile, '.'), 1) first. What is the role of the strrchar() function? Let me give you an example. For example, for a picture file pic.jpg, we use strrchar() handles, strrchr(pic.jpg,'.'), it will return .jpg, do you understand? This function returns the string after the last occurrence of the string. With substr(), we can get jpg, so that we can get the file extension to determine whether the uploaded file conforms to the specified format. This program puts the specified format in an array, which can be added as needed during actual use.
Next, we call the function to determine the file type and convert it to lowercase strtolower($_FILES['file']['name']), here is The very critical thing $_FILES is a super global array that saves the form data that needs to be processed. If register_globals is turned on, it can also be accessed directly, but this is unsafe. Look at the upload interface just now name="file"
type="file">, based on this form name, we can get a lot of information:
$_FILES['file']['name']-- Get the file name
$_FILES['file']['tmp_name']--Get Temporary storage location
$_FILES['file']['size']--get file size
$_FILES['file']['type'] --Get the file MIME type
After getting this information, you can easily determine the file information. Isn't it very convenient? ^_^, there are some functions that need to be understood next,
file_exists()--determine whether the specified directory exists. If it does not exist, of course we cannot upload it (it seems It’s nonsense! ), is_uploaded_file--determines whether the file has been uploaded via HTTP POST, move_uploaded_file--moves the uploaded file to the specified directory . If the upload is successful, we will output the preview, otherwise the output upload will fail...

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1108339.htmlTechArticleHow to upload image files and replace them in php. First create two files: change.html and change.php change The form code of the .html file is as follows: html head titlechange file exampl...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!


File: