我正在尝试使用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代码中看到我添加了“&amp;&amp; $ imageFileType!=”mp3“,我得到的唯一错误是”抱歉,上传文件时出错。“错误。 它似乎正在经历一切正常,直到它不需要上传任何其他错误。
它上传的图片很好但是mp3文件它不会这样做。我试图找到其他例子,但导致同样问题的一切。如果有人能解释为什么这样做会很棒!谢谢!
答案 0 :(得分:0)
您只是在初始检查中检查图像文件类型,这就是您的音频失败的原因。 MIME类型对于mp3是不同的,它是'audio / mpeg'而不是jpg / png / gif,这是图像MIME类型检查的内容。理想情况下,您应该在php中检查一系列MIME类型(并从那里开始)..在php website上的用户注释中有一个很好的例子
发布的示例为
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;
}
}
}
?>
你的情况有点长!你可以把它缩短到
<?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);
?>
即。省略你不想要的类型! :)
希望这有帮助
编辑:当然,您可以只检查文件大小而不是检查图像大小,然后检查扩展名,而不是this
{{1}}
更方便!