第一步,提交表单:
<!--enctype 提交表单时要使用哪种内容类型-->
<form action="uploadclass2.php" method="post" enctype="multipart/form-data">
歌手姓名:<input type="text" name="name" value="">
歌曲名称:<input type="text" name="musicname" value="">
<label>上传mp3文件</label>
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="提交">
</form>
第二步,把表单提交到uploadclass2.php,代码如下:
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<?php
$audioInfo = array(
'flag' => false,
'code' => 1,
'msg' => '没有接收到数据流',
'data' => array(),
);
//二进制数据流 下面这个会显示全部的参数
//print_r($_FILES);die;
//echo file_get_contents($_FILES['file']['tmp_name']);
//die;
//$data = file_get_contents('php://input') ? file_get_contents('php://input') :gzuncompress($GLOBALS ['HTTP_RAW_POST_DATA']);
//$data = file_get_contents('php://input') ? file_get_contents('php://input') : $_FILES;
$data = file_get_contents($_FILES['file']['tmp_name']);
//$data = base64_decode($data);
//递归创建目录,移动音频到指定位置
$time = time();
$month = date('Ymd', $time);
//$today = date('j', $time);
//获取.最后一个的位置
$ext = "mp3";
$audioName = "xxxxxxxx";
$fileName = time() . $audioName . rand(1, 100000);
$fileName = md5($fileName);
$dirPath = './file/' . $month;
$filePath = $dirPath . '/' . $fileName . '.' . $ext;
if (file_exists($dirPath)) {
} else {
mkdir($dirPath,0777,true);
}
if (!empty($data)) {
//创建并写入数据流,然后保存文件
if (@$fp = fopen($filePath, 'w')) {
fwrite($fp, $data);
fclose($fp);
$size = filesize($filePath);
$audioInfo['data'] = array(
'size' => $size,
'filename' => $fileName,
'ext' => 'mp3',
'dateline' => date('Y-m-d H:i:s', $time),
);
$audioInfo['flag'] = true;
$audioInfo['code'] = 0;
$audioInfo['msg'] = '数据流生成音频成功';
print_r($audioInfo);
}
$audioInfo['code'] = 2;
$audioInfo['msg'] = '数据流写入文件失败';
return $audioInfo;
} else {
//没有接收到数据流
return $audioInfo;
}
//}
?>