I have a concern about my program....the files that I want uploaded vary n size....would that affect the upload?
I did not set anything anywhere to enforce the file size to be uploaded, and only really small files are able to upload...

Recommended Answers

All 65 Replies

okie dokie....I see I got some reading to do...
Will keep you posted...

Well I did increase

post_max_size = 100M

as well as

upload_max_filesize = 50M

and it still doesn't upload the larger file, only the smaller one...

I also increased

memory_limit

and

max_execution_time

and nothing seems to work...

Okay ! I did a quick test. I changed post_max_size to 80M and upload_max_filesize to 80M. I could upload a file of size 40mb (I didn't try 80mb!).

Upload: Toss the feather.divx
Type: video/divx
Size: 41345.8066406 Kb
Temp file: C:\wamp\tmp\php29.tmp
Stored in: upload/Toss the feather.divx

Maybe, You are using something like this ? <input type="hidden" name="MAX_FILE_SIZE" value="100000" />

No, I didn't use a hidden input...
here's my code...

<tr>
    <td colspan="6">
		  	<form action="<? echo $_SERVER['REQUEST_URI']; ?>" enctype="multipart/form-data" method="post">
				<table>					
				  <tr>
					  <td>
					      <p align="left"><span class="style4 style25 style28">Bill Uploader</span></p>
					      <p align="left"><span class="style21 style29">The Bill Uploader works in 2 steps.</span></p>					  </td>
				  </tr>
   				  <tr><td><p>&nbsp;</p></td></tr>
				  <tr>
				  	<td>
				      <table width="950" border="0" align="center" cellpadding="1" cellspacing="0" >
						<tr>
						  <td><p>&nbsp;</p></td>
						  <td colspan="4" bgcolor="#006600">
						  	<p align="left" class="style32">
						  <span class="style21  style33">Step 1: Browse for file</span></p>						  </td>
						  <td><p>&nbsp;</p></td>
						</tr>
						<tr>
						  <td><p>&nbsp;</p></td>
						  <td bgcolor="#006600"><p>&nbsp;</p></td>
						  <td width="125" bgcolor="#006600" scope="row"><div align="left" class="style13 style29 style31">Browse</div></td>
						  <td width="356" bgcolor="#006600">
						  	<div align="left">
								<strong>
									<input type="file" name="file" />
								</strong>							</div>						  </td>
						  <td bgcolor="#006600"><p>&nbsp;</p></td>
						  <td><p>&nbsp;</p></td>
						</tr>
						<tr width="950">
						  <td><p>&nbsp;</p></td>
						  <td colspan="4" bgcolor="#006600">
						  	<p align="left"><strong>
							  <span class="style21 style30">Step 2: Upload the file selected</span></strong></p>						  						  </td>
						  <td><p>&nbsp;</p></td>
						</tr>
						<tr>
						  <td><p>&nbsp;</p></td>
						  <td bgcolor="#006600"><p>&nbsp;</p></td>
						  <td bgcolor="#006600"><p>&nbsp;</p></td>
						  <td bgcolor="#006600">
						  <div align="left">
						  	<strong>
					      		<input type="submit" value="Upload" name="btnSubmit" />
						  	</strong>						  </div>  						  </td>
						  <td bgcolor="#006600"><p>&nbsp;</p></td>
						  <td><p>&nbsp;</p></td>
						</tr>
					  </table>  				    </td>
				</tr>
			</table>
		</form>	</td>
  </tr>
  <tr>
  	<td colspan="6" >
		
	  <div align="left" class="style29">
		<?
			include ("./connect.php");
			global $conn;
			ini_set("display_errors", "1");
			error_reporting (E_ALL);
			
			if(count($_FILES) > 0)
			{
					$ext = "";
				$ext = substr(trim($_FILES["file"]["name"]), -4);
				$allowedext = array(".txt", ".csv", ".sql");
				
				if(in_array($ext, $allowedext))
				{
					$filename = $_FILES['file']['tmp_name'];
					//$fh = fopen($_FILES['file']['tmp_name'], 'r');
					$handle = fopen($filename, "r");
					
					while (($data = fgetcsv($handle, 100000, ',', '"')) !== FALSE)
					{
						$query = "INSERT INTO testtsttbills VALUES ('". implode("','", $data)."')";
						$query = @mysql_query($query,$conn);
					}		
					//$contents = fread($handle, filesize($filename));
					fclose($handle);		
					//echo $contents;
					echo "<div align='center'><font color='red'>The file was uploaded.</font></div>";
				}
				else
				{
					echo "<div align='center'><font color='red'>You are trying to upload an invalid file format. Please try again.</font></div>";
				}
			}
		?>
    </div></td>
  </tr>
  <tr><td colspan="6">&nbsp;</td></tr>

I checked your code, replaced your query with move_uploaded_file. It worked fine. I believe something is going wrong in the insert query. Print out the query, remove @ from mysql_query, use die(mysql_error()); . Everything else seems fine.
Here is the edited code. Try it out. If it uploads the file, then you know where the problem is!

<tr>
    <td colspan="6">
		  	<form action="<? echo $_SERVER['REQUEST_URI']; ?>" enctype="multipart/form-data" method="post">
				<table>					
				  <tr>
					  <td>
					      <p align="left"><span class="style4 style25 style28">Bill Uploader</span></p>
					      <p align="left"><span class="style21 style29">The Bill Uploader works in 2 steps.</span></p>					  </td>
				  </tr>
   				  <tr><td><p>&nbsp;</p></td></tr>
				  <tr>
				  	<td>
				      <table width="950" border="0" align="center" cellpadding="1" cellspacing="0" >
						<tr>
						  <td><p>&nbsp;</p></td>
						  <td colspan="4" bgcolor="#006600">
						  	<p align="left" class="style32">
						  <span class="style21  style33">Step 1: Browse for file</span></p>						  </td>
						  <td><p>&nbsp;</p></td>
						</tr>
						<tr>
						  <td><p>&nbsp;</p></td>
						  <td bgcolor="#006600"><p>&nbsp;</p></td>
						  <td width="125" bgcolor="#006600" scope="row"><div align="left" class="style13 style29 style31">Browse</div></td>
						  <td width="356" bgcolor="#006600">
						  	<div align="left">
								<strong>
									<input type="file" name="file" />
								</strong>							</div>						  </td>
						  <td bgcolor="#006600"><p>&nbsp;</p></td>
						  <td><p>&nbsp;</p></td>
						</tr>
						<tr width="950">
						  <td><p>&nbsp;</p></td>
						  <td colspan="4" bgcolor="#006600">
						  	<p align="left"><strong>
							  <span class="style21 style30">Step 2: Upload the file selected</span></strong></p>						  						  </td>
						  <td><p>&nbsp;</p></td>
						</tr>
						<tr>
						  <td><p>&nbsp;</p></td>
						  <td bgcolor="#006600"><p>&nbsp;</p></td>
						  <td bgcolor="#006600"><p>&nbsp;</p></td>
						  <td bgcolor="#006600">
						  <div align="left">
						  	<strong>
					      		<input type="submit" value="Upload" name="btnSubmit" />
						  	</strong>						  </div>  						  </td>
						  <td bgcolor="#006600"><p>&nbsp;</p></td>
						  <td><p>&nbsp;</p></td>
						</tr>
					  </table>  				    </td>
				</tr>
			</table>
		</form>	</td>
  </tr>
  <tr>
  	<td colspan="6" >
		
	  <div align="left" class="style29">
		<?
		/*
			include ("./connect.php");
			global $conn;
		*/
			ini_set("display_errors", "1");
			error_reporting (E_ALL);
			
			if(count($_FILES) > 0)
			{
					$ext = "";
				$ext = substr(trim($_FILES["file"]["name"]), -4);
				$allowedext = array(".txt", ".csv", ".sql");
				
				if(in_array($ext, $allowedext))
				{
					$filename = $_FILES['file']['tmp_name'];
					
					/*
					//$fh = fopen($_FILES['file']['tmp_name'], 'r');
					$handle = fopen($filename, "r");
					
					while (($data = fgetcsv($handle, 100000, ',', '"')) !== FALSE)
					{
						echo $query = "INSERT INTO testtsttbills VALUES ('". implode("','", $data)."')";
						echo "<br>";
						//$query = @mysql_query($query,$conn);
					}		
					//$contents = fread($handle, filesize($filename));
					fclose($handle);		
					*/
					move_uploaded_file($filename,"upload/".$_FILES['file']['name']);
					//echo $contents;
					echo "<div align='center'><font color='red'>The file was uploaded.</font></div>";
				}
				else
				{
					echo "<div align='center'><font color='red'>You are trying to upload an invalid file format. Please try again.</font></div>";
				}
			}
		?>
    </div></td>
  </tr>
  <tr><td colspan="6">&nbsp;</td></tr>

please correct me if I am wrong, but are you trying to upload and store a file?

I am trying to upload and store the data within the selected file...

Yep. I am trying to upload a file. It doesn't really matter what you do with the uploaded file. You can either upload/store it on the file system or open it and process the data in the file. I believe the problem is with the query and not the file upload.
Btw, check if you have specified a tmp folder path (upload_tmp_dir) in php.ini .

nope, there's no specified tmp folder

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
upload_tmp_dir = "c:/wamp/tmp"

I don't know whats the system default tmp folder. But I would suggest you to create a temporary folder with all the permissions. Did you print the query ? What does it print ?

I echoed these three statements...

while (($data = fgetcsv($handle, 100000, ',', '"')) !== FALSE)					
					{						
						echo $query = "INSERT INTO testtsttbills VALUES ('". implode("','", $data)."')";						
						echo "<br>";						
						echo $query = mysql_query($query,$conn);					
					}

here are the results...

INSERT INTO testtsttbills VALUES ('127000769106','630-3228','1/11/2007','7:58:46','DY','TRINIDAD','18683555181','4:00:00','0','3.2','3.2','P0')

Notice: Undefined variable: conn in /var/www/html/billUpload.php on line 207

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /var/www/html/billUpload.php on line 207

and nothing went into the database...

and this is what I set the upload location to:

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
upload_tmp_dir = "c:/uploads/tmp"

There is something wrong with your insert query. You can try it this way. insert into tablename (col1, col2, col3, ..) values ('val1','val2','val3',...) .
Also, you can execute this query in phpmyadmin to see if it executes without any error. :)

