[英]How Can I Upload Audio(mp3,ogg,flac) By Using PHP?
我被困在這里
我以前這樣做過,並通過使用此 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>
嘗試使用
$path = "../test/cover/"; //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($name2))
{
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";
}
}
}
但請確保您將使用的表單正確支持此代碼 asper 包括以下內容
enctype="multipart/form-data" method="post"
檢查 php.ini 文件並確保 upload_max_filesize 足夠。 如果不是,請增加 upload_max_filesize 值。 然后試試下面的代碼
if(isset($_POST['submit']))
{
$path = "uploads/"; //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'];
$fileInfo=pathinfo($file1);
$ext=$fileInfo['extension'];
if(in_array($ext,$valid_formats1))
{
$actual_image_name = uniqid().".".$ext;
$tmp = $_FILES['file1']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
//success upload
}
else
echo "failed";
}else{
echo "File Support Not Support";
}
}
}
這段代碼已經過測試,所以我認為它可以正常工作:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.