[英]Unable to upload a chunked file using PHP
I have a chunk upload for a file using PHP and JS.我有一个使用 PHP 和 JS 的文件块上传。 The chunk process is happening,
块过程正在发生,
However, the file is not getting uploaded to the directory.但是,文件没有上传到目录。
My Code goes as:我的代码如下:
Index.php索引.php
<HTML>
<head>
<script src="plupload/js/plupload.full.min.js"></script>
<script>
window.addEventListener("load", function () {
var path = "plupload/js/`";
var uploader = new plupload.Uploader({
browse_button: 'pickfiles',
container: document.getElementById('container'),
url: 'testupload.php',
chunk_size: '1000kb',
max_retries: 2,
filters: {
max_file_size: '30mb',
mime_types: [{title: "Video", extensions: "mp4,3gp,mov"}]
},
init: {
PostInit: function () {
document.getElementById('filelist').innerHTML = '';
},
FilesAdded: function (up, files) {
plupload.each(files, function (file) {
document.getElementById('filelist').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div>';
});
uploader.start();
},
UploadProgress: function (up, file) {
document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
},
Error: function (up, err) {
// DO YOUR ERROR HANDLING!
console.log(err);
}
}
});
uploader.init();
});
</script>
</head>
<body>
<form action="insert.php" method="post" >
<div id="container">
<span id="pickfiles" style="font-weight:600;">Click <span> Here </span> to Upload Full Video</span>
</div>
<div id="filelist" style="color:red">Your browser doesn't support HTML5 upload.</div>
<span class="pure-form-message"> * required</span>
<input type="text" name="inp1" />
<input type="text" name="inp2" />
<button type="submit" >Submit</button>
</form>
testupload.php测试上传文件
<?php
// RESPONSE FUNCTION
function verbose($ok=1,$info=""){
// THROW A 400 ERROR ON FAILURE
if ($ok==0) { http_response_code(400); }
die(json_encode(["ok"=>$ok, "info"=>$info]));
}
// INVALID UPLOAD
if (empty($_FILES) || $_FILES['file']['error']) {
verbose(0, "Failed to move uploaded file.");
}
// THE UPLOAD DESITINATION - CHANGE THIS TO YOUR OWN
$filePath = __DIR__ . DIRECTORY_SEPARATOR . "uploads";
if (!file_exists($filePath)) {
if (!mkdir($filePath, 0777, true)) {
verbose(0, "Failed to create $filePath");
}
}
$fileName = isset($_REQUEST["name"]) ? $_REQUEST["name"] : $_FILES["file"]["name"];
$filePath = $filePath . DIRECTORY_SEPARATOR . $fileName;
// DEAL WITH CHUNKS
$chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
$chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0;
$out = @fopen("{$filePath}.part", $chunk == 0 ? "wb" : "ab");
if ($out) {
$in = @fopen($_FILES['file']['tmp_name'], "rb");
if ($in) {
while ($buff = fread($in, 4096)) { fwrite($out, $buff); }
} else {
verbose(0, "Failed to open input stream");
}
@fclose($in);
@fclose($out);
@unlink($_FILES['file']['tmp_name']);
} else {
verbose(0, "Failed to open output stream");
}
// CHECK IF FILE HAS BEEN UPLOADED
if (!$chunks || $chunk == $chunks - 1) {
rename("{$filePath}.part", $filePath);
}
verbose(1, "Upload OK");
?>
insert.php插入.php
$inp1 = $_POST['inp1'];
$inp2 = $_POST['inp2'];
$file2= isset($_REQUEST["name"]) ? $_REQUEST["name"] : $_FILES["file"]["name"];
$sql2 = "INSERT INTO table (inp1, inp2, file)
VALUES ('$inp1', '$inp2', '$file2')";
if ($conn->query($sql2) === TRUE) { } else { }
Please note that :请注意:
Any help is greatly appreciated..任何帮助是极大的赞赏..
first of all, change your js script of your plugin首先,更改插件的js脚本
index.html索引.html
<script src="https://cdnjs.cloudflare.com/ajax/libs/plupload/3.1.2/plupload.full.min.js" integrity="sha512-+F2bTlYeSZrUs0uNo8OsYlE4tbdEoGp3Hhur4V+4o4nmsljKKDgCexLpqc5mERevdiOqthT4SVL+SHhz4lyfcA==" crossorigin="anonymous"></script>
and below code to your js code和下面的代码到你的js代码
window.addEventListener("load", function () {
var path = "plupload/js/`";
var fileInputBox = document.querySelector("#videoTitle");
var uploader = new plupload.Uploader({
browse_button: 'pickfiles',
container: document.getElementById('container'),
url: 'testupload.php',
chunk_size: '1000kb',
max_retries: 2,
filters: {
max_file_size: '30mb',
mime_types: [{title: "Video", extensions: "mp4,3gp,mov"}]
},
init: {
PostInit: function () {
document.getElementById('filelist').innerHTML = '';
},
FilesAdded: function (up, files) {
plupload.each(files, function (file) {
document.getElementById('filelist').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div>';
fileInputBox.value = file.name;
});
uploader.start();
},
UploadProgress: function (up, file) {
document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
},
Error: function (up, err) {
// DO YOUR ERROR HANDLING!
console.log(err);
}
}
});
uploader.init();
});
and you want to add the video title to your PHP variable then you can add some js changes并且您想将视频标题添加到您的 PHP 变量中,然后您可以添加一些 js 更改
testupload.php测试上传文件
// RESPONSE FUNCTION
function verbose($ok=1,$info=""){
// THROW A 400 ERROR ON FAILURE
if ($ok==0) { http_response_code(400); }
die(json_encode(["ok"=>$ok, "info"=>$info]));
}
// INVALID UPLOAD
if (empty($_FILES) || $_FILES['file']['error']) {
verbose(0, "Failed to move uploaded file.");
}
// THE UPLOAD DESITINATION - CHANGE THIS TO YOUR OWN
$filePath = __DIR__ . DIRECTORY_SEPARATOR . "uploads";
if (!file_exists($filePath)) {
if (!mkdir($filePath, 0777, true)) {
verbose(0, "Failed to create $filePath");
}
}
$fileName = isset($_REQUEST["name"]) ? $_REQUEST["name"] : $_FILES["file"]["name"];
$filePath = $filePath . DIRECTORY_SEPARATOR . $fileName;
// DEAL WITH CHUNKS
$chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
$chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0;
$out = @fopen("{$filePath}.part", $chunk == 0 ? "wb" : "ab");
if ($out) {
$in = @fopen($_FILES['file']['tmp_name'], "rb");
if ($in) {
while ($buff = fread($in, 4096)) { fwrite($out, $buff); }
} else {
verbose(0, "Failed to open input stream");
}
@fclose($in);
@fclose($out);
@unlink($_FILES['file']['tmp_name']);
} else {
verbose(0, "Failed to open output stream");
}
// CHECK IF FILE HAS BEEN UPLOADED
if (!$chunks || $chunk == $chunks - 1) {
rename("{$filePath}.part", $filePath);
}
verbose(1, "Upload OK");
now you can access the video title from insert.php现在您可以从 insert.php 访问视频标题
insert.php插入.php
$inp1 = $_POST['inp1'];
$inp2 = $_POST['inp2'];
$file2= isset($_REQUEST["videoTitle"]) ? $_REQUEST["videoTitle"] : $_FILES["file"]["name"];
return print_r([
$inp1,
$inp2,
$file2,
]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.