我被困在这里
我以前这样做过,并通过使用此 PHP 代码上传图像,但是当我尝试将其更改为上传音频文件时,它无法上传?
我的 PHP 代码:(上传图片[作品]):
<?php
$allowed = array('png', 'jpg', 'jpeg', 'gif', 'swf');
if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0){
$extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);
if(!in_array(strtolower($extension), $allowed)){
echo '{"status":"error"}';
exit;
}
if(move_uploaded_file($_FILES['upl']['tmp_name'], 'arts/'.$_FILES['upl']['name'])){
echo '{"status":"success"}';
exit;
}
}
echo '{"status":"error"}';
exit;
}
PHP 代码:(上传音频[不工作]):
<?php
$allowed = array('mp3', 'ogg', 'flac');
if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0){
$extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);
if(!in_array(strtolower($extension), $allowed)){
echo '{"status":"error"}';
exit;
}
if(move_uploaded_file($_FILES['upl']['tmp_name'], 'upload/'.$_FILES['upl']['name'])){
echo '{"status":"success"}';
exit;
}
}
echo '{"status":"error"}';
exit;
}
?>
最佳答案
使用此代码确保您的服务器上有该文件夹
<?php
if(isset($_POST['submit']))
{
$path = "test/music/"; //file to place within the server
$valid_formats1 = array("mp3", "ogg", "flac"); //list of file extention to be accepted
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$file1 = $_FILES['file1']['name']; //input file name in this code is file1
$size = $_FILES['file1']['size'];
if(strlen($file1))
{
list($txt, $ext) = explode(".", $file1);
if(in_array($ext,$valid_formats1))
{
$actual_image_name = $txt.".".$ext;
$tmp = $_FILES['file1']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
//success upload
}
else
echo "failed";
}
}
}
}
?>
<form enctype="multipart/form-data" id="form1" method="post" action="text1.php">
<input type="file" name="file1" accept=".ogg,.flac,.mp3" required="required"/>
<input type="submit" name="submit"/>
</form>
关于php - 如何使用 PHP 上传音频(mp3、ogg、flac)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34429317/