我有一个简单的脚本,可以将CSV文件上传到MySQL数据库。它工作正常,但如果用户错过选择某个CSV文件上传并按下UPLOAD按钮,脚本会尝试在数据库中插入数千行。
我想知道如果用户已经选择了FILE,是否有办法检查befoere上传文件。
这是我的代码:
<?php
include("conection.php"); //Connect to Database
if (isset($_POST['submit'])) {
if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
echo "<br><center><p>" . " ". $_FILES['filename']['name'] ." " . "</p></center>";
}
//Import uploaded file to Database
$handle = fopen($_FILES['filename']['tmp_name'], "r");
$count = 0; //skip first line of the CSV file
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
if($count) //skip first line of the CSV file
{
$import="INSERT into PAX (name,surname,group) VALUES('$data[0]','$data[1]','$data[2]')";
mysql_query($import) or die(mysql_error());
}
$count++;
}
fclose($handle);
print "<br><center><p>UPLOADED </p></center> ";
print "<br> ";
print "<center><a href='index.php'><button>HOME</button</a></center> ";
//view upload form
}else {
print "<center><br><p><b>UPLOAD</b> </p>\n";
print "<center><br>Select the file for Upload \n";
print "<form enctype='multipart/form-data' action='upload.php' method='post'>";
print "<center><input size='50' type='file' name='filename'><br />\n";
print "<br>";
print "<input type='submit' name='submit' value='UPLOAD'></form>";
}
?>
答案 0 :(得分:0)
很难用你的代码明确地说出它所呈现的&amp;没有看到原始数据,但我认为只需设置检查$data
的条件就足以防止您看到的问题:
所以这一行:
if ($count) //skip first line of the CSV file {
对此的更改:
if ($count && !empty($data)) //skip first line of the CSV file {
这是您的代码调整和缩进清理以便于阅读。 编辑我已对其进行了编辑,因此现在有一个$SUCCESS_SUBMIT
变量。这样,如果提交成功&amp;您可以true
或false
采取行动。
include("conection.php"); //Connect to Database
$SUCCESS_SUBMIT = false;
if (isset($_POST['submit'])) {
if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
echo "<br><center><p>" . " ". $_FILES['filename']['name'] ." " . "</p></center>";
}
//Import uploaded file to Database
$handle = fopen($_FILES['filename']['tmp_name'], "r");
$count = 0; //skip first line of the CSV file
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
if ($count && !empty($data)) //skip first line of the CSV file {
$import = "INSERT into PAX (name,surname,group) VALUES('$data[0]','$data[1]','$data[2]')";
mysql_query($import) or die(mysql_error());
$SUCCESS_SUBMIT = true;
}
$count++;
}
fclose($handle);
}
if ($SUCCESS_SUBMIT) {
print "<br><center><p>UPLOADED </p></center> ";
print "<br> ";
print "<center><a href='index.php'><button>HOME</button</a></center> ";
}
else {
print "<center><br><p><b>UPLOAD</b> </p>\n";
print "<center><br>Select the file for Upload \n";
print "<form enctype='multipart/form-data' action='upload.php' method='post'>";
print "<center><input size='50' type='file' name='filename'><br />\n";
print "<br>";
print "<input type='submit' name='submit' value='UPLOAD'></form>";
}
答案 1 :(得分:-1)
“有一种方法可以显示用户是否未选择任何csv文件给出消息文件”请选择文件“或类似的东西”
如果没有选择文件,你可以使用javascript来发送消息:
print "<form enctype='multipart/form-data' action='upload.php'
onsubmit='return validateUpload();' name='formUpload'
method='post'>";
print "<center><input size='50' type='file' name='filename'><br />\n";
print "<br>";
print "<input type='submit' name='submit' value='UPLOAD'></form>";
}
?>
<script type="text/javascript" >
function validateUpload()
{
var fName = document.forms["formUpload"]["filename"].value;
if (fName==null || fName=="")
{
alert("Please select file !");
return false;
}
}
</script>