【问题标题】:Cannot upload audio file but images works fine无法上传音频文件,但图像工作正常
【发布时间】:2016-12-08 16:55:12
【问题描述】:

我正在尝试设置一个简单的文件上传,使用 w3schools 文件上传来上传音频

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["file"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["file"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" && $imageFileType != "mp3" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

<form class="" action="<?php echo WEB_ROOT ?>components/includes/_int/su.php" method="POST" enctype="multipart/form-data">
        <div class="row">
            <div class="col-lg-4">
                <div class="form-group">
                    <label for="sTitle">Song Title</label>
                    <input class="form-control" name="sTitle" type="text" id="sTitle">
                </div>
            </div>
            <div class="col-lg-4">
                <div class="form-group">
                    <label for="sGenre">Genre</label>
                    <select id="sGenre" name="sGenre" class="selectpicker form-control">
                        <?php foreach (genre::GetGenresT() as $genre) : ?>
                            <option value="<?php $genre->PrintID(); ?>"><?php $genre->PrintName(); ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
            <div class="col-lg-4">
                <div class="form-group">
                    <label for="sArtist">Artist</label>
                    <select id="sArtist" name="sArtist" class="selectpicker form-control">
                        <?php foreach (artist::GetArtists() as $artist) : ?>
                            <option value="<?php $artist->PrintID(); ?>"><?php $artist->PrintName(); ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
            <div class="col-lg-12">
                <div class="form-group">
                    <label for="sLyrics">Lyric</label>
                    <textarea class="form-control">sadsadas</textarea>
                </div>
            </div>
            <div class="col-lg-12">
                <div class="form-group">
                    <input type="file" class="form-control" name="file" id="file">
                </div>
            </div>
            <div class="col-lg-4">
                <div class="form-group">
                    <a href="#" class="btn btn-warning"><i class="fa fa-arrow-left"></i> Cancel</a>
                    <button name="btn-submit" class="btn btn-success" type="submit">SUBMIT</button>
                </div>
            </div>
        </div>
    </form>

正如您将在 PHP 代码中看到的,我添加了一个“&& $imageFileType!=”mp3”,我得到的唯一错误是“抱歉,上传您的文件时出错。”错误。 在不需要上传其他错误之前,它似乎一切正常。

它可以很好地上传图像,但它不会上传 mp3 文件。我试图找到其他例子,但一切都会导致同样的问题。如果有人可以解释为什么会这样做,那就太好了!谢谢!

【问题讨论】:

    标签: php html file-upload


    【解决方案1】:

    您只是在初始检查中检查图像文件类型,这就是您的音频失败的原因。 mp3 的 MIME 类型不同,它是“audio/mpeg”,而不是 jpg/png/gif,后者是图像 MIME 类型检查的内容。理想情况下,您应该检查 php 中的 MIME 类型数组(并从那里开始)。php website 上的用户注释中有一个很好的示例

    发布的例子是

    if(!function_exists('mime_content_type')) {
    
        function mime_content_type($filename) {
    
            $mime_types = array(
    
                'txt' => 'text/plain',
                'htm' => 'text/html',
                'html' => 'text/html',
                'php' => 'text/html',
                'css' => 'text/css',
                'js' => 'application/javascript',
                'json' => 'application/json',
                'xml' => 'application/xml',
                'swf' => 'application/x-shockwave-flash',
                'flv' => 'video/x-flv',
    
                // images
                'png' => 'image/png',
                'jpe' => 'image/jpeg',
                'jpeg' => 'image/jpeg',
                'jpg' => 'image/jpeg',
                'gif' => 'image/gif',
                'bmp' => 'image/bmp',
                'ico' => 'image/vnd.microsoft.icon',
                'tiff' => 'image/tiff',
                'tif' => 'image/tiff',
                'svg' => 'image/svg+xml',
                'svgz' => 'image/svg+xml',
    
                // archives
                'zip' => 'application/zip',
                'rar' => 'application/x-rar-compressed',
                'exe' => 'application/x-msdownload',
                'msi' => 'application/x-msdownload',
                'cab' => 'application/vnd.ms-cab-compressed',
    
                // audio/video
                'mp3' => 'audio/mpeg',
                'qt' => 'video/quicktime',
                'mov' => 'video/quicktime',
    
                // adobe
                'pdf' => 'application/pdf',
                'psd' => 'image/vnd.adobe.photoshop',
                'ai' => 'application/postscript',
                'eps' => 'application/postscript',
                'ps' => 'application/postscript',
    
                // ms office
                'doc' => 'application/msword',
                'rtf' => 'application/rtf',
                'xls' => 'application/vnd.ms-excel',
                'ppt' => 'application/vnd.ms-powerpoint',
    
                // open office
                'odt' => 'application/vnd.oasis.opendocument.text',
                'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
            );
    
            $ext = strtolower(array_pop(explode('.',$filename)));
            if (array_key_exists($ext, $mime_types)) {
                return $mime_types[$ext];
            }
            elseif (function_exists('finfo_open')) {
                $finfo = finfo_open(FILEINFO_MIME);
                $mimetype = finfo_file($finfo, $filename);
                finfo_close($finfo);
                return $mimetype;
            }
            else {
                return 'application/octet-stream';
            }
        }
    }
    ?>
    

    在你的情况下有点啰嗦!您可以将其显着缩短为

    if(!function_exists('mime_content_type')) {
    
        function mime_content_type($filename) {
    
            $mime_types = array(
    
                // images
                'png' => 'image/png',
                'jpe' => 'image/jpeg',
                'jpeg' => 'image/jpeg',
                'jpg' => 'image/jpeg',
                'gif' => 'image/gif',
                'bmp' => 'image/bmp',
                'ico' => 'image/vnd.microsoft.icon',
                'tiff' => 'image/tiff',
                'tif' => 'image/tiff',
                'svg' => 'image/svg+xml',
                'svgz' => 'image/svg+xml',
    
                // audio/video
                'mp3' => 'audio/mpeg',
                'qt' => 'video/quicktime',
                'mov' => 'video/quicktime',
    
    
            );
    
            $ext = strtolower(array_pop(explode('.',$filename)));
            if (array_key_exists($ext, $mime_types)) {
                return $mime_types[$ext];
            }
            elseif (function_exists('finfo_open')) {
                $finfo = finfo_open(FILEINFO_MIME);
                $mimetype = finfo_file($finfo, $filename);
                finfo_close($finfo);
                return $mimetype;
            }
        }
    }
    ?>
    

    即省略你不想要的类型! :)

    希望对你有帮助

    编辑:当然,不是检查图像大小,(getimagesize)你可以只检查文件大小,然后用this之类的东西检查扩展名

    <?php
    $finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
    foreach (glob("*") as $filename) {
        echo finfo_file($finfo, $filename) . "\n";
    }
    finfo_close($finfo);
    ?>
    

    更方便!

    【讨论】:

    • 啊,我明白了!非常感谢:)
    猜你喜欢
    • 2016-08-16
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多