2013-07-10 73 views
2

你好,我有一个PHP脚本,用于允许文件上传。该脚本具有允许的文件类型的数组。如何允许所有文件类型在php中上传?

 $fileTypes = array('jpg', 'jpeg', 'gif', 'png', 'zip', 'xlsx') 

因为形式是一个日志面积为我的客户后面,他请求允许所有类型的文件,而不是被限制在阵列中的文件。尽管我已经向客户解释限制这些文件会更好,但他坚持让它开放。

有没有办法允许数组中的任何文件类型?

完整剧本

 $uploadDir = '/uploads/'; 

     // Set the allowed file extensions 
     $fileTypes = array('jpg', 'jpeg', 'gif', 'png', 'zip', 'xlsx', 'cad', 'pdf', 'doc', 'docx', 'ppt', 'pptx', 'pps', 'ppsx', 'odt', 'xls', 'xlsx', '.mp3', 'm4a', 'ogg', 'wav', 'mp4', 'm4v', 'mov', 'wmv'); // Allowed file extensions 

    $verifyToken = md5('unique_salt' . $_POST['timestamp']); 

    if (!empty($_FILES) && $_POST['token'] == $verifyToken) { 
$tempFile = $_FILES['Filedata']['tmp_name']; 
$uploadDir = $_SERVER['DOCUMENT_ROOT'] . $uploadDir; 
$targetFile = $uploadDir . $_FILES['Filedata']['name']; 

// Validate the filetype 
$fileParts = pathinfo($_FILES['Filedata']['name']); 
if (in_array(strtolower($fileParts['extension']), $fileTypes)) { 

    // Save the file 
    move_uploaded_file($tempFile, $targetFile); 
    echo 1; 

    } else { 

    // The file type wasn't allowed 
    echo 'Invalid file type.'; 

    } 
    } 
+10

是 - 干脆不要做过滤。 – Timothy

+0

你能告诉我们完整的脚本吗? – zzlalani

+0

哦,对不起,我用脚本更新了这个 – gcupat

回答

4

干脆不要做过滤。

<?php 
$uploadDir = '/uploads/'; 

$verifyToken = md5('unique_salt' . $_POST['timestamp']); 

if (!empty($_FILES) && $_POST['token'] == $verifyToken) { 
    $tempFile = $_FILES['Filedata']['tmp_name']; 
    $uploadDir = $_SERVER['DOCUMENT_ROOT'] . $uploadDir; 
    $targetFile = $uploadDir . $_FILES['Filedata']['name']; 

    // Save the file 
    move_uploaded_file($tempFile, $targetFile); 
    echo 1; 
} 
1

只需删除该文件类型的验证和上传所有

//if (in_array(strtolower($fileParts['extension']), $fileTypes)) { 

    // Save the file 
    if(move_uploaded_file($tempFile, $targetFile)){ 
    echo 1; 
    else{ 
    echo 'An error occurred '; 
    } 

// } else { 

    // The file type wasn't allowed 
    // echo 'Invalid file type.'; 

    //} 
1

使用,而下面的代码

<?php 
$uploadDir = '/uploads/'; 

$verifyToken = md5('unique_salt' . $_POST['timestamp']); 

if (!empty($_FILES) && $_POST['token'] == $verifyToken) { 
    $tempFile = $_FILES['Filedata']['tmp_name']; 
    $uploadDir = $_SERVER['DOCUMENT_ROOT'] . $uploadDir; 
    $targetFile = $uploadDir . $_FILES['Filedata']['name']; 

    // Save the file 
    move_uploaded_file($tempFile, $targetFile); 
    echo 1; 

} 

?> 
相关问题