PHP file upload form from drupal code _ PHP Tutorial

Source: Internet
Author: User
Tags php development environment php file upload php website drupal
The PHP file upload form is taken from the drupal code. The following code copies an example of a drupal file upload form: functionupload_form () {$ formarray (); Ifthis # attributeisnotpresent, uploadwillfailonsubmit $ form [# attri drupal file upload form

The code is as follows:


Function upload_form (){
$ Form = array ();
// If this # attribute is not present, upload will fail on submit
$ Form ['# attributes'] ['enablesype '] = 'multipart/form-data ';
$ Form ['File _ upload'] = array (
'# Title' => t ('Upload file '),
'# Type' => 'File ',
);
$ Form ['submit _ upload'] = array (
'# Type' => 'Submit ',
'# Value' => 'Submit'
);
Return $ form;
}
Function upload_submit ($ form, & $ form_state ){
$ Validators = array ();
$ Dest = file_directory_path ();
$ File = file_save_upload ('File _ upload', $ validators, $ dest );
// $ File will be 0 if the upload doesn't exist, or the $ dest directory
// Isn' t writable
If ($ file! = 0 ){
$ File-> filepath; // file relative path
}
Else {
Form_set_error ('myform', t ("Failed to save the file ."));
}
}


Php file upload function code example tutorial
In PHP website development, how PHP programs implement the file upload function has always been a new topic. In addition, the file upload function is generally used, such as multipart Upload. Today, we will share with you how to compile php file upload code based on specific code examples and detailed annotations, which is suitable for beginners of php.
The PHP code example mainly describes how to upload images. after reading the program, you can modify the file type to upload other files.
Programming Environment
PHP5.2.4, PHP4.3 or later, which can be used
Preparations
Check the upload_tmp_dir item
If the PHP development environment is self-built, you need to edit php before writing a file upload program. ini file, find and edit the upload_tmp_dir option, which is used to set the 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 silly one-click installation package, generally upload_tmp_dir is set. you can also use the phpinfo () function to view the configuration.
Compile an upload file and set the file upload form

The code is as follows:




Note:
1. enctype = "multipart/form-data" must be specified in the form to let the server know that the file carries regular form information.
2. there must be a form area that can be set to the maximum length of the uploaded file, that is, the maximum size of the file to be uploaded (calculated in bytes). It is a hidden value range, that is, max_file_size, you can set the Value to limit the size of the uploaded file. This prevents users from having to wait for the upload of a large file before finding that the file is too big and troublesome. However, this value can be bypassed by others. for security reasons, it is best to configure the upload_max_filesize option in the php. ini file to set the file upload size. the default value is 2 MB.
File upload program

The code is as follows:


Function uploadfile ($ type, $ name, $ ext, $ size, $ error, $ tmp_name, $ targetname, $ upload_dir)
{
$ MAX_SIZE = 2000000;
$ FILE_MIMES = array ('image/pjpeg ', 'image/jpeg', 'image/jpg ', 'image/GIF', 'image/png ');
$ FILE_EXTS = array('.jpg ', '.gif', '.png ','. JPG ','. GIF ','. PNG ');
$ File_path = $ upload_dir. $ targetname;
If (! Is_dir ($ upload_dir ))
{
If (! Mkdir ($ upload_dir ))
Die ("the file upload directory does not exist and the file upload directory cannot be created ");
If (! Chmod ($ upload_dir, 0755 ))
Die ("the file upload directory permission cannot be set to readable and writable ");
}
If ($ size> $ MAX_SIZE)
Die ("The size of the uploaded file exceeds the specified size ");
If ($ size = 0)
Die ("Select the uploaded file ");
If (! In_array ($ type, $ FILE_MIMES) |! In_array ($ ext, $ FILE_EXTS ))
Die ("please upload the file type that meets the requirements ");
If (! Move_uploaded_file ($ tmp_name, $ file_path ))
Die ("failed to copy the file, please upload it again ");
Switch ($ error)
{
Case 0:
Return;
Case 1:
Die ("the uploaded file exceeds the limit of the upload_max_filesize option in php. ini ");
Case 2:
Die ("The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form ");
Case 3:
Die ("only part of the file is uploaded ");
Case 4:
Die ("no files are uploaded ");
}
}


Parameter description
$ Type, $ name, $ size, $ error, $ tmp_name corresponds to the related variables in the global variable $ _ FILES, that is:
$ _ FILES ['userfile'] ['type']: MIME type of the file, which must be supported by the browser. the sample file type is "image/gif ".
$ _ FILES ['userfile'] ['name']: The original name of the client file.
$ _ FILES ['userfile'] ['size']: size of the uploaded file, in bytes.
$ _ FILES ['userfile'] ['tmp _ name']: temporary file name stored on the server after the file is uploaded.
$ _ FILES ['userfile'] ['error']: error code related to the file upload, that is
Value: 0: No error occurs. the file is uploaded successfully.
Value: 1: the uploaded file exceeds the limit of the upload_max_filesize option in php. ini.
Value: 2: The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form.
Value: 3: only part of the file is uploaded.
Value: 4: no file is uploaded.
$ Ext: File Upload extension
$ Targetname: The final file name after the file is uploaded
$ Upload_dir: directory to which the object is uploaded. the relative path is used.
Note:
3rd rows ~ Row 6th: sets the size of the image file to be uploaded, the MIME type and extension of the file. because this code is an image file upload program, all the image types are listed in the two arrays, for example, PNG, GIF, and JEPG.
17th rows ~ Row 24th: If the file is empty, the size is equal to 0. If the image file extension or type does not match, the file jumps out.
Row 26th: the move_uploaded_file function moves the files in the temporary directory of the server set by upload_tmp_dir to the files specified by $ file_path. Note that if the target file already exists, it overwrites the target file.
How to upload multiple files? For example, upload three files simultaneously.
You only need

The code is as follows:




Change

The code is as follows:






When this function is called, $ _ FILES ['userfile'] ['name'] [0] indicates information about the first file, the same applies to others.
Summary
This function is the simplest core code for PHP file uploading. image uploading is only one of them. you only need to modify or expand the information about the $ FILE_MIMES and $ FILE_EXTS arrays, you can upload other types of files. On the periphery of the function, you can write other relevant code as needed to implement other functions, such as database Association.

The upload code is as follows: function upload_form () {$ form = array (); // If this # attribute is not present, upload will fail on submit $ form ['# attri...

Related Article

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.