Page 1 of 1

Upload Files in form tag

Posted: Tue Apr 27, 2010 12:50 am
by dunk_6
Hi all,

I have this upload file code, independently i can run this file but when i what to add in my codes, it doesn't work. What issue should i aware of and and if my codes already have form tag, which part should i paste this code to make sure that it runs? My concern is

Code: Select all

 enctype="multipart/form-data

Code: Select all

<?

if($_POST['check']){ 	 
	$faillampiran=$_POST['faillampiran'];
	$file=$_FILES['faillampiran']["name"];
	$fileSize = $_FILES['faillampiran']['size'];
	$fileType = $_FILES['faillampiran']['type'];
			
	if ($_FILES["faillampiran"]["error"] > 0 )
	{
	echo "Return Code: " . $_FILES["faillampiran"]["error"] . "<br />";
	}
	else
	{
	move_uploaded_file($_FILES["faillampiran"]["tmp_name"],"upload/" . $_FILES["faillampiran"]["name"]);
	echo '<table align = "center">';
	echo "<tr><td>";
	echo "Your file has been successfully stored.";
	echo "</td></tr>";
	echo '</table>';
	}	
}
?>

<form method="post" name="form1" id="form1" enctype="multipart/form-data"> 	
	<tr><td></td><td><input type="hidden" name="MAX_FILE_SIZE" value=""> </td> </tr>
	<tr><td> Please choose a file</td><td>:</td></tr>
	<tr>
	<input type="file" size="50" name="faillampiran" alt="faillampiran" id="faillampiran" 1value= "<?=$faillampiran;?>" />
	<tr align = "center"><td colspan = "3"><input type="submit" value="Hantar" name="check"/></td></tr>
	</tr>
</form>	

Re: Upload Files in form tag

Posted: Tue Apr 27, 2010 5:24 am
by social_experiment
dunk_6 wrote:I have this upload file code, independently i can run this file but when i what to add in my codes, it doesn't work.
Your coding logic is flawed, you are checking for an error BEFORE you have uploaded the file. An example.

Code: Select all

<?php if ( isset($_FILES['file1']) ) { 
 $name = $_FILES['file1']['name'];
 $source = $_FILES['file1']['tmp_name']; 							
 $target = "PATH_TO_FOLDER";
 if ( @is_uploaded_file($_FILES['file1']['tmp_name']) ) {
 $timo = @move_uploaded_file( $source, $target ); 
 }					 
 if ( !$timo ) { 
 $error = $_FILES['file1']['error'];
 if ( $error == 2 ) {										 $name = $_FILES['file1']['name'];
 $msg1 = "$name is to large to upload";
 }
 else {
  $msg1 = "No file has been selected for upload. A default image will be used.";
   }									
  }
 else { 
   $img1 = $_FILES['file1']['name'];							
  $msg1 = "<b>$img1</b> has been uploaded.";						
  } ?>
You also don't have a value for ><input type="hidden" name="MAX_FILE_SIZE" value="????"> which means any file up to 2mb can be uploaded without a problem. What isn't shown in the example is to check for file types when uploading.