PHP receives uploaded files

Source: Internet
Author: User
Tags error code file size file upload
PrefaceThis Monday straight again to engage in PHP, involving file upload part of some forgotten, here record

Form form for HTMLUsing HTML forms to simulate a POST request for a file upload, the code is as follows:



 
  
File Upload




 
  
	
  
   
	Send this File:
  
   
	
  
   

 
  




Attention:
To ensure that the properties of the file upload form are enctype= "Multipart/form-data", otherwise the file will not upload

PHPFirst, you need to explain the PHP global variable $_files, which contains all the uploaded file information
$_file[' UserFile ' [' Name ']: The original name of the client machine file
$_file[' userfile ' [' type ']: MIME type of File
$_file[' userfile ' [' size ']: Uploaded file size
$_file[' UserFile ' [' Tmpname ']: temporary filename stored on server after file is uploaded
$_file[' userfile ' [' Error ']: and the error code uploaded by the file

Ideas

1, generate 40-bit random string as file name 2, according to the file is picture or voice to the different file location 3, temporarily do not do file size and file type of checksum
function Processfile ($files, $type) {
    $uploadName = null;
    foreach ($files as $name => $value) {
        $originalName = $value [' name '];
        $arr = Explode (".", $originalName);
        $postfix = $arr [Count ($arr)-1];
        $tmpPath = $value [' Tmp_name '];
        $tmpType = $value [' type '];
        $tmpSize = $value [' size '];
    }
    
    $newname = Ehlstaticfunction::generaterandomstr (40). ".". $postfix;
    
    Switch ($type) {case
        1: 
            //process sound file
            $destination = Videouploaddir. $newname;
            break;
        Case 2:
            //Processing image file
            $destination = Imageuploaddir. $newname;
            break;
    
    Move_uploaded_file ($tmpPath, $destination);
}




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.