PHP functions for uploading picture files in PHP

Source: Internet
Author: User

Programming environment

PHP5.2.4, basically PHP4.3 the above version, this code can be used

Preparatory work

Check Upload_tmp_dir items

If the PHP development environment is built on its own, you need to edit the php.ini file before you write the file upload program, and find and edit the Upload_tmp_dir option, which is used to set up a temporary folder when the file is uploaded to the server, such as Upload_tmp_dir = e:/ Phpos/uploads, and then restart Apache. If the PHP development environment uses a fool-type one-click installation package, the general Upload_tmp_dir are set up, you can also use the phpinfo () function to view the configuration

Html

The code is as follows Copy Code
<input name= "UserFile" type= "File" >

Example:

The code is as follows Copy Code

if (! empty ($_files [' file '] [' name '])} {
$img = $this->up_file ("file");
}

Up_file () function:

The code is as follows Copy Code

function Up_file ($inputname, $destinate = ". /data/agency/", $type =" ", $maxSize = 0) {
$arr = Explode ('. ', $_files [$inputname] [' name ']);
$count = count ($arr);
$typearr = Explode (', ', $type);

if (! empty ($type)) {
if (! In_array ($arr [$count-1], $typearr)) {
$this->show_warning (' file type not allowed to upload ');
Exit ();
}
}
if ($maxSize!= 0) {
if (($_files [$inputname] [' size ']/1000) >= $maxSize) {
$this->show_warning (' file size exceeds limit ');
Exit ();
}
}

if (! empty ($destinate)) {
$destinate = substr (Str_replace ("", "/", $destinate),-1) = = "/"? $destinate: $destinate. "/";
if (! is_writable ($destinate)) {
$this->show_warning (' File directory error ');
Exit ();
}
}
$filename = Date ("Ymdhis"). (Microtime () * 1000000). "." . $arr [$count-1];

if (Is_file ($destinate. $filename)) {
Up_file ($inputname, $destinate = "", $type = "", $maxSize = 0);
} else {
$filename = $filename;
}
Copy ($_files [$inputname] [' tmp_name '], $destinate. $filename);
@unlink ($_files [$inputname] [' tmp_name ']);
return $filename;
}

The contents of the $_files array in the example above are shown below. We assume that the File upload field name is UserFile (name can be arbitrarily named)
How do I upload multiple files? Like uploading 3 files at a time.

Simply Place

The code is as follows Copy Code


<input name= "UserFile" type= "File" >
Change into

<input name= "userfile[]" type= "file" >
<input name= "userfile[]" type= "file" >
<input name= "userfile[]" type= "file" >

When this function is called, the $_files[' userfile ' [' name '][0] represents the related file information for the first file, and so on.

$_files[the original name of the ' UserFile ' [' Name '] client machine file.
The MIME type of the $_files[' userfile ' [' type '] file requires the browser to provide support for that information, such as "Image/gif".
$_files[' userfile ' [' size '] the size of the uploaded file, in bytes.
$_files[' UserFile ' [' tmp_name '] files are stored in the server-side temporary file name after being uploaded.
$_files[' userfile ' [' Error '] and the file upload related error code

Value: 0; No error occurred, file upload succeeded.
Value: 1; The uploaded file exceeds the value of the Upload_max_filesize option limit in php.ini.
Value: 2; The size of the upload file exceeds the value specified by the Max_file_size option in the HTML form.
Value: 3; The file is only partially uploaded.
Value: 4; No files were uploaded.

Failure to upload large files solution

The temporary directory where files are uploaded must be open and be writable by the user of the PHP process owner. If not specified, PHP uses the system default value.

Max_execution_time = 90
File_uploads = On
Upload_max_filesize = 2M Set the maximum size of the file upload

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.