Hi,

I trying to creat code to upload images .in my code i can upload files but i cant upload images can any one tell me what is the problem in my code


this is my code

<?php
$command=$_POST["command"];
If($command == "") {
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<TITLE>File Uploading Interface</TITLE>
</HEAD>
<BODY>
<CENTER>
<BR><BR>
<FORM ENCTYPE="multipart/form-data" NAME=MyForm ACTION="" METHOD="POST">
<INPUT TYPE="hidden" NAME="command" VALUE="1">
<INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="10000000000">
<TABLE>
<TR>
<TD>Choose File</TD>
<TD><INPUT NAME="MyFile" TYPE="File"></TD>
</TR>
<TR>
<TD COLSPAN="2"><INPUT NAME="submit" VALUE="Upload" TYPE="submit"></TD>
</TR>
</TABLE>
</FORM>
</CENTER>
</BODY>
</HTML> 
<?php
} else {
	$DestinationDir = "F:\wamp\www\surveys-html/";
	$DestinationFile = $_FILES['MyFile']['name'];
	if (move_uploaded_file($_FILES['MyFile']['tmp_name'], $DestinationFile)) {
	echo "File uploaded successfully.";
	} else {
		switch($_FILES['MyFile']['error']) {
			case 1 : echo "1The uploaded file exceeds the upload_max_filesize directive in php.ini.";
					break;
			case 2 : echo "1The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.";
					break;
			case 3 : echo "1The uploaded file was only partially uploaded.";
					break;
			case 4 : echo "1No file was uploaded.";
					break;
			case 6 : echo "1Missing a temporary folder.";
					break;
			case 7 : echo "1Failed to write file to disk";
					break;
			case 8 : echo "File upload stopped by extension";
					break;
		}
	}
}
?>

thanks in advance
Punitha pary

Recommended Answers

All 5 Replies

I couldn't see any wrong in your code.

in the move_uploaded_file() function you are moving the file to the file name itself without the upload directory prepended to it. you need to specify the directory the file needs to go to.

i rewrote your code. i made i a lot more secure. its untested, please let me know of any errors.

<?php

$upldDir = 'F:\wamp\www\surveys-html/';
$allowed = array( 'jpg','jpeg','gif','png','doc','docx','xls','ppt','pptx','pdf','zip','tiff' );
$maxSize = 10000000;

function getExtension( $file ) {
	$i = strrpos( $file,'.' );
	if ( !$i ) {
		return "";
	}
	$l   = strlen( $file ) - $i;
	$ext = substr( $file,$i + 1,$l );
	return $ext;
}

if ( isset( $_POST['submit'] ) ) {
	if ( count( $_FILES ) > 0 ) {
		$fileName = $_FILES['file']['name'];
		$tempName = $_FILES['file']['tmp_name'];
		if ( !is_uploaded_file( $tempName ) ) {
			$result = "File is not a valid uploaded file";
		}
		else {
			$exten = getExtension( $fileName );
			if ( !in_array( $exten,$allowed ) ) {
				$result = "File has an unknown extension";
			}
			else {
				$fileSize = filesize( $tempName );
				if ( $fileSize > $maxSize ) {
					$result = "File exceeds the max file size limit";
				}
				else {
					$upldDir = $upldDir . basename( $fileName );
					if ( !copy( $tempName,$upldDir ) ) {
						$result = "Unable to upload file";
					}
					else {
						$result = "File uploaded successfully!";
					}
				}
			}
		}
	}
}

$thisPage = $_SERVER['PHP_SELF'];

$html =<<<HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<TITLE>File Uploading Interface</TITLE>
</HEAD>
<BODY>
<CENTER>
<BR><BR>
<FORM ENCTYPE="multipart/form-data" NAME="MyForm" ACTION="{$thisPage}" METHOD="POST">
<TABLE>
<TR>
<TD>Choose File</TD>
<TD><INPUT NAME="file" TYPE="File"></TD>
</TR>
<TR>
<TD COLSPAN="2"><INPUT NAME="submit" VALUE="Upload" TYPE="submit"></TD>
</TR>
<TR>
<TD COLSPAN="2">{$result}</TD>
</TR>
</TABLE>
</FORM>
</CENTER>
</BODY>
</HTML>
HTML;

echo $html;

?>

hi

let me try this simple one

