I need to do some checking on my form and I can't work out how to do it.
Here's my page code:
My question is how do I check if the file upload field is empty or not?
What I actually want is for this input field to be optional, meaning if the user simply wishes to update the text input fields without loading a new image, then they have the choice of not uploading an image, just update the text fields.
Here's my page code:
Code:
<?php
session_start();
if (!isset($_SESSION['uid'])) {
die ("<span style='font-family:Verdana; font-size:12px'>You need to be logged in to view this page.<br /><a href='index.php'>Login</a></span>");
}
include_once ('blah/blah/includes/dbc.php'); // Connect to the db.
if ($_POST['Submit'] == 'Save') {
$alttext = $_POST['alttext'];
$link = $_POST['link'];
$upload_Name = $_FILES['image']['name'];
$upload_Size = $_FILES['image']['size'];
$upload_Temp = $_FILES['image']['tmp_name'];
$upload_Mime_Type = $_FILES['image']['type'];
$sPattern = '/\s*/m';
$sReplace = '';
$uploaded_image = preg_replace( $sPattern, $sReplace, $upload_Name );
if( $upload_Size == 0) {
header("Location: boxedit.php?msg=File size is ZERO!");
exit();
}
if( $upload_Mime_Type != "image/jpeg" AND $upload_Mime_Type != "image/gif" ) {
unlink($upload_Temp);
header("Location: boxedit.php?msg=Wrong type of image - only JPG or GIF");
exit();
}
//Where the file is uploaded to
$uploadFile = '../images/home/'.$uploaded_image;
if (!is_dir(dirname($uploadFile))) {
@RecursiveMkdir(dirname($uploadFile));
} else {
@chmod(dirname($uploadFile), 0755);
}
move_uploaded_file( $upload_Temp , $uploadFile);
chmod($uploadFile, 0755);
$upload_URL = '/images/home/'.$uploaded_image;
$sql_query = "UPDATE home SET image='%s', imagerollover='%s', alttext='%s', link='%s' WHERE id='%s'";
$params = array($upload_URL,$upload_URL2,$alttext,$link,$_POST['id']);
//escape the params
$params = array_map('mysql_real_escape_string', $params);
mysql_query(vsprintf($sql_query, $params)) or die (mysql_error());
header("Location: boxmenu.php?&msg=Content Updated");
}
?>
<html>
<head>
<title>Blah Blah</title>
<link rel="stylesheet" type="text/css" href="./includes/newstyle.css" />
<style>
input {
border: 1px solid #666666;
background-color: #DDDDDD;
}
</style>
</head>
<body>
<div id="container">
<div id="banner"><h1>Blah Blah</h1></div>
<div id="nav"><?php include './includes/nav.inc.php'; ?></div>
<div id="content">
<?php
$id = $_REQUEST['id'];
if( $id ) {
// retrieve the row from the database
$query = "SELECT * FROM home WHERE `boxnum`='$id'";
$result = mysql_query( $query );
if( $result && $home = mysql_fetch_object( $result ) ) {
// set our variables
$image = $home -> image;
$alttext = $home -> alttext;
$link = $home -> link;
}
}
// print out the form
//if (isset($_GET['msg'])) { echo "<div class=\"msg\"> $_GET[msg] </div>"; }
?>
<form action="<?php echo $PHP_SELF; ?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?php echo($id) ?>">
<br />
<table width="80%" border="1" cellpadding="3" cellspacing="0">
<tr>
<td>Image</td>
<td align="left" valign="top">
<input type="file" name="image" value="<?php echo($secondary) ?>" size="50">
</td>
<td><strong>Image size</strong><br />Small boxes: 350 x 300<br />Box 5: 314 x 605<br />Box 6: 659 x 190</td>
</tr>
<tr>
<td>ALT Text</td>
<td align="left" valign="top">
<input type="text" name="alttext" value="<?php echo($alttext) ?>" size="50">
</td>
<td> </td>
</tr>
<tr>
<td>Web Link</td>
<td align="left" valign="top">
<input type="text" name="link" value="<?php echo($link) ?>" size="50">
</td>
<td> </td>
</tr>
<tr>
<td align="center" colspan="4">
<input type="submit" name="Submit" value="Save"> <input type="button" Value="Cancel" onClick="history.go(-1)">
</td>
</tr>
</table>
</form>
</div>
<div id="footer"> </div>
</div>
</body>
</html>
My question is how do I check if the file upload field is empty or not?
What I actually want is for this input field to be optional, meaning if the user simply wishes to update the text input fields without loading a new image, then they have the choice of not uploading an image, just update the text fields.