Php 仅限制上传到mp3
我一直在编写一个PHP脚本,允许人们将MP3文件上传到服务器:Php 仅限制上传到mp3,php,mp3,media,uploading,Php,Mp3,Media,Uploading,我一直在编写一个PHP脚本,允许人们将MP3文件上传到服务器: $target_dir = "upload/"; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1; $fileType = pathinfo($target_file,PATHINFO_EXTENSION); // Check if image file is a actual image or fake im
$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$fileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$uploadOk = 1;
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 150000000000000000000000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if ($fileType != "audio/mpeg" && $fileType != "audio/mpeg3" && $fileType != "audio/mp3"
&& $fileType != "audio/x-mpeg" && $fileType != "audio/x-mp3" && $fileType != 'audio/x-mpeg3'
&& $fileType != 'audio/x-mpg' && $fileType != "audio/x-mpegaudio" && $fileType != "audio/mpg"
&& $fileType != "audio/x-mpg") {
echo "Invalid Audio Type, please use MP3 Format.";
$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["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
在测试过程中,我已经能够通过禁用限制上传音频文件,所以我知道该程序可以正常工作,但我不断遇到与下面的错误类似的错误。此代码返回:
Notice: Undefined index: file in /uploadsong.php on line 6
Invalid Audio Type, please use MP3 Format.Sorry, your file was not uploaded.
我试过上传不同的mp3文件,但都失败了。有人能提出一个解决这些错误的方法吗?我已经在网上搜索了一个半小时,但运气不好。我也知道安全是一个问题,我计划以后再解决这个问题,但如果有人有任何建议,我将不胜感激。
$fileType=pathinfo($target\u文件,pathinfo\u扩展名)代码>
这是返回“.mp3”或simular
到
$fileType=$\u文件[“fileToUpload”][“type”]
它将返回“audio/mpeg”,这是您在您的条件下检查的内容您的路径信息
,返回的文件扩展名不是您检查的类型请查看$target\u文件的值。我猜是一个非法角色。知道哪条是6号线会有帮助。所以我猜第四行是6。“注意:第6行/uploadsong.php中的未定义索引:file in/uploadsong.php”错误只是一个愚蠢的错误,在代码中找不到,我很抱歉。谢谢你解决了我的问题!