CSV import
Posted: Sat Sep 25, 2010 4:09 am
i ahve created a file for csv import to mysql,It works fine on localhost but on server it show
error
"Warning: fopen(e:\\item1.csv) [function.fopen]: failed to open stream: No such file or directory in "
My code is
<?php
require_once('classes/tc_calendar.php');
?>
<?php
include "conn.php";
if(isset($_POST['submit']))
{
$filename=$_POST['filename'];
$id=$_POST['id'];
$handle = fopen("$filename", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$sql5=mysql_query("select bal from stock where item='$data[0]' and id='$id' ");
$num=mysql_num_rows($sql5);
if($num==0)
{
$import="INSERT into stock(id,item,receive,bal) values('$id','$data[0]','$data[1]','$data[1]')";
mysql_query($import) or die(mysql_error());
}
else{
$newbal='0';
$sql44=mysql_query("select bal from stock where item='$data[0]' and id='$id' and time =(select max(time)as time from `stock` where item='$data[0]' and id='$id' ) ");
while($row44=mysql_fetch_array($sql44))
{
$newbal=$data[1]+$row44['bal'];
$import="INSERT into stock(id,item,receive,bal) values('$id','$data[0]','$data[1]','$newbal')";
mysql_query($import) or die(mysql_error());
}
}
}
fclose($handle);
print "Import done";
}
else
{
?>
<html><head><link href="calendar.css" rel="stylesheet" type="text/css" />
<script language="javascript" src="calendar.js"></script></head>
<body>
<table width="100%">
<tr>
<td align='left'><a href='eview.php'>Dealer Control</a>
</BR></BR>
<a href='import.php'>Item ADD Through Excel file</a>
</br></br>
<a href='#'>Search</a></td>
<td align='right'>
<a href=logout.php style='text-decoration:none; color:red;'>Logout</a>
</td>
</tr>
</table>
<form action='import2.php' method='post'>
Type file name to import:<br>
<input type='text' name='filename' size='20'>
Buyer<select name="id">
<option value="">Select a Buyer:</option>
<?php
include"conn.php";
$userid=$_SESSION['userid'];
$sql44=mysql_query("select * from user ");
while($row44=mysql_fetch_array($sql44))
{
echo"<option value='$row44[id]'>$row44[name]</option>";
}
?>
</select></td>
<input type='submit' name='submit' value='submit'></form>
<?php
}
?>
</body>
</html>
"Warning: fopen(e:\\item1.csv) [function.fopen]: failed to open stream: No such file or directory in "
My code is
<?php
require_once('classes/tc_calendar.php');
?>
<?php
include "conn.php";
if(isset($_POST['submit']))
{
$filename=$_POST['filename'];
$id=$_POST['id'];
$handle = fopen("$filename", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$sql5=mysql_query("select bal from stock where item='$data[0]' and id='$id' ");
$num=mysql_num_rows($sql5);
if($num==0)
{
$import="INSERT into stock(id,item,receive,bal) values('$id','$data[0]','$data[1]','$data[1]')";
mysql_query($import) or die(mysql_error());
}
else{
$newbal='0';
$sql44=mysql_query("select bal from stock where item='$data[0]' and id='$id' and time =(select max(time)as time from `stock` where item='$data[0]' and id='$id' ) ");
while($row44=mysql_fetch_array($sql44))
{
$newbal=$data[1]+$row44['bal'];
$import="INSERT into stock(id,item,receive,bal) values('$id','$data[0]','$data[1]','$newbal')";
mysql_query($import) or die(mysql_error());
}
}
}
fclose($handle);
print "Import done";
}
else
{
?>
<html><head><link href="calendar.css" rel="stylesheet" type="text/css" />
<script language="javascript" src="calendar.js"></script></head>
<body>
<table width="100%">
<tr>
<td align='left'><a href='eview.php'>Dealer Control</a>
</BR></BR>
<a href='import.php'>Item ADD Through Excel file</a>
</br></br>
<a href='#'>Search</a></td>
<td align='right'>
<a href=logout.php style='text-decoration:none; color:red;'>Logout</a>
</td>
</tr>
</table>
<form action='import2.php' method='post'>
Type file name to import:<br>
<input type='text' name='filename' size='20'>
Buyer<select name="id">
<option value="">Select a Buyer:</option>
<?php
include"conn.php";
$userid=$_SESSION['userid'];
$sql44=mysql_query("select * from user ");
while($row44=mysql_fetch_array($sql44))
{
echo"<option value='$row44[id]'>$row44[name]</option>";
}
?>
</select></td>
<input type='submit' name='submit' value='submit'></form>
<?php
}
?>
</body>
</html>