2017-06-08 156 views
0

這是我上傳文件的php代碼。該代碼顯示了變量初始化行中的udentified index的通知。此外,顯示的輸出是「無效的文件擴展名」,而我正在上傳一個JPEG文件。上傳圖片文件時出現'無效的文件擴展名'錯誤

<?php 


    if (isset($_POST["submit"])) { 

     $name  = $_FILES['file']['name']; 
     $tmpName = $_FILES['file']['tmp_name']; 
     $error = $_FILES['file']['error']; 
     $size  = $_FILES['file']['size']; 
     $ext  = strtolower(pathinfo($name, PATHINFO_EXTENSION)); 

     switch ($error) { 
      case UPLOAD_ERR_OK: 
       $valid = true; 
       //validate file extensions 
       if (!in_array($ext, array('jpg','jpeg','png','gif'))) { 
        echo $ext; 
        $valid = false; 
        $response = 'Invalid file extension.'; 
       } 
       //validate file size 
       if ($size/1024/1024 > 2) { 
        $valid = false; 
        $response = 'File size is exceeding maximum allowed size.'; 
       } 
       //upload file 
       if ($valid) { 
        $targetPath = dirname(__FILE__) . DIRECTORY_SEPARATOR. 'uploads' . DIRECTORY_SEPARATOR. $name; 
        move_uploaded_file($tmpName,$targetPath); 
        header('Location: index.php') ; 
        exit; 
       } 
       break; 
      case UPLOAD_ERR_INI_SIZE: 
       $response = 'The uploaded file exceeds the upload_max_filesize directive in php.ini.'; 
       break; 
      case UPLOAD_ERR_FORM_SIZE: 
       $response = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.'; 
       break; 
      case UPLOAD_ERR_PARTIAL: 
       $response = 'The uploaded file was only partially uploaded.'; 
       break; 
      case UPLOAD_ERR_NO_FILE: 
       $response = 'No file was uploaded.'; 
       break; 
      case UPLOAD_ERR_NO_TMP_DIR: 
       $response = 'Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.'; 
       break; 
      case UPLOAD_ERR_CANT_WRITE: 
       $response = 'Failed to write file to disk. Introduced in PHP 5.1.0.'; 
       break; 
      case UPLOAD_ERR_EXTENSION: 
       $response = 'File upload stopped by extension. Introduced in PHP 5.2.0.'; 
       break; 
      default: 
       $response = 'Unknown error'; 
      break; 
     } 

     echo $response; 
    } 
    ?> 

HTML文件如下:

<html> 
    <body> 
    <form method = "post" action = "upload.php"> 
     <input type="file" name="file"> 
     <input type="submit" value="Upload Image" name="submit"> 
    </form> 
    </body> 
</html> 
+0

檢查你的'$ ext'包含發送文件? –

+0

當我嘗試打印這些變量的值時,它不打印任何東西,@MayankPandeyz –

+0

嘗試在表單標記中添加屬性enctype =「multipart/form-data」 –

回答

0

添加屬性enctype="multipart/form-data"在表單標籤與形式

+0

錯誤已解決,但文件未上傳至上述目錄@Daniyal –

+0

更改上傳目錄@ManyaAgarwal – Daniyal

+0

沒有什麼區別,路徑存在。 –

相關問題