I'm working on trying to approve photos that get posted. It works fine, but to keep files with the same name from overwriting each other I went with assigning them a random number. The problem is that even if a file is not uploaded it still writes a random number to the table and it shows up in the admin as an unknown file extension. I can't get it to not submit if no file is posted. Any ideas?

//Assign random number 
	$ran = rand () ;

// Add . to random number
	$ran2 = $ran.".";
 	$ext = getExtension ($_FILES['photo']['name']) ;	
	  
 	if(!isset($_FILES['photo']['name']))
	{
	$ran2='';
	$ext='';
	}	
	
 // Insert fields into XINFO 
	  $sql = "INSERT INTO xinfo (userid, date_submit, first_name, last_name, city, state_province, country, dob, email, photo)
	  VALUES ('$id', '$dsub','$fn', '$ln', '$city', '$state', '$cntry', '$dob', '$em', '$ran2$ext')";
Member Avatar for diafol
if(!isset($_FILES['photo']['name'])){
            $photo = '';
       }else{
	   //Assign random number 
	   $ran = rand () ;
           // Add . to random number
	   $ran2 = $ran.".";
 	   $ext = getExtension ($_FILES['photo']['name']) ;	
	   $photo = $ran2.$ext;
       }
       // Insert fields into XINFO 
      $sql = "INSERT INTO xinfo (userid, date_submit, first_name, last_name, city, state_province, country, dob, email, photo) VALUES ('$id', '$dsub','$fn', '$ln', '$city', '$state', '$cntry', '$dob', '$em', '$photo')";

How's that grab you?

Looks absolutely perfect. I changed the $photo variable to avoid confusion cuz I have it defined in another part of the script, but the darned thing still writes the random number to the table .... I'm missing something here. I see no reason for that not to work.

The only way this makes any sense at all to me is if $_FILES['photo']['name'] is passing along a value (which it's not) or the script for whatever reason is totally ignoring if(!isset($_FILES['photo']['name'])) which makes just as little sense to me. I'm stumped. I've never been one to give up but i'm rapidly approaching that point. I definitely need a new perspective on this problem. It still writes the random number to the table only without the file extension which makes sense. So I end up with 123546.

Thanks Ardav, I got it to work. I changed the code to this:

if(!isset($_FILES['photo']['name'])){
            $picture = '';
       }else{
	   //Assign random number 
	   $ran = rand () ;
       // Add . to random number
	   $ran2 = $ran.".";
 	   $ext = getExtension ($_FILES['photo']['name']) ;	
	   if($picture=='' && $ext==''){
	   $picture2='NONE'; }
	   else
	   $picture2 = $ran2.$ext; }

Which to me seems like I took a simple code and made it more long-winded. There's a good chance I could have simply corrected whatever error I was overlooking in the first place when I re-wrote the code. In any case, it works. Thanks again.

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.