Determining whether there is anything in the $_FILES array and prevent upload

Hi all,

I am trying to edit a profile and have a problem with the image.

When I create the profile, I can upload the image successfully and see the image when I view the profile.

However when I go to edit the profile, if I do not change values such as name, age etc then these get written back to the database with the unchanged value.

Unfortunately if the image is not changed, when the edit button is clicked, it gets overwritten with blank values.

What I am after is a way to say:

IF $FILES is empty
{
do not update image details
}
ELSE
{
update image details
}

My current code looks like this:

// EDIT A PLAYER PROFILE

if (isset($_POST['action']) and $_POST['action'] == 'Edit' )
{
	include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
	try
	{
		// $sql = 'SELECT id, name, age, position, height, weight, satscore, gpa FROM player WHERE id = :id';
		$sql = 'SELECT player.id, player.name AS name, age, position, height, weight, previousclubs.id AS previousclubsid, GROUP_CONCAT(distinct previousclubs.name) previousclubs,
			satscore, gpa, GROUP_CONCAT(distinct link) link, email, filename, mimetype, filedata
			FROM player INNER JOIN playerpreviousclubs
				ON player.id = playerid
			INNER JOIN previousclubs
				ON previousclubid = previousclubs.id
			INNER JOIN links
				ON links.playerid = player.id
			WHERE player.id = :id';
		$s = $pdo->prepare($sql);
		$s->bindValue(':id', $_POST['id']);
		$s->execute();
	}
	catch (PDOException $e)
	{
		$error = 'Error fetching profile details.' . $e->getMessage();
		include 'error.html.php';
		exit();
	}

	$row = $s->fetch();
	$pageTitle = 'Edit Profile';
	$action = 'editform';
	$name = $row['name'];
	$age = $row['age'];	
	$position = $row['position'];
	$height = $row['height'];
	$weight = $row['weight'];
	$satscore = $row['satscore'];
	$gpa = $row['gpa'];
	$previousclubs = $row['previousclubs'];
	$previousclubsid = $row['previousclubsid'];
	$link = $row['link'];
	$email = $row['email'];
	$filename = $row['filename'];
	$mimetype = $row['mimetype'];
	$filedata = $row['filedata'];*/
	$id = $row['id'];
	$button = 'Update Profile';

	include 'addplayerprofile.html.php';
	exit();
}

if (isset($_GET['editform']))
{
	include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
	
	// UPDATE MAIN PLAYER PROFILE DETAILS
	try
	{
		$sql = "UPDATE player SET
			name = :name,
			age = :age,
			position = :position,
			height = :height,
			weight = :weight,
			satscore = :satscore,
			gpa = :gpa,
			email = :email,
				filename = :filename,
				mimetype = :mimetype,
				filedata = :filedata
			WHERE id = :id";
		$s = $pdo->prepare($sql);
		$s->bindValue(':id', $_POST['id']);
		$s->bindValue(':name', $_POST['name']);
		$s->bindValue(':age', $_POST['age']);
		$s->bindValue(':position', $_POST['position']);
		$s->bindValue(':height', $_POST['height']);
		$s->bindValue(':weight', $_POST['weight']);
		$s->bindValue(':satscore', $_POST['satscore']);
		$s->bindValue(':gpa', $_POST['gpa']);
		$s->bindValue(':email', $_POST['email']);
				$s->bindValue(':filename',  $uploadname);
				$s->bindValue(':mimetype',   $uploadtype);
				$s->bindValue(':filedata',   $uploaddata);
		$s->execute();
	}
	catch (PDOException $e)
	{
		$error = 'Error editing player profile main details.' . $e->getMessage();
		include 'error.html.php';
		exit();
	}

	// UPDATE PREVIOUS CLUBS
	try
	{
		$sql = 'UPDATE previousclubs SET
			name = :previousclubs
			WHERE id = :previousclubsid';
		$s = $pdo->prepare($sql);
		$s->bindValue(':previousclubs', $_POST['previousclubs']);
		$s->bindValue(':previousclubsid', $_POST['previousclubsid']);
		$s->execute();
	}
	catch (PDOException $e)
	{
		$error = 'Error editing player previous clubs.' . $e->getMessage();
		include 'error.html.php';
		exit();
	}

I thought I could do something like this but it is not working:

if (isset($_FILES['upload']) && $_FILES['upload'] <> '')
{
$s->bindValue(':filename',  $uploadname);
$s->bindValue(':mimetype',   $uploadtype);
$s->bindValue(':filedata',   $uploaddata);
}
else
{
$s->bindValue(':filename',  $filename);
$s->bindValue(':mimetype',   $mimetype);
$s->bindValue(':filedata',   $filedata);
}

All help is appreciated.

Thanks

if( is_uploaded_file($_FILES[‘upload’][‘tmp_name’]) )
is what i use…This checks that file is uploaded on server or not…

Perfect. That is exactly what I was after!


 && ( $_FILES['upload']['error'] > 0 )

is a further important condition you can add, or you can trap the fact that error > 0 and feedback something or fail gracefully if you want to.

Error codes explained