Ok, so at this stage, this may seem to be a few rather dumb questions...but what is phpMyAdmin? Can't I use MySQL Query Browser?:$ :sweat: :icon_redface:

phpmyadmin is an application to manage mysql databases just like mysql query browser :)

insert into tablename (col1, col2, col3, ..) values ('val1','val2','val3',...) .

The values ('val1','val2','val3',...) ...how am I to derive that from $data?...

How do you determine which value in $data to be inserted into which column ?

when the data was read

$handle = fopen($filename, "r");

the order that it was read is the order in which the data in sent

...('". implode("','", $data)."')

from the php manual...i got this for the return value of implode():
Returns a string containing a string representation of all the array elements in the same order, with the glue string between each element.

In a previous thread (when I was creating this script) someone gave me that chunk of code to try...and it worked....but only on small files...

What if $data has an empty column ? For example, instead of
'test1','test2','test3' if it has 'test1','test3' , then your query will definitely fail (because the column count in the table will not match with the values given).
Did you execute the query in query browser ?

I understand what you are saying....

yes i did run it in the query browser, and it ran well....with hardcoded data...
here it is:

insert into testtsttbills (account,service,date,time,timebandtype,destination,callednumber,mins,airtimecharge,tollcharge,amt,privatenumber) values ('127000769106','630-3228','1/11/2007','7:58:46','DY','TRINIDAD','18683555181','4:00:00','0','3.2','3.2','P0')

