php if else error with form field -


my form has input file type field.

<input type="file" name="file" size="80" value=""> 

when submitted , field empty file upload part of php script should skipped. not seem happening. getting wrong file type message popping. nothing populated in field on form why if/else statement not being followed? doing wrong?

<?php // connect datebase require "master.db.php";   // if(empty statement not working? // should checking see if form field 'file' populated if check file // type if not skip , update sql database information in form field 'test' if(!empty($_files)) {     if ((($_files["file"]["type"] == "image/gif")     || ($_files["file"]["type"] == "image/jpeg")     || ($_files["file"]["type"] == "image/pjpeg"))     && ($_files["file"]["size"] < 2097152))     {         // code here works - function upload file - not part of current problem     }      // else statement displayed regardless of 'file' input     // wrong function intended purpose     else     {         // wrong file type         echo "invalid file <br />";         echo "tryed upload: " . $_files["file"]["name"] . "<br />";         echo "type: " . $_files["file"]["type"] . "<br />";         echo "size: " . ($_files["file"]["size"] / 1024) . " kb max 2mb <br />";         echo "temp file: " . $_files["file"]["tmp_name"] . "<br />";         die;     } } else  { // update other data in mysql database $sql="update `characters` set test='$test' id='$id'"; $result=mysql_query($sql);      // if updated mysql.     if($result){         echo "<b>update successful</b> <br />";         echo "<a href='test.html'>view result</a>";         touch('master.html');         clearstatcache();     }     else     {         echo "whoops: " . mysql_error(); ;     }     mysql_close(); } ?> 

the browser still send input value, though value empty. consequence, php populate $_files array like:

array (     [file] => array         (             [name] =>              [type] =>              [tmp_name] =>              [error] => 4             [size] => 0         )  ) 

so instead of checking isset/empty, guess should use php's native is_uploaded_file method. see this post more info on checking if optional file input provided.

i'll try clarify answer. reference gave suggests using file_exists , is_uploaded_file. according php manual using is_uploaded_file sufficient. in fact, can imagine is_uploaded_file looks file anyway (so internally file_exists).

please note should use tmp_name-key instead of name-key. 'name' key specifies name of file on client's pc, tmp_name specifies name on server. there situations when server renames file , therefore differs name on users pc.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -