PHP File Upload form excerpt from Drupal code _php tips

Source: Internet
Author: User
Tags html form learn php php code php development environment php file upload drupal
Example of a Drupal file upload form
Copy Code code as follows:

function Upload_form () {
$form = Array ();
If This #attribute isn't present, upload'll fail on submit
$form [' #attributes '] [' enctype '] = ' 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 would 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
PHP in the development of Web sites, PHP program how to achieve file upload function, has been a novice topic. and file upload function is generally useful, such as picture upload. Today, the combination of specific code examples and detailed annotations and you share how to write php file upload code, suitable for beginners to learn PHP.
PHP code example is mainly about the image upload, read the program after you can modify the relevant file types can be implemented other file upload function.
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.
Write a upload file, set file Upload form
Copy Code code 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>

Attention
1. The enctype= "Multipart/form-data" in the form must be specified so that the server knows that the file has regular form information.
2, must have a file can be set to upload the maximum length of the form area, that is, the maximum allowable upload file (in bytes), it is a hidden value range, that is max_file_size, by setting its value (values) can limit the size of the upload file, Avoid the trouble that a user spends time waiting to upload a large file before discovering the file is too big. But generally others can bypass this value, so for security reasons, it is best to configure the Upload_max_filesize option in the php.ini file, set the file upload size, the default is 2M.
File Upload Program
Copy Code code 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 ("File upload directory does not exist and cannot create file upload directory");
if (!chmod ($upload _dir,0755))
Die ("File upload directory permissions cannot be set to readable or writable");
}
if ($size > $MAX _size)
Die ("uploaded file size exceeds the specified size");
if ($size = = 0)
Die ("Please select File to upload");
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 ("Copy file failed, please upload again");
Switch ($error)
{
Case 0:
return;
Case 1:
Die ("The uploaded file exceeds the value of the Upload_max_filesize option limit in php.ini");
Case 2:
Die ("The size of the upload file exceeds the value specified by the Max_file_size option in the HTML form");
Case 3:
Die ("file is only partially uploaded");
Case 4:
Die ("No file uploaded");
}
}

Parameter description
$type, $name, $size, $error, $tmp _name corresponding variables in the global variable $_files, namely:
$_files[' userfile ' [' type ']: The MIME type of the file that requires the browser to provide support for that information, such as the picture type "Image/gif".
$_files[' UserFile ' [' Name ']: The original name of the client file.
$_files[' userfile ' [' Size ']: the size of the uploaded file, in bytes.
$_files[' UserFile ' [' Tmp_name ']: a temporary filename stored on the server after the file is uploaded.
$_files[' userfile ' [' Error ']: The error code associated with the file upload, i.e.
Value: 0: No error occurred, file upload success.
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 are uploaded.
$ext: Uploading file name extensions
$targetname: Final filename after file is uploaded
$upload _dir: Which directory to upload to, using a relative path
Comments:
Line 3rd ~ 6th: Set the image file upload size, and the file MIME type and extension, because this code for the image file upload program, so two array of all the picture types, such as PNG,GIF,JEPG.
Line 17th ~ 24th: If the file is empty, size equals 0, and if the image file extension or type does not match, jump out.
Line 26th: The function of the Move_uploaded_file function is to move the file in the Upload_tmp_dir server temp directory to the file specified by $file_path, and note that if the target file already exists, overwrite the target file
How do I upload multiple files? Like uploading 3 files at a time.
Simply Place
Copy Code code as follows:

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

Change into
Copy Code code as follows:

<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.
Summarize
This function is php file upload the simplest core code, image upload is only one of them, only need to modify or expand the $file_mimes and $file_exts array of relevant information, you can achieve other types of file upload function. In the periphery of the function, write the related code according to own need to realize other functions, such as the association with the database and so on.

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.