上传文件不适用于mp3和mp4

时间:2016-03-25 02:17:29

标签: php forms file upload

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

这里处理的是图片,但没有使用mp3,mp4,我也改变了.ini文件中的upload_max_size但是这不起作用      

 $target_dir = "videos/";
 echo '<br>';
 echo $target_file = $target_dir . $_FILES['fileToUpload']['name'];
 $uploadOk = 1;

 echo '<br>';
 //$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
 // Check if image file is a actual image or fake image 
 if(isset($_POST["submit"])) {
 echo 'started';
 echo '<br>';    
 echo $check = $_FILES['fileToUpload']['tmp_name'];
 if($check !== false) {
    move_uploaded_file($check,$target_file);
    echo 'Uploaded';
     } else {
    echo "File is not an image.";
    $uploadOk = 0;
    } 
  }
  ?> 

1 个答案:

答案 0 :(得分:1)

看起来你应该设置其他php.ini变量,例如:

  • post_max_size = 200M
  • upload_max_filesize = 200M

但是你总是可以发送大于这个限制的文件(然后超级全局_POST和_FILES都是空的)所以我认为最好的做法是使用这样的

if ( $_SERVER['REQUEST_METHOD'] == 'POST' && empty($_POST) &&
     empty($_FILES) && $_SERVER['CONTENT_LENGTH'] > 0 )
{      
  $displayMaxSize = ini_get('post_max_size');

  switch ( substr($displayMaxSize,-1) )
  {
    case 'G':
      $displayMaxSize = $displayMaxSize * 1024;
    case 'M':
      $displayMaxSize = $displayMaxSize * 1024;
    case 'K':
       $displayMaxSize = $displayMaxSize * 1024;
  }

  $error = 'Posted data is too large. '.
           $_SERVER[CONTENT_LENGTH].
           ' bytes exceeds the maximum size of '.
           $displayMaxSize.' bytes.";
}

代码ant解释可以在这里找到:http://andrewcurioso.com/blog/archive/2010/detecting-file-size-overflow-in-php.html