我想上传多个文件,而不是输入他们的标题,而不是想通过点击提交存储在数据库中
I Want To Upload Multiple Files and than enter their Title with particular and than want to store in database by clicking on submit
需要帮助使用动态添加的多个文本输入框创建用于多次上传的表单
上传文件到服务器
if(isset($_POST['submit']))
{
$count=count($_FILES['files']['name']);
for($i=0;$i<$count;$i++)
{
$allowed = array("mp3" => "audio/mp3","wav" => "audio/wav");
$cat_type = $_POST['cat_type'];
$title = $_POST['title'][$i];
$filename= $_FILES['files']['name'][$i];
$tempfilename= $_FILES['files']['tmp_name'][$i];
$filetype=$_FILES['files']['type'][$i];
$filesize = $_FILES['files']['size'][$i];
// Verify file extension
$video_ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!array_key_exists($video_ext, $allowed)) die("Error: Please select a valid file format.");
// Verify file size - 5MB maximum
$maxsize = 5 * 1024 * 1024;
if($filesize > $maxsize) die("Error: File size is larger than the allowed limit.");
// Verify MYME type of the file
if(in_array($filetype, $allowed))
{
// Check whether file exists before uploading it
if(file_exists($ringtonelocation . $filename))
{
echo $filename . " is already exists.";
}
else
{
//uploading file and storing it to database as well
try
{
$cat_type = $_POST['cat_type'];
$cat_alias = strtolower(preg_replace('/\s+/', '', $cat_type));
$highest_id += 1;
$uploaddate= date("d-m-Y_h-i-sa");
// Rename file
$newvideofilename = strtolower(str_replace(" ", "_",$title)). "_" .$highest_id. "_" .$uploaddate .".".$video_ext;
move_uploaded_file($_FILES['files']['tmp_name'][$i],$videolocation.$newvideofilename);
// $filepath="upload/".$filename;
// echo "<br/> Category:-".$cat_type;
// echo "<br/> Title:-".$title;
// echo "<br/> File Name:-".$filename;
// echo "<br/> Record Insert successfully";
// echo "<br/>";
// $sql = "INSERT INTO video_master (id, name , url) VALUES (NULL,'$title','$filepath')";
$sql = "INSERT INTO ringtone_master
(id, name ,cat_type, cat_alias, url, downloads, views, fav_counter, likes, dislikes)
VALUES
(NULL,'$title','$cat_type','$cat_alias','$newvideofilename',0,0,0,0,0)";
if(mysqli_query($con,$sql))
{
echo "<br/> Record Insert successfully";
}
else
{
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
}
catch(Exception $e)
{
$response['error'] = true;
$response['message'] = 'Could not upload file1....';
}
}
}
}
}
在浏览器中临时存储文件
<script>
var selDiv = "";
var storedFiles = [];
$(document).ready(function() {
$("#files").on("change", handleFileSelect);
selDiv = $("#selectedFiles");
$("#myForm").on("submit", handleForm);
$("body").on("click", ".selFile", removeFile);
});
function handleFileSelect(e) {
var files = e.target.files;
var filesArr = Array.prototype.slice.call(files);
filesArr.forEach(function(f) {
if(!f.type.match("audio.mp3")) {
return;
}
storedFiles.push(f);
var reader = new FileReader();
reader.onload = function (e) {
// var html = "<div><img src=\"" + e.target.result + "\" data-file='"+f.name+"' class='selFile' title='Click to remove'>" + f.name + "<br clear=\"left\"/></div>";
var html = "<div><input type='text' name='title[] required'>"+ f.name + "</div>";
selDiv.append(html);
}
reader.readAsDataURL(f);
});
}
</script>
Html portion
<br><br>
<label for="fileSelect">Select Video to Upload:</label>
<input type="file" id="files" name="files[]" multiple>
<br><br>
<div id="selectedFiles"></div>
<input type="submit" name="submit" value="Upload">
当我选择多个文件时,它会根据上传的文件生成输入字段的数量,但问题是输入字段和文件的顺序不同
所以请帮忙解决这个问题谢谢
好吧退后一步,我注意到我没有提到很多事情,为了更正它,我使用了与我建议的不同的实现,以下对我有用。
将您的 handleFileSelect 更改为以下内容:
function handleFileSelect(e) {
var files = e.target.files;
var filesArr = Array.prototype.slice.call(files);
filesArr.forEach(function(f,index) {
if(!f.type.match("audio.mp3")) {
return;
}
storedFiles.push(f);
var reader = new FileReader();
reader.onload = function (e) {
// var html = "<div><img src=\"" + e.target.result + "\" data-file='"+f.name+"' class='selFile' title='Click to remove'>" + f.name + "<br clear=\"left\"/></div>";
var html = "<div><input type='text' name='title["+index+"] required'>"+ f.name + "</div>";
selDiv.append(html);
}
reader.readAsDataURL(f);
});
}
然后在您的 php 代码中,您可以切换回执行以下操作:
if(isset($_POST['submit']))
{
$count=count($_FILES['files']['name']);
for($i=0;$i<$count;$i++)
{
$allowed = array("mp3" => "audio/mp3","wav" => "audio/wav");
$cat_type = $_POST['cat_type'];
$title = $_POST['title'][$i];
$filename= $_FILES['files']['name'][$i];
$tempfilename= $_FILES['files']['tmp_name'][$i];
$filetype=$_FILES['files']['type'][$i];
$filesize = $_FILES['files']['size'][$i];
// Verify file extension
$video_ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!array_key_exists($video_ext, $allowed)) die("Error: Please select a valid file format.");
// Verify file size - 5MB maximum
$maxsize = 5 * 1024 * 1024;
if($filesize > $maxsize) die("Error: File size is larger than the allowed limit.");
// Verify MYME type of the file
if(in_array($filetype, $allowed))
{
// Check whether file exists before uploading it
if(file_exists($ringtonelocation . $filename))
{
echo $filename . " is already exists.";
}
else
{
//uploading file and storing it to database as well
try
{
$cat_type = $_POST['cat_type'];
$cat_alias = strtolower(preg_replace('/\s+/', '', $cat_type));
$highest_id += 1;
$uploaddate= date("d-m-Y_h-i-sa");
// Rename file
$newvideofilename = strtolower(str_replace(" ", "_",$title)). "_" .$highest_id. "_" .$uploaddate .".".$video_ext;
move_uploaded_file($_FILES['files']['tmp_name'][$i],$videolocation.$newvideofilename);
// $filepath="upload/".$filename;
// echo "<br/> Category:-".$cat_type;
// echo "<br/> Title:-".$title;
// echo "<br/> File Name:-".$filename;
// echo "<br/> Record Insert successfully";
// echo "<br/>";
// $sql = "INSERT INTO video_master (id, name , url) VALUES (NULL,'$title','$filepath')";
$sql = "INSERT INTO ringtone_master
(id, name ,cat_type, cat_alias, url, downloads, views, fav_counter, likes, dislikes)
VALUES
(NULL,'$title','$cat_type','$cat_alias','$newvideofilename',0,0,0,0,0)";
if(mysqli_query($con,$sql))
{
echo "<br/> Record Insert successfully";
}
else
{
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
}
catch(Exception $e)
{
$response['error'] = true;
$response['message'] = 'Could not upload file1....';
}
}
}
}
}
只要您不以其他方式更改文件的顺序,脚本中为 foreach 添加的索引将为每个标题提供与文件在其文件数组中相同的索引,这意味着它们将遵循 each-others 索引。
这意味着在 php 端他们也会遵循 each-other- 上面的解决方案对我有效,我怀疑如果它对你无效,你有一些改变需要索引和进一步调查的顺序。
需要帮助使用动态添加的多个文本输入框创建用于多次上传的表单
上传文件到服务器
if(isset($_POST['submit']))
{
$count=count($_FILES['files']['name']);
for($i=0;$i<$count;$i++)
{
$allowed = array("mp3" => "audio/mp3","wav" => "audio/wav");
$cat_type = $_POST['cat_type'];
$title = $_POST['title'][$i];
$filename= $_FILES['files']['name'][$i];
$tempfilename= $_FILES['files']['tmp_name'][$i];
$filetype=$_FILES['files']['type'][$i];
$filesize = $_FILES['files']['size'][$i];
// Verify file extension
$video_ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!array_key_exists($video_ext, $allowed)) die("Error: Please select a valid file format.");
// Verify file size - 5MB maximum
$maxsize = 5 * 1024 * 1024;
if($filesize > $maxsize) die("Error: File size is larger than the allowed limit.");
// Verify MYME type of the file
if(in_array($filetype, $allowed))
{
// Check whether file exists before uploading it
if(file_exists($ringtonelocation . $filename))
{
echo $filename . " is already exists.";
}
else
{
//uploading file and storing it to database as well
try
{
$cat_type = $_POST['cat_type'];
$cat_alias = strtolower(preg_replace('/\s+/', '', $cat_type));
$highest_id += 1;
$uploaddate= date("d-m-Y_h-i-sa");
// Rename file
$newvideofilename = strtolower(str_replace(" ", "_",$title)). "_" .$highest_id. "_" .$uploaddate .".".$video_ext;
move_uploaded_file($_FILES['files']['tmp_name'][$i],$videolocation.$newvideofilename);
// $filepath="upload/".$filename;
// echo "<br/> Category:-".$cat_type;
// echo "<br/> Title:-".$title;
// echo "<br/> File Name:-".$filename;
// echo "<br/> Record Insert successfully";
// echo "<br/>";
// $sql = "INSERT INTO video_master (id, name , url) VALUES (NULL,'$title','$filepath')";
$sql = "INSERT INTO ringtone_master
(id, name ,cat_type, cat_alias, url, downloads, views, fav_counter, likes, dislikes)
VALUES
(NULL,'$title','$cat_type','$cat_alias','$newvideofilename',0,0,0,0,0)";
if(mysqli_query($con,$sql))
{
echo "<br/> Record Insert successfully";
}
else
{
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
}
catch(Exception $e)
{
$response['error'] = true;
$response['message'] = 'Could not upload file1....';
}
}
}
}
}
在浏览器中临时存储文件
<script>
var selDiv = "";
var storedFiles = [];
$(document).ready(function() {
$("#files").on("change", handleFileSelect);
selDiv = $("#selectedFiles");
$("#myForm").on("submit", handleForm);
$("body").on("click", ".selFile", removeFile);
});
function handleFileSelect(e) {
var files = e.target.files;
var filesArr = Array.prototype.slice.call(files);
filesArr.forEach(function(f) {
if(!f.type.match("audio.mp3")) {
return;
}
storedFiles.push(f);
var reader = new FileReader();
reader.onload = function (e) {
// var html = "<div><img src=\"" + e.target.result + "\" data-file='"+f.name+"' class='selFile' title='Click to remove'>" + f.name + "<br clear=\"left\"/></div>";
var html = "<div><input type='text' name='title[] required'>"+ f.name + "</div>";
selDiv.append(html);
}
reader.readAsDataURL(f);
});
}
</script>
Html portion
<br><br>
<label for="fileSelect">Select Video to Upload:</label>
<input type="file" id="files" name="files[]" multiple>
<br><br>
<div id="selectedFiles"></div>
<input type="submit" name="submit" value="Upload">
当我选择多个文件时,它会根据上传的文件生成输入字段的数量,但问题是输入字段和文件的顺序不同
所以请帮忙解决这个问题谢谢
好吧退后一步,我注意到我没有提到很多事情,为了更正它,我使用了与我建议的不同的实现,以下对我有用。
将您的 handleFileSelect 更改为以下内容:
function handleFileSelect(e) {
var files = e.target.files;
var filesArr = Array.prototype.slice.call(files);
filesArr.forEach(function(f,index) {
if(!f.type.match("audio.mp3")) {
return;
}
storedFiles.push(f);
var reader = new FileReader();
reader.onload = function (e) {
// var html = "<div><img src=\"" + e.target.result + "\" data-file='"+f.name+"' class='selFile' title='Click to remove'>" + f.name + "<br clear=\"left\"/></div>";
var html = "<div><input type='text' name='title["+index+"] required'>"+ f.name + "</div>";
selDiv.append(html);
}
reader.readAsDataURL(f);
});
}
然后在您的 php 代码中,您可以切换回执行以下操作:
if(isset($_POST['submit']))
{
$count=count($_FILES['files']['name']);
for($i=0;$i<$count;$i++)
{
$allowed = array("mp3" => "audio/mp3","wav" => "audio/wav");
$cat_type = $_POST['cat_type'];
$title = $_POST['title'][$i];
$filename= $_FILES['files']['name'][$i];
$tempfilename= $_FILES['files']['tmp_name'][$i];
$filetype=$_FILES['files']['type'][$i];
$filesize = $_FILES['files']['size'][$i];
// Verify file extension
$video_ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!array_key_exists($video_ext, $allowed)) die("Error: Please select a valid file format.");
// Verify file size - 5MB maximum
$maxsize = 5 * 1024 * 1024;
if($filesize > $maxsize) die("Error: File size is larger than the allowed limit.");
// Verify MYME type of the file
if(in_array($filetype, $allowed))
{
// Check whether file exists before uploading it
if(file_exists($ringtonelocation . $filename))
{
echo $filename . " is already exists.";
}
else
{
//uploading file and storing it to database as well
try
{
$cat_type = $_POST['cat_type'];
$cat_alias = strtolower(preg_replace('/\s+/', '', $cat_type));
$highest_id += 1;
$uploaddate= date("d-m-Y_h-i-sa");
// Rename file
$newvideofilename = strtolower(str_replace(" ", "_",$title)). "_" .$highest_id. "_" .$uploaddate .".".$video_ext;
move_uploaded_file($_FILES['files']['tmp_name'][$i],$videolocation.$newvideofilename);
// $filepath="upload/".$filename;
// echo "<br/> Category:-".$cat_type;
// echo "<br/> Title:-".$title;
// echo "<br/> File Name:-".$filename;
// echo "<br/> Record Insert successfully";
// echo "<br/>";
// $sql = "INSERT INTO video_master (id, name , url) VALUES (NULL,'$title','$filepath')";
$sql = "INSERT INTO ringtone_master
(id, name ,cat_type, cat_alias, url, downloads, views, fav_counter, likes, dislikes)
VALUES
(NULL,'$title','$cat_type','$cat_alias','$newvideofilename',0,0,0,0,0)";
if(mysqli_query($con,$sql))
{
echo "<br/> Record Insert successfully";
}
else
{
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
}
catch(Exception $e)
{
$response['error'] = true;
$response['message'] = 'Could not upload file1....';
}
}
}
}
}
只要您不以其他方式更改文件的顺序,脚本中为 foreach 添加的索引将为每个标题提供与文件在其文件数组中相同的索引,这意味着它们将遵循 each-others 索引。
这意味着在 php 端他们也会遵循 each-other- 上面的解决方案对我有效,我怀疑如果它对你无效,你有一些改变需要索引和进一步调查的顺序。