<?php
mysql_connect("localhost","root","rootwdp");
mysql_select_db("associates") or die("unable to select");
if(isset($_POST["update"]))
{
	$empid=$_POST['empid'];
	$username=$_POST['uname'];
	$dofjoin=$_POST['date'];
	$design=$_POST['design'];
	$exp=$_POST['exp'];
	$contact=$_POST['contact'];
	$pname=$_POST['pname'];
    $photo=$_POST['userfile'];
    if($_FILES['userfile']['size'] > 0)
		{
			
			$file_dir = "photos";
			foreach($_FILES as $file_name => $file_array) 
			$fileName = $_FILES['userfile']['name'];
			$fileSize = $_FILES['userfile']['size'];
			$fileType = $_FILES['userfile']['type'];
			$tmpName  = $_FILES['userfile']['tmp_name'];
			$file_array;
			$fileName = $empid.".jpg";
			if (is_uploaded_file($file_array['tmp_name'])) 
				{
					$test=move_uploaded_file($file_array['tmp_name'],"$file_dir/$fileName") or die ("Couldn't copy");
				}
		}
		if(@$test)
		{
		
		$photo=$file_dir."/".$fileName;
		}
//echo "<img src='".$_SESSION['photopath']."'/>";
//echo "<a href='#' onclick='refreshParent()'>close</a>";

$remarks=$_POST['remarks'];
 $query = "UPDATE `jos_employee` SET name='$username', designation='$design', date_of_joining='$dofjoin', experience='$exp', contact='$contact', remarks='$remarks' ";
 if($test)
{
$query.= "photo_url ='$photo'";
}
else
{

}

$query.=" WHERE emp_id=$empid";
$run=mysql_query($query);
//header("Location:index.php?option=com_testingofupdatedelete&page=$currpage");
header("Location:index.php?option=com_completedetailsofassociate");

}?>

<?php
mysql_connect("localhost","root","rootwdp");
mysql_select_db("associates") or die("unable to select");
 $id=$_GET['id'];
 $query = "SELECT * FROM `jos_employee` WHERE emp_id='$id'";
 $dd=mysql_query($query);

?>
	<form action="" method="post" name="myform" enctype="multipart/form-data">
		<table bgcolor="#CDE3EE" align="center" class="tablecls" >
  		<?php    
			   while( $row=mysql_fetch_array($dd))
				{
				?>
		  <tr>
		      <td>Username :&nbsp;&nbsp; </td>
			<td><input type="text" value="<?php echo $row[0];?>" name="uname" id="uname" /></td>
		   </tr>
		   
		   	<tr> <td>Emp Id :&nbsp;&nbsp; </td> 
	         <td><input type="text" value="<?php echo $row[1];?>" name="empid" id="empid" /></td>
		   </tr>
		   <tr>
		      <td>Designation :&nbsp;&nbsp; </td>
			  <td> <input type="text" value="<?php echo $row[2];?>" name="design" id="design" /></td>
		   </tr>
		   <tr>
		      <td>Date Of Join :&nbsp;&nbsp; </td>
		 <td><input type="text" value="<?php echo $row[3];?>" name="date" id="date" /></td>

		   </tr>
		   
		   <tr>
		      <td>Experience :&nbsp;&nbsp; </td>
			  <td> <input type="text" value="<?php echo $row[4];?>" name="exp" id="exp" /></td>
		   </tr>
		   <tr>
		      <td>Contact Number :&nbsp;&nbsp; </td>
			 <td><input type="text" value="<?php echo $row[5];?>" name="contact" id="contact" /></td>
		   </tr>
		   <tr><td>Photo</td>
		   <td colspan="2">
		  <img src="http://localhost/associates/<?php echo $row[6];?>" />
		  </td><td><input type="file" id="userfile" name="userfile" /></td></tr>
		    <tr>
		      <td>Remarks :&nbsp;&nbsp; </td>
		<td> <input type="text" value="<?php echo $row[7];?>" name="remarks" id="remarks" /></td>
		   </tr>
		  
		   <tr>
		   	  <td colspan="2" align="center"><input type="submit" name="update" id="update" value="Update"/>&nbsp;&nbsp;&nbsp;<input type="reset" id="reset" name="reset"></td>
			</tr>
			<?php } ?>
		</table>

Hello all , I get "1 Missing a temporary folder." where can I add my temp folder ?

Thanks

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.