我制作了一个管理页面来将csv文件上传到mysql。文件 csv 的大小为 59,9mb。但是我的脚本没有将文件上传到 mysql。你介意帮我解决这个问题吗?这是我的代码:
<HTML>
<HEAD>
<TITLE> Admin Update </TITLE>
</HEAD>
<BODY>
<form enctype='multipart/form-data' action="<?php echo $PHP_SELF ?>" method='post'>
<font face=arial size=2>Type file name to import:</font><br>
<input type='file' name='filename' size='20'><br>
<input type='submit' name='submit' value='submit'></form>
<?php
include("phpsqlajax_dbinfo.php");
$connection = mysql_connect ('127.0.0.1', $username, $password);
if (!$connection) { die('Not connected : ' . mysql_error());}
$db_selected = mysql_select_db("ipcoba", $connection);
if (!$db_selected) {
die ('Can''t use db : ' . mysql_error());
}
if(isset($_POST['submit']))
{
// address to copy csv file
$target_path = "C:/xampp/htdocs/bikinwebtracert";
$target_path = $target_path . basename( $_FILES['filename']['name']);
if(move_uploaded_file($_FILES['filename']['tmp_name'], $target_path)) {
echo "<font face=arial size=2>The file ". basename( $_FILES['filename']['name']). " Upload Success</font><br>";
} else{
echo "<font face=arial size=2>Failed to Upload, Please try again</font><br>";
}
$filename=$target_path;
$load=mysql_query("LOAD DATA LOCAL INFILE $filename INTO TABLE CityBlocks FIELDS TERMINATED BY ',' ENCLOSED BY ''"' LINES TERMINATED BY ''r'n' IGNORE 2 LINES (startIpNum, endIpNum, locId)''n' ") or die(mysql_error());
if ($load){
echo("Failed");
} else {
echo("Success");
}
}
?>
</BODY>
</HTML>
我需要尽快解决这个问题。请帮忙。谢谢。
1 - 尝试改进过滤器,以检测错误发生的位置
2 - 尝试为 csv 文件使用一个名称(重命名上传的文件以进行测试.csv),以保护自己,并确保文件将成功上传
3 - 尝试使用 file_get_contents 而不是move_uploaded_file
4 - 确定您的错误何时发生? 上传或导入时出错?
试试这个代码:
编辑
我很高兴您已经解决了您的问题
我已经尽可能多地解释了,我很抱歉我的英语
<?php
/*
The script idea is
1- we get the file name on the user computer
2- we rename it to import.csv
3- we check if file already exists or not
4- we upload the file
5- import the file
*/
/*
STEP 1
if there is incoming files.
*/
if(!$_FILES['filename']['tmp_name']||empty($_FILES['filename']['tmp_name']))
die('Please select a file');
if($_FILES['filename']['tmp_name']){
/*
STEP 2
Here we will check if there is import.csv file
if there is import.csv file, that's mean there is a process underway
*/
if(file_exists('/tmp/import.csv'))
die('Please wait, we still working on the previous process...');
/*
File path
on the user computer
*/
$filepath=$_FILES['filename']['tmp_name'];
/*
New path for the file
/tmp folder.. always have a permits that allow the lifting of the file
*/
$newpath='/tmp/import.csv';
/*
Get the content of the file
read more about file_get_contents()
*/
$getFile=file_get_contents($filepath);
/*
file_put_contents()
function that's requires to parametrs #1 file name #2 file content
and it will create a file with the name and content that you have provided
*/
$uploade=(!empty($getFile))?file_put_contents($newpath,$getFile):die('We cant get the file!');
/*
if file exists we have succeed
*/
if(!file_exists($newpath))
die('The file did not uploaded..');
//import to database
$load = mysql_query("LOAD DATA LOCAL .........");
if($load){
//remove the file..
unlink($newpath);
echo 'success...';
}
else{
echo(file_exists($getFile))?'File uploaded but did not imported to mysql':'File not uploaded';
}
}
?>
您是否可以使用该导入命令通过命令行将文件导入 mysql? 首先尝试以确保它正常工作,然后再调查其余代码。
编辑:
根据mysql_query,如果成功,它应该返回 TRUE 或成功资源,如果不成功,则返回 FALSE。 您的代码如下所示:
if ($load){
echo("Failed");
} else {
echo("Success");
}
我认为可能存在逻辑错误。 尝试将代码更改为以下内容,并告诉我们会发生什么:
$load=mysql_query("LOAD DATA LOCAL INFILE $filename INTO TABLE CityBlocks FIELDS TERMINATED BY ',' ENCLOSED BY ''"' LINES TERMINATED BY ''r'n' IGNORE 2 LINES (startIpNum, endIpNum, locId)''n' ");
if (!$load){
echo mysql_error();
} else {
echo("Success");
}