2016-04-11 16 views
1

kullanıyorum çalıştı:PHP kullanarak çok parçalı form verilerini nasıl tespit edebilirim?

$this->method = $method = $_SERVER['REQUEST_METHOD']; 

ama Aslında POST yöntemi kullanılarak gönderilir

POST

döndürür, ancak en az benim durumumda her iki

çok parçalı bir anlam olduğunu

$_POST ve $_FILE doldurulur.

Teşekkür ederiz.

+2

'isset ($ _ DOSYASI)': böyle bir şey işlemek için Ardından

<form action="upload.php" method="post" enctype="multipart/form-data"> Select file to upload: <input type="file" name="File" id="fileToUpload"> <input type="submit" value="Upload File" name="submit"> </form> 

: Form böyle bir şey yapmak? – Jon

+1

, $ _FILES okuyamıyor mu? – MacGyer

+0

Kesintisiz parça verisi gönderebilmenizin tüm yollarından emin değilim, ama biliyorum ki $ _FILES' ** bir **. –

cevap

-1

Formu kurarken çoktan/form verisi ayarlamalısınız. Daha sonra, formu işlediğinizde, dosyanın dosya adını almak için yükleme dosyasının ve _FILES dosyasının dosya adını almak için _POST'a bakmış olursunuz. Aşağıda, .PDF veya Word docs'ın çok parçalı yüklemelerini işlemek için yazdığım bir uygulama için eski bir kod var. Öncelikle, dosya adının _POST içinde ayarlandığından emin olun, sonra izin verilen bir uzantı içerdiğini kontrol edin, ardından uzantının beklenen MIME türüyle eşleştiğinden emin olun (herhangi bir şeyin yüklenmesine izin vermekten biraz daha güvenli):

if (isset($_POST['File'])) { 
    if (isset($_FILES['File']['name']['filename'])) { 
     if (!$_FILES['File']['error']['filename']) { 
     // Check mime type 
     $type = $_FILES['File']['type']['filename']; 
     if ($type != 'application/pdf' && $type != 'application/msword' && 
         $type != 'application/vnd.openxmlformats-officedocument.presentationml.presentation') { 
      echo "<br>Sorry - type: " . $type . " is not supported. "; 
      die; 
     } 
     $fi = finfo_open(FILEINFO_MIME_TYPE); 
     //verify file infor matches the mime type; 
     $mime = finfo_file($fi, $_FILES['File']['tmp_name']['filename']); 
     finfo_close($fi); 
     if ($mime != $type) { 
      echo "<br>Sorry this file type: " . $type . " does not match the mime type: " . $mime; 
      die; 
     } 
     // Check that the extension is allowed 
     $filename = strtolower($_FILES['File']['name']['filename']) 
     $whitelist = array('pdf', 'doc', 'pptx'); 
     $ext = end(explode('.', $filename)); 
     if (!in_array($ext, $whitelist)) { 
      echo '\n<br> Sorry file ' . $filename . ' has invalid extension: ' . $ext; 
      die; 
     }  
     // If you made it to here - ok to process file    
     } else { 
     // Give back a message for debugging 
     switch ($_FILES['File']['error']['filename']) { 
      case 1: 
       $errMsg = "UPLOAD_ERR_INI_SIZE - Size " . $img->size . " exceeds php.ini upload_max_filesize"; 
       break; 
       case 2: 
        $errMsg = "UPLOAD_ERR_FORM_SIZE - Size " . $img->size . " exceeds MAX_FILE_SIZE of HTML form"; 
        break; 
       case 3: 
        $errMsg = "UPLOAD_ERR_PARTIAL - File did not complete uploading"; 
        break; 
       case 4: 
        $errMsg = "UPLOAD_ERR_NO_FILE - File failed to upload"; 
        break; 
       case 6: 
        $errMsg = "UPLOAD_ERR_NO_TMP_DIR - Missing tmp folder "; 
        break; 
       case 7: 
        $errMsg = "UPLOAD_ERR_CANT_WRITE - Failed to write file to disk"; 
        break; 
       case 8: 
        $errMsg = "UPLOAD_ERR_EXTENSION - php extenion stopped this file from uploading - check php.ini"; 
        break; 
       default: 
        $errMsg = $_FILES['File']['error']['filename'] . " - Unknown error in image upload"; 
       } 
      echo "File Upload died with errors. " . $errMsg; 
      die; 
    } 
} 
İlgili konular