PHP upload class upload. php Usage method. What we bring to you today is the specific code as follows :? Php *** my file Upload class ** unfinished functions: * 1. determine whether the target directory exists * 2. what we bring to you today when uploadingThe code is as follows:
- Php
-
- /**
- * My file Upload class
- *
- * Unfinished functions:
- * 1. determine whether the target directory exists
- * 2. if a duplicate name appears during the upload, it is automatically renamed.
- *
- * @ Author M.Q.<[Url] www.mengqi.net [/url]>
- */
- Class upload
- {
- /**
- * PHP upload class upload. php file upload information. this value is obtained by The constructor. if the file upload fails or is not uploaded, this value is false.
- *
- * @ Var array
- */
- Private $File=False;
-
-
- /**
- * Constructor: obtains the information of the uploaded file.
- *
- * If an error occurs in the project where the file is uploaded, the error file will not be returned in the result, and all files in the result are available.
- *
- * @ Param string $ tag form<Input>Name attribute value in the tag, for example<Input Name="P" Type="File">
- *
- * Example 1: upload a single file:
- *<Input Name="Upfile" Type="File">
- *
- * Example 2: upload multiple files:
- *<Input Name="Upfile []" Type="File">
- *<Input Name="Upfile []" Type="File">
- *
- * The result (saved in the $ file variable) is as follows:
- *
- * Array (
- * [0] =>Array (
- * 'Name' =>'Abc.txt'
- * 'Type' =>'Text/plain'
- * 'Tmp _ name' =>'/Tmp/phpgxecCb'
- * 'Error' =>0
- * 'Size' =>62
- *)
- * [1] =>Array (
- * 'Name' =>'Abc.txt'
- * 'Type' =>'Text/plain'
- * 'Tmp _ name' =>'/Tmp/phpgxecCb'
- * 'Error' =>0
- * 'Size' =>62
- *)
- *)
- */
- Public function _ construct ($ tag)
- {
- $File= $ _ FILES [$ tag];
-
- If (! Isset ($ file) | empty ($ file ))
- {
- Return; // no file is uploaded
- }
-
- $Num=Count($ File ['name']); // number of files uploaded by the PHP upload class upload. php
-
- $Data=Array(); // Array used to save the information of the uploaded file
-
- // Multiple files are uploaded.
- If ($ num>1)
- {
- For ($I=0; $ I<$ Num; $ I ++)
- {
- $D=Array();
- $ D ['name'] = $ file ['name'] [$ I];
- $ D ['type'] = $ file ['type'] [$ I];
- $ D ['tmp _ name'] = $ file ['tmp _ name'] [$ I];
- $ D ['error'] = $ file ['error'] [$ I];
- $ D ['size'] = $ file ['size'] [$ I];
-
- If ($ d ['error'] = 0)
- {
- $ Data [] = $ d;
- }
- Else
- {
- @ Unlink ($ d ['tmp _ name']);
- }
- }
- }
- // Only one file is uploaded.
- Else
- {
- $D=Array();
- $ D ['name'] = $ file ['name'];
- $ D ['type'] = $ file ['type'];
- $ D ['tmp _ name'] = $ file ['tmp _ name'];
- $ D ['error'] = $ file ['error'];
- $ D ['size'] = $ file ['size'];
-
- If ($ d ['error'] = 0)
- {
- $ Data [] = $ d;
- }
- Else
- {
- @ Unlink ($ d ['tmp _ name']);
- }
- }
-
- If (empty ($ data) return;
-
- $ This-> File= $ Data; // save the information of the uploaded file
- }
-
- /**
- * Move the uploaded file from the temporary folder to the target path.
- *
- * @ Param array $ src file information array, which is an element of the $ file array (still an array)
- * @ Param string $ destpath the destination path for the Upload
- * @ Param string $ filename: name of the uploaded file. if it is null, the uploaded file name is used.
- * @ Return bool
- */
- Public function save ($ src, $ destpath, $Filename=Null)
- {
- $SrcTName= $ Src ['tmp _ name']; // temporary file name of the original uploaded File
- $SrcFName= $ Src ['name']; // original file name
-
- // If the $ filename parameter is null, the file name is used during the upload.
- If (empty ($ filename ))
- {
- $Filename= $ SrcFName;
- }
-
- // $ Dest is the final path and file name of the file to be copied.
- If (empty ($ destpath ))
- {
- $Dest= $ Filename;
- }
- Else
- {
- // Modify the slash in the path to/. if the slash is not at the end, add a slash to the end/
- $Pathend= $ Destpath [strlen ($ destpath)-1]; // The last character of the uploaded destination path
- If ($Pathend= '\')
- {
- $Dest=Substr_replace($ Destpath, '/', strlen ($ destpath)-1). $ filename;
- }
- Else if ($ pathend! = '/')
- {
- $Dest= $ Destpath. '/'. $ filename;
- }
- Else
- {
- $Dest= $ Destpath. $ filename;
- }
- }
-
- // File uploaded successfully
- If (@ move_uploaded_file ($ srcTName, $ dest ))
- {
-
- Return true;
- }
- Else
- {
- Return false;
- }
- }
-
- /**
- * Obtain the information of the uploaded file
- *
- * @ Return array
- */
- Public function getFileInfo ()
- {
- Return $ this->File;
- }
- }
-
-
- $A=NewUpload ('upfile ');
-
- $Fileinfo= $->GetFileInfo ();
- If ($Fileinfo= False)
- {
- Echo 'no file uploaded! ';
- Exit;
- }
-
- For ($I=0; $ I< Count($ Fileinfo); $ I ++)
- {
- Echo 'uploading '. $ fileinfo [$ I] ['name']. '';
- If ($->Save ($ fileinfo [$ I], 'upload') echo 'complete ';
- Else echo 'failed ';
- Echo'<Br>';
- }
- ?>
The above code describes how to use PHP upload class upload. php.
The specific code of callback is as follows :? Php/*** my file Upload class ** unfinished features: * 1. determine whether the target directory exists * 2. if the file is uploaded...