PHP 上传太大会失败?
PHP upload failes if too large?
所以我使用 recorder.js 捕获一些音频,然后使用此代码将音频上传到我们的服务器:
var url = URL.createObjectURL(blob);
var au = document.createElement('audio');
var li = document.createElement('li');
var link = document.createElement('a');
//name of .wav file to use during upload and download (without extendion)
var filename = "test";
//add controls to the <audio> element
au.controls = true;
au.src = url;
//save to disk link
link.href = url;
link.download = filename+".wav"; //download forces the browser to donwload the file using the filename
link.innerHTML = "Save to disk";
//add the new audio element to li
li.appendChild(au);
//add the filename to the li
li.appendChild(document.createTextNode(filename+".wav "))
//add the save to disk link to li
li.appendChild(link);
//upload link
var xhr=new XMLHttpRequest();
xhr.onload=function(e) {
if(this.readyState === 4) {
console.log("Server returned: ",e.target.responseText);
Redirect();
}
};
var fd=new FormData();
fd.append("audio_data",blob, filename);
xhr.open("POST","upload_audio.php",false);
xhr.send(fd);
这是PHP:
<?php
print_r($_FILES); //this will print out the received name, temp name, type, size, etc.
$size = $_FILES['audio_data']['size']; //the size in bytes
$input = $_FILES['audio_data']['tmp_name']; //temporary name that PHP gave to the uploaded file
$output = $_FILES['audio_data']['name'].".wav"; //letting the client control the filename is a rather bad idea
//move the file from temp name to local folder using $output name
move_uploaded_file($input, "../data/".$output)
?>
如果您录制时长不超过 30 秒,此脚本将完美运行。如果录音变大,我们会从服务器收到此响应:
Server returned:
Array
(
[audio_data] => Array
(
[name] => QA_3_Issue,html,7fac7e13c55a4eb18b0f2160f8581097_7fac7e13c55a4eb18b0f2160f8581097
[type] =>
[tmp_name] =>
[error] => 1
[size] => 0
)
)
有人知道发生了什么事吗?可能是服务器问题?
我也不太明白为什么它说错误,但没有说明错误到底是什么。
此致,
丹尼尔
您应该在 .htaccess 文件或 php.ini 文件中设置更大的上传文件大小,例如
upload_max_filesize = 100M。
希望对您有所帮助。
所以我使用 recorder.js 捕获一些音频,然后使用此代码将音频上传到我们的服务器:
var url = URL.createObjectURL(blob);
var au = document.createElement('audio');
var li = document.createElement('li');
var link = document.createElement('a');
//name of .wav file to use during upload and download (without extendion)
var filename = "test";
//add controls to the <audio> element
au.controls = true;
au.src = url;
//save to disk link
link.href = url;
link.download = filename+".wav"; //download forces the browser to donwload the file using the filename
link.innerHTML = "Save to disk";
//add the new audio element to li
li.appendChild(au);
//add the filename to the li
li.appendChild(document.createTextNode(filename+".wav "))
//add the save to disk link to li
li.appendChild(link);
//upload link
var xhr=new XMLHttpRequest();
xhr.onload=function(e) {
if(this.readyState === 4) {
console.log("Server returned: ",e.target.responseText);
Redirect();
}
};
var fd=new FormData();
fd.append("audio_data",blob, filename);
xhr.open("POST","upload_audio.php",false);
xhr.send(fd);
这是PHP:
<?php
print_r($_FILES); //this will print out the received name, temp name, type, size, etc.
$size = $_FILES['audio_data']['size']; //the size in bytes
$input = $_FILES['audio_data']['tmp_name']; //temporary name that PHP gave to the uploaded file
$output = $_FILES['audio_data']['name'].".wav"; //letting the client control the filename is a rather bad idea
//move the file from temp name to local folder using $output name
move_uploaded_file($input, "../data/".$output)
?>
如果您录制时长不超过 30 秒,此脚本将完美运行。如果录音变大,我们会从服务器收到此响应:
Server returned:
Array
(
[audio_data] => Array
(
[name] => QA_3_Issue,html,7fac7e13c55a4eb18b0f2160f8581097_7fac7e13c55a4eb18b0f2160f8581097
[type] =>
[tmp_name] =>
[error] => 1
[size] => 0
)
)
有人知道发生了什么事吗?可能是服务器问题? 我也不太明白为什么它说错误,但没有说明错误到底是什么。
此致, 丹尼尔
您应该在 .htaccess 文件或 php.ini 文件中设置更大的上传文件大小,例如 upload_max_filesize = 100M。 希望对您有所帮助。