2015-10-21 121 views
1

因此,我使用树莓派,并希望将它用作可以上传和下载任何类型的文件的任何类型的扩展名的地方。允许php上传脚本的所有文件扩展名

这里是我的html

<!DOCTYPE html> 
<html> 
<body> 

<form action="upload.php" method="post" enctype="multipart/form-data"> 
    <input type="file" name="file"> 
    <input type="submit" value="upload"> 
</form> 

</body> 
</html> 

这里是我的PHP

<?php 
if(isset($_FILES['file'])) { 
    $file = $_FILES['file']; 

    $file_name = $file['name']; 
    $file_tmp = $file['tmp_name']; 
    $file_size = $file['size']; 
    $file_error = $file['error']; 

    $file_ext = explode('.', $file_name); 
    $file_ext = strtolower(end($file_ext)); 

    $allowed = array('txt', 'jpg'); 

    if(in_array($file_ext, $allowed)) { 
     if($file_error === 0) { 
      if($file_size <= 1073741824) { 

       $file_name_new = uniqid('', true) . '.' . $file_ext; 
       $file_destination = 'files/' . $file_name_new; 

       if(move_uploaded_file($file_tmp, $file_destination)) { 
        echo $file_destination; 
       } 
      } 
     } 
    } 
} 

它的伟大工程,但只让我上传.txt和.jpg文件。我试图一起删除“$允许”的东西,但打破了剧本。

任何想法?

+0

如果你只是删除'如果(in_array($ file_ext,$允许))',它应该工作 –

回答

0

我想你应该更改代码这样:

if($file_error === 0) { 
     if($file_size <= 1073741824) { 

      $file_name_new = uniqid('', true) . '.' . $file_ext; 
      $file_destination = 'files/' . $file_name_new; 

      if(move_uploaded_file($file_tmp, $file_destination)) { 
       echo $file_destination; 
      } 
     } 
    } 
+0

这工作,谢谢! – Nick

相关问题