Exactly ! You need to specify the column names in your query..

ok, and what about the values?

could it be done like pulling the data into an array or something?

As in this example,

val1_1;val1_2;val1_3;val1_4
val2_1;val2_2;;val2_4

Notice that there is no val2_3. But there is a whitespace in the place of val2_3. So, there are fixed number of columns. I would then explode each row and separate the values.

$data_array = explode(";",$data); 
$value1 = $data_array[0];
$value2 = $data_array[1];
$value3 = $data_array[2];
$value4 = $data_array[3];
//since we know the number of columns in csv file, we can do it this way and even if a column is empty, nothing happens.
$query = "Insert into table (col1,col2,col3,col4) values ('$value1','$value2','$value3','$value4')";
...

This is how I do it.. Maybe someone else has a different idea ?

Hi,

Here is my code:

<?
	include ("./connect.php");			
	global $conn;			
	ini_set("display_errors", "1");			
	error_reporting (E_ALL); 			
			
	if(count($_FILES) > 0)			
	{					
		$ext = "";				
		$ext = substr(trim($_FILES["file"]["name"]), -4);			
		$allowedext = array(".txt", ".csv", ".sql"); 				
					
		if(in_array($ext, $allowedext))				
		{					
			$filename = $_FILES['file']['tmp_name'];
			move_uploaded_file($filename,"upload/".$_FILES['file']['name']);										
			//$fh = fopen($_FILES['file']['tmp_name'], 'r');			
			$handle = fopen($filename, "r"); 				
						
			while (($data = fgetcsv($handle, 100000, ',', '"')) !== FALSE)					
			{
				
				$data_array = explode(";",$data); 
				$value1 = $data_array[0];
				$value2 = $data_array[1];
				$value3 = $data_array[2];
				$value4 = $data_array[3];
				$value5 = $data_array[4];
				$value6 = $data_array[5];
				$value7 = $data_array[6]; 
				$value8 = $data_array[7]; 
				$value9 = $data_array[8];
				$value10 = $data_array[9]; 
				$value11 = $data_array[10]; 
				$value12 = $data_array[11]; 
										
				$query = "INSERT INTO testtsttbills (account,service,date,time,timebandtype,destination,callednumber,mins,airtimecharge,tollcharge,amt,privatenumber) VALUES ('$value1','$value2','$value3','$value4','$value5','$value6','$value7','$value8','$value9','$value10','$value11','$value12')";						
				$qry = mysql_query($query,$conn);					
			}							
			//$contents = fread($handle, filesize($filename));				
			fclose($handle);							
			//echo $contents;					
			echo "<div align='center'><font color='red'>The file was uploaded.</font></div>";				
		}				
		else				
		{					
			echo "<div align='center'><font color='red'>You are trying to upload an invalid file format. Please try again.</font></div>";			
		}			
	}		
