I have a form that gives the user an option to upload a file. The field is optional, but if they do not upload a file I would like to insert a default image into the database. At one point it was working but I can't seem to figure out what happened, here is the script:

if(isset($_FILES['fn'])){
$fn = $_FILES['fn']['name'];
//now try to upload the file
if(!move_uploaded_file($_FILES['fn']['tmp_name'],'../images/'.$_FILES['fn']['name'].'')){
switch($_FILES['fn']['error']){
case 1:
$msg .= "The file exceeds the upload max file size setting";
break;
case 2:
$msg .="The file exceeds the max file size setting";
break;
case 3:
$msg .="The file was only partially uploaded";
break;
case 4:
$msg .="No file was uploaded";
break;
}

if(isset($fn)){
$img = $fn;
}else{
$img = "user.gif";
}

The image upload works fine it just doesn't enter "user.gif" into the database if they do not upload a file. What it seems to me is happening is that if the user doesn't upload a file, the $img variable is never set, but I am not sure. I've tried several ways with no success.

It's hard to see what is wrong with that code.
Is it possible to see the bit of code that updates your database?

I wonder if that bit is dependent on a file being uploaded and so you have something like this:

if (isset($fn))
  {
     /* your MySql query here - this is depended on $fn existing for running*/
  }

One way to test if $img is ever being set...
What happens if you temporarily add a line of code to do do this:

if(isset($fn)){
$img = $fn;
}else{
$img = "user.gif";
echo "just testing that user.gif is being set";
}

If the line is not echoed, then you know that the else option is not being triggered and so $img isn't being set

I have a form that gives the user an option to upload a file. The field is optional, but if they do not upload a file I would like to insert a default image into the database. At one point it was working but I can't seem to figure out what happened, here is the script:

if(isset($_FILES['fn'])){
$fn = $_FILES['fn']['name'];
//now try to upload the file
if(!move_uploaded_file($_FILES['fn']['tmp_name'],'../images/'.$_FILES['fn']['name'].'')){
switch($_FILES['fn']['error']){
case 1:
$msg .= "The file exceeds the upload max file size setting";
break;
case 2:
$msg .="The file exceeds the max file size setting";
break;
case 3:
$msg .="The file was only partially uploaded";
break;
case 4:
$msg .="No file was uploaded";
break;
}

if(isset($fn)){
$img = $fn;
}else{
$img = "user.gif";
}

The image upload works fine it just doesn't enter "user.gif" into the database if they do not upload a file. What it seems to me is happening is that if the user doesn't upload a file, the $img variable is never set, but I am not sure. I've tried several ways with no success.

Try this code
it is working and I have tested it.

if(isset($_FILES['fn']))
{

$img = $_FILES['fn']['name'];

    //now try to upload the file
    if(!move_uploaded_file($_FILES['fn']['tmp_name'],'../images/'.$_FILES['fn']['name'].''))
    {
        switch($_FILES['fn']['error'])
        {
        case 1:
        $msg .= "The file exceeds the upload max file size setting";
        break;
        case 2:
        $msg .="The file exceeds the max file size setting";
        break;
        case 3:
        $msg .="The file was only partially uploaded";
        break;
        case 4:
        $msg .="No file was uploaded";
        break;
        }
    }
}
else
{
    $img = "user.gif";
}

echo $img;

Hire, I have tried that code and still the user.gif record is not added if no image is uploaded. Here is The updated code including the insert statement:

if(isset($_FILES['fn']))
{

$img = $_FILES['fn']['name'];

    if(!move_uploaded_file($_FILES['fn']['tmp_name'],'../images/'.$_FILES['fn']['name'].''))
    {
        switch($_FILES['fn']['error'])
        {
        case 1:
        $msg .= "The file exceeds the upload max file size setting";
        break;
        case 2:
        $msg .="The file exceeds the max file size setting";
        break;
        case 3:
        $msg .="The file was only partially uploaded";
        break;
        case 4:
        $msg .="No file was uploaded";
        break;
        }
    }
}
else
{
    $img = "user.gif";
}

echo "$img";

$theme = $_POST['select'];

$sql_ins="INSERT INTO members(uid,fname,lname,uname,upass,email,level,theme,img,actcode) values

('','".$fname."','".$lname."','".$uname."','".md5($upass)."','".$email."','".$level."','".$theme."','".$img."','".$actcode."')

";
$result = mysql_query($sql_ins);

I left the echo line there, user.gif is not echoed to the browser, telling me that the else condition is not working for some reason. Thanks for the suggestions.

Hmm didn't wrap on line 36. Should have ."','".$img."','".$actcode."') at the end.

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.