PHP:在截断表之前检查文件是否存在
在截断表之前,请有人帮助我让这个脚本检查文件是否存在。 如果文件名不存在,我想停止导入PHP:在截断表之前检查文件是否存在,php,mysql,import,truncate,Php,Mysql,Import,Truncate,在截断表之前,请有人帮助我让这个脚本检查文件是否存在。 如果文件名不存在,我想停止导入 <?php //set the connection variables $hostname = "host"; $username = "username"; $password = "pass"; $database = "database"; $filename = "filename.csv"; //connect to mysql database $connection = mysqli_
<?php
//set the connection variables
$hostname = "host";
$username = "username";
$password = "pass";
$database = "database";
$filename = "filename.csv";
//connect to mysql database
$connection = mysqli_connect($hostname, $username, $password, $database) or die("Error " . mysqli_error($connection));
mysqli_query($connection, "TRUNCATE TABLE `my_tablename`");
// open the csv file
$fp = fopen($filename,"r");
//parse the csv file row by row
while(($row = fgetcsv($fp,"500",",")) != FALSE)
{
//insert csv data into mysql table
$sql = "INSERT INTO Pristabell (Produkt, Pris, Rabattkr, Rabattprosent, Lagerstatus, Butikk, TAGS) VALUES('" . implode("','",$row) . "')";
if(!mysqli_query($connection, $sql))
{
die('Error : ' . mysqli_error($conection));
}
}
fclose($fp);
//close the db connection
mysqli_close($connection);
?>
一个简单的解决方案是使用
文件不存在
在if()块中
在while()之前,如果为true,则继续,否则退出或trow一个异常。文件存在
您尝试过吗?在截断前使用显示[FULL]表[{FROM | IN}db_name[如'pattern'| WHERE expr]
显示数据库中的表,如'my|u tablename'
。好的,没有弄错,但会留下注释。
if(file_exists($pathtofile)){
//do import
}else{
//stop
}