?>

Here is the result: (this displays for the exact number of rows of data in the file)

Warning: move_uploaded_file(upload/smallTestFile.csv) [function.move-uploaded-file]: failed to open stream: No such file or directory in /var/www/html/billUploadTest.php on line 255

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phprGU33K' to 'upload/smallTestFile.csv' in /var/www/html/billUploadTest.php on line 255

Notice: Array to string conversion in /var/www/html/billUploadTest.php on line 262
Array
Notice: Undefined offset: 1 in /var/www/html/billUploadTest.php on line 264

Notice: Undefined offset: 2 in /var/www/html/billUploadTest.php on line 265

Notice: Undefined offset: 3 in /var/www/html/billUploadTest.php on line 266

Notice: Undefined offset: 4 in /var/www/html/billUploadTest.php on line 267

Notice: Undefined offset: 5 in /var/www/html/billUploadTest.php on line 268

Notice: Undefined offset: 6 in /var/www/html/billUploadTest.php on line 269

Notice: Undefined offset: 7 in /var/www/html/billUploadTest.php on line 270

Notice: Undefined offset: 8 in /var/www/html/billUploadTest.php on line 271

Notice: Undefined offset: 9 in /var/www/html/billUploadTest.php on line 272

Notice: Undefined offset: 10 in /var/www/html/billUploadTest.php on line 273

