I’m trying to use the following code to upload a csv file usually around 20,000 data into sql server database.
This code very rarely uploads successfully but usually i get the error below :
**Warning: fopen() [function.fopen]: Filename cannot be empty in C:\Inetpub\wwwroot\file.php on line 37
Warning: fgetcsv() expects parameter 1 to be resource, boolean given in C:\Inetpub\wwwroot\file.php on line 39
Warning: fclose(): supplied argument is not a valid stream resource in C:\Inetpub\wwwroot\file.php on line 40**
<?php
if(isset($_POST['SUBMIT']))
{
$filename =$_FILES['sel_file']['name'];
$checkextension = explode(".",$filename);
if($checkextension[1]) == "csv")
{
$filename1 = $_FILES['sel_file']['tmp_name'];
$handle=fopen($filename1,"r");
while($data=fgetcsv($handle,1000000)) { $records = count($data); }
fclose($handle);
if($records==15) {
$handle=fopen($filename1,"r");
while (($data = fgetcsv($handle, 1000000,",")) !== FALSE) {
$sqlquery = "INSERT into dataupload (date, field1, field2,
field3, field4, field5, field6, field7, field8, field9,
field10, field11, field12, field13, field14, field15)
values ('$data[0]','$data[1]', '$data[2]','$data[3]','$data[4]',
'$data[5]', '$data[6]', '$data[7]', '$data[8]', '$data[9]',
'$data[10]', '$data[11]', '$data[12]', '$data[13]', '$data[14]')";
mssql_query($sqlquery); }
fclose($handle);
echo "imported successfuly"; } else { echo "--sorry it is not csv file..."; }
} }
The temporary name in
$_FILES['input_name']['tmp_name']is not the name of the file you sent to the server.It’s a temporary name generated by PHP to make sure it can be stored in the temporary folder without overwriting other files.
$_FILES['input_name']['name']should contain the original name.This is the structure of the
$_FILESarray (when you upload one file only):