What we are bringing to you today is aboutThe specific code is as follows:
- php
- /**
- * My file Upload class
- *
- * Unfinished Features:
- * 1. The determination of the existence of the target directory
- * 2. Automatically rename if duplicate name appears on upload
- *
- * @author m.q. < [Url]www.mengqi.net[/url] >
- */
- Class upload
- {
- /**
- * PHP upload class upload.php Upload the information of the file, this value is obtained by the constructor, if the upload file failed or error or not uploaded, this value is False
- *
- * @var Array
- */
- Private $ file = false ;
-
-
- /**
- * Constructor: Get the information to upload the file
- *
- * If there is an error in the project of uploading the file, then the error file will not be returned in the result, the files in the result will be available.
- *
- * @param string $tag form form <input> the value of the Name property in the label, example <input name="P" type="File" >
- *
- * Example 1, upload a single file:
- * <input name="upfile" type="file" >
- *
- * Example 2, uploading multiple files:
- * <input name="upfile[]" type="File" >
- * <input name="upfile[]" type="file" >
- *
- * Results (stored in the $file variable) are as follows:
- *
- * Array (
- * [0] = > Array (
- * ' name ' = > ' Abc.txt '
- * ' type ' = > ' Text/plain '
- * ' tmp_name ' = > '/TMP/PHPGXECCB '
- * ' ERROR ' = > 0
- * ' size ' = > +
- * )
- * [1] = > Array (
- * ' name ' = > ' Abc.txt '
- * ' type ' = > ' Text/plain '
- * ' tmp_name ' = > '/TMP/PHPGXECCB '
- * ' ERROR ' = > 0
- * ' size ' = > +
- * )
- * )
- */
- Public function __construct ($tag)
- {
- $ file = $_files[$tag];
-
- if (!isset ($file) | | | empty ($file))
- {
- Return No files Uploaded
- }
-
- $ Num = Count ($file [' name ']);//php upload class upload.php number of uploaded files
-
- $ Data = Array ();//An array of information used to save the uploaded file
- Uploaded multiple Files
- 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 was 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 information for uploading files
- }
-
- /**
- * Move uploaded files from the Temp folder to the destination path
- *
- * @param array of $src file information, is one of the elements of the $file array (still an array)
- * @param string $destpath The target path of the upload
- * @param string $filename The file name after uploading, and if it is empty, use the file name when uploading
- * @return BOOL
- */
- Public function Save ($SRC, $destpath, $filename = null)
- {
- $ Srctname = $src [' tmp_name '];//temporary file name of the original upload
- $ Srcfname = $src [' name ']; Original file name
-
- If the $filename parameter is empty, the file name when uploading is used
- if (empty ($filename))
- {
- $ filename = $srcFName;
- }
-
- $dest is the path and file name to which the files will eventually be copied
- if (empty ($destpath))
- {
- $ dest = $filename;
- }
- Else
- {
- Fix the slash in the path, change the end to/, and if not at the end, add a/
- $ Pathend = $destpath [Strlen ($destpath)-1];//The last character of the destination path to upload
- if ($pathend = = ' \ ')
- {
- $ dest = Substr_replace ($destpath, '/', strlen ($destpath)-1). $filename;
- }
- else if ($pathend! = '/')
- {
- $ dest = $destpath. ' /'. $filename;
- }
- Else
- {
- $ dest = $destpath. $filename;
- }
- }
-
- Upload file successfully
- if (@move_uploaded_file ($srcTName, $dest))
- {
-
- return true;
- }
- Else
- {
- return false;
- }
- }
-
- /**
- * Get information on uploading files
- *
- * @return Array
- */
- Public Function GetFileInfo ()
- {
- return $this- > file;
- }
- }
- $ a = New upload (' upfile ');
- $ FileInfo = $a- > GetFileInfo ();
- if ($fileinfo = = False)
- {
- Echo ' didn't upload the file! ';
- Exit
- }
- For ($i = 0; $i < Count($ FileInfo); $i + +)
- {
- Echo ' uploading '. $fileinfo [$i] [' name ']. ' ';
- if ($a-> Save ($fileinfo [$i], ' upload ')) echo ' complete ';
- else echo ' failure ';
- Echo ' <br> ';
- }
- ?>
The above code is about PHP upload class upload.php The specific use of the method.
http://www.bkjia.com/PHPjc/446389.html www.bkjia.com true http://www.bkjia.com/PHPjc/446389.html techarticle What we are bringing to you today is about the specific code as follows: PHP/** * My file Upload class * * Unfinished function: * *. If the target directory is present .