Notice: Undefined offset: 11 in /var/www/html/billUploadTest.php on line 274

Notice: Array to string conversion in /var/www/html/billUploadTest.php on line 262
Array
Notice: Undefined offset: 1 in /var/www/html/billUploadTest.php on line 264

Notice: Undefined offset: 2 in /var/www/html/billUploadTest.php on line 265

Notice: Undefined offset: 3 in /var/www/html/billUploadTest.php on line 266

Notice: Undefined offset: 4 in /var/www/html/billUploadTest.php on line 267

Notice: Undefined offset: 5 in /var/www/html/billUploadTest.php on line 268

Notice: Undefined offset: 6 in /var/www/html/billUploadTest.php on line 269

Notice: Undefined offset: 7 in /var/www/html/billUploadTest.php on line 270

Notice: Undefined offset: 8 in /var/www/html/billUploadTest.php on line 271

Notice: Undefined offset: 9 in /var/www/html/billUploadTest.php on line 272

Notice: Undefined offset: 10 in /var/www/html/billUploadTest.php on line 273

Notice: Undefined offset: 11 in /var/www/html/billUploadTest.php on line 274
.
.
.
The file was uploaded.

And incorrect data (under the column 'account', the data stored is 'Array') was stored in the first column in the table for the exact number of rows from the file...

Since the delimiter you are using is ,, you should explode the data on , and not ;. And also, You don't need move_uploaded_file in your script.

I took off move_uploaded_file and I also changed the delimiter from ; to ,
and I am getting the same error...

Can you do a thing ? Can you print $data and tell us what it prints ? ( And also, print_r($data_array); )
Btw, You can ignore notices by using error_reporting(E_ALL ^ E_NOTICE);

here is the code:

while (($data = fgetcsv($handle, 100000, ',', '"')) !== FALSE)			{
		echo ("data<br />");
		echo $data;
			
		echo ("<br />data_array<br />");
		$data_array = explode(",",$data); 
					print_r ($data_array);
		echo ("<br />");
		$value1 = $data_array[0];
		$value2 = $data_array[1];
		$value3 = $data_array[2];				$value4 = $data_array[3];
		$value5 = $data_array[4];
		$value6 = $data_array[5];
		$value7 = $data_array[6]; 
		$value8 = $data_array[7]; 
		$value9 = $data_array[8];
		$value10 = $data_array[9]; 
		$value11 = $data_array[10]; 
		$value12 = $data_array[11]; 
					$query = "INSERT INTO testtsttbills VALUES ('$value1','$value2','$value3','$value4','$value5','$value6','$value7','$value8','$value9','$value10','$value11','$value12')";						$qry = mysql_query($query,$conn);			}

here is what it prints:

data
Array
data_array

Notice: Array to string conversion in /var/www/html/billUploadTest.php on line 265
Array ( [0] => Array )

Notice: Undefined offset: 1 in /var/www/html/billUploadTest.php on line 270

Notice: Undefined offset: 2 in /var/www/html/billUploadTest.php on line 271

Notice: Undefined offset: 3 in /var/www/html/billUploadTest.php on line 272

Notice: Undefined offset: 4 in /var/www/html/billUploadTest.php on line 273

Notice: Undefined offset: 5 in /var/www/html/billUploadTest.php on line 274

Notice: Undefined offset: 6 in /var/www/html/billUploadTest.php on line 275

Notice: Undefined offset: 7 in /var/www/html/billUploadTest.php on line 276

Notice: Undefined offset: 8 in /var/www/html/billUploadTest.php on line 277

Notice: Undefined offset: 9 in /var/www/html/billUploadTest.php on line 278

Notice: Undefined offset: 10 in /var/www/html/billUploadTest.php on line 279

Notice: Undefined offset: 11 in /var/www/html/billUploadTest.php on line 280

:$ I am sorry !!! fgetcsv itself returns an array. You can directly use $data[0],$data[1] and so on..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.