【发布时间】: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