Example of a Drupal file upload form
Copy CodeThe code is as follows:
function Upload_form () {
$form = Array ();
If This #attribute was not present, upload would fail on submit
$form [' #attributes '] [' enctype '] = ' multipart/form-data ';
$form [' file_upload '] = Array (
' #title ' and 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'll 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 the development of PHP Web site, PHP program how to implement file upload function, has always been a novice topic. and file upload function is generally useful, than the chip upload. Today, combined with specific code examples and detailed comments and everyone to share how to write php file upload code, 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 files upload function.
Programming environment
PHP5.2.4, basically PHP4.3 or later, this code can be used
Preparatory work
Check for upload_tmp_dir items
If the PHP development environment is self-built, you need to edit the php.ini file before you write the file upload program, 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 fool-type one-click installation package, the general Upload_tmp_dir is set up, you can also use the phpinfo () function to view the configuration.
Write a upload file, set the file upload form
Copy CodeThe code is as follows:
Attention
1. Enctype= "Multipart/form-data" in the form must be specified to let the server know that the file has regular form information.
2, must have an upload file maximum length of the form area, which is allowed to upload the maximum value of the file (in bytes), it is a hidden range, that is, max_file_size, by setting its value (value) can limit the size of the uploaded file, Avoid the hassle of having to wait for a large file to be uploaded before the user discovers that the file is too big. But generally others can bypass this value, so security, 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 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 ("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 writable");
}
if ($size > $MAX _size)
Die ("The size of the uploaded file 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 re-upload");
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 uploaded file exceeds the value specified by the Max_file_size option in the HTML form");
Case 3:
Die ("files are only partially uploaded");
Case 4:
Die ("No files are uploaded");
}
}
Parameter description
$type, $name, $size, $error, $tmp _name corresponds to the relevant variable in the global variable $_files, namely:
$_files[' userfile ' [' type ']: The MIME type of the file, which requires the browser to provide support for that information, the sample 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 ']: temporary file names stored on the server after the files have been uploaded.
$_files[' userfile ' [' Error ']: The error code associated with the file upload, i.e.
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 uploaded 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.
$ext: Upload file name extension
$targetname: The final filename after file upload
$upload _dir: Which directory to upload to, using a relative path
Comments:
Line 3rd ~ 6th: Set the size of the image file upload, as well as the file's MIME type and extension, as this code is the picture file upload program, so two arrays listed all the picture types, such as PNG,GIF,JEPG.
Line 17th ~ 24th: If the file is empty, size is equal to 0, and if the extension or type of the picture file does not match, it jumps out.
Line 26th: The function of the Move_uploaded_file function is to move the files in the server temporary directory Upload_tmp_dir set to the file specified by $file_path, and note that if the destination file already exists, overwrite the target file
How do I upload multiple files? Like uploading 3 files at a time.
Just add
Copy CodeThe code is as follows:
Change into
Copy CodeThe code is as follows:
Corresponding when calling this function, $_files[' userfile ' [' Name '][0], which represents the relevant file information for the first file, and so on, and so on.
Summarize
This function is the simplest core code in PHP file upload, image upload is just one of them, only need to modify or expand the $file_mimes and $file_exts array information, you can implement other types of file upload function. In the periphery of the function, writing related other code according to your own needs can implement other functions, such as database Association and so on.
http://www.bkjia.com/PHPjc/322934.html www.bkjia.com true http://www.bkjia.com/PHPjc/322934.html techarticle example of a Drupal file upload form copy the code as follows: function Upload_form () {$form = array ();/If This #attribute are not present, upload would Fail on Submit $form [' #attri ...