我正在上传文件以恢复备份表(.sql 格式)...下面的代码用于上传 .sql 文件:
<form enctype="multipart/form-data" method="post">
<font style="font-size:18px;">File to import:</font><br />
<input size='30' type='file' name='filename'>
<input type="submit" name="submit" value="Upload File">
</form>
提交后,以下代码是:
set_time_limit ( 0 );
//Upload File
if (isset($_POST['submit'])) {
if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
if (move_uploaded_file($_FILES['filename']['tmp_name'], $_FILES['filename']['name'])) {
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$table_name = "items";
$backup_file = $_FILES['filename']['name'];
if(!file_exists($backup_file)){
echo "File Not Exists";
}
$sql = "LOAD DATA INFILE '$backup_file' INTO TABLE $table_name";
mysql_select_db('MM_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not load data : ' . mysql_error());
}
echo "Loaded data successfully\n";
mysql_close($conn);
fclose($handle);
}
?>
<script>
alert('Items Uploaded Successfully');
</script>
<?php
exit();
}
}
上传时,我收到此错误:
Could not load data : File 'c:\wamp\bin\mysql\mysql5.5.24\data\MM_db\items.sql' not found (Errcode: 2)
就像它没有找到我的 items.sql 文件一样,虽然我已经通过做检查了它是否存在file_exists($backup_file)
,但我仍然面临这个问题。我认为文件上传代码可能缺少某些内容?