Example of drupal File Upload form
Copy codeThe 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
Copy codeThe Code is as follows:
<Form enctype = "multipart/form-data" action = "upload. php" method = "post">
<Input type = "hidden" name = "max_file_size" value = "100000">
<Input name = "userfile" type = "file">
<Input type = "submit" value = "Upload File">
</Form>
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
Copy codeThe 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
Copy codeThe Code is as follows:
<Input name = "userfile" type = "file">
Change
Copy codeThe Code is as follows:
<Input name = "userfile []" type = "file">
<Input name = "userfile []" type = "file">
<Input name = "userfile []" type = "file">
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.