I have an upload form that works great. What I would like to add to it is a default picture if they don't have one to upload yet that would also run through the rename function, so it gets a random name associated with it as well. If I add copy in the bit about if NO FILE, "That file type is not allowed" so I'm not sure how to add this feature.
Here is the code
if($_POST['upload']) {
// your settings;
$max_filesize = 30720; // 30kb, you can edit this
$upload_dir = '/home/xxxxxxx/public_html/xxxxxxxxx/admin/images/'; // you get the idea
// choose which file extension you allow user to upload.
$img_types = array('gif', 'jpeg', 'jpg', 'png', 'GIF', 'JPEG', 'JPG', 'PNG');
// no files
if ($_FILES['image']['name'] == "") {
echo "Please select a file to upload!\n";
exit;
}
// extension of filename
$ext = substr(stristr($_FILES['image']['name'],'.'),1);
if (!in_array( $ext, $img_types )) {
echo "That file type is not allowed!\n";
exit;
}
if ($_FILES['image']['size'] > $max_filesize) {
echo "That file type is too large!\n";
exit;
}
// this is only for file size checking, width and height...comment out if you dont want this
$size = getimagesize($_FILES['image']['tmp_name']);
if($imagewidth > $maxwidth || $imageheight > $maxheight) {
echo "That file type is too large!\n";
exit;
}
// rename file to a unique
$newname = time() . substr($file, -4);
/**************************************************/
//
// On successful upload, insert info in DB
//
/**************************************************/
if (move_uploaded_file($_FILES['image']['tmp_name'], "". $upload_dir . $newname . '.' . $ext )) {
// db insert codes go here
// Add file entry
$photo = $_FILES['image']['name'];
$picurl = $_POST['picurl'];
$lastName = $_POST['lastName'];
$firstName = $_POST['firstName'];
$mobile = $_POST['mobile'];
$office = $_POST['office'];
$fax = $_POST['fax'];
$para = $_POST['para'];
$parb = $_POST['parb'];
$sql = "INSERT INTO `contacts` ( `photo`,`picurl`,`lastName`,`firstName`,`mobile`,`office`,`fax`,`para`,`parb` )
VALUES ( '$newname.$ext','$picurl$newname.$ext>$newname.$ext</a>','$lastName','$firstName','$mobile','$office','$fax','$para','$parb' )";
// save the info to the database
$results = mysql_query( $query );
mysql_query($sql) or die(mysql_error());
}
}
?>
And the form
<html>
<head><title>Admin - Add Database Entry </title><link rel="stylesheet" href="dbstyle.css" type="text/css"></head>
<body>
<center><h3>Add An Item</h3>
<form name="uploader" method="post" action="<?php $_SERVER['PHP_SELF']?>" enctype="multipart/form-data">
<table bgcolor="#000000" width="500" cellpadding="3" cellspacing="0" border="0">
<tr>
<th bgcolor="#e3e4ff" align="left">Photo To Upload</th>
<td bgcolor="#d7ffff" align="left"><input type="file" name="image">
<input type="hidden" name="picurl" size="30" value="<a href=http://www.xxxxxxxxxxxxxxxxxx.com/xxxxxxxxxxx/images/">
</td>
</tr>
<tr>
<th bgcolor="#e3e4ff" align="left">First Name</th>
<td bgcolor="#d7ffff" align="left"><input type="text" name="firstName" size="30"></td>
</tr>
<tr>
<th bgcolor="#e3e4ff" align="left">Last Name</th>
<td bgcolor="#d7ffff" align="left"><input type="text" name="lastName" size="30"></td>
</tr>
<tr>
<th bgcolor="#e3e4ff" align="left">Mobile</th>
<td bgcolor="#d7ffff" align="left"><input type="text" name="mobile" size="15"></td>
</tr>
<tr>
<th bgcolor="#e3e4ff" align="left">Office</th>
<td bgcolor="#d7ffff" align="left"><input type="text" name="office" size="15"></td>
</tr>
</tr>
<tr>
<th bgcolor="#e3e4ff" align="left">Fax</th>
<td bgcolor="#d7ffff" align="left"><input type="text" name="fax" size="15"></td>
</tr>
<tr>
<th bgcolor="#e3e4ff" align="left">Intro</th>
<td bgcolor="#d7ffff" align="left"><textarea name="para" cols="40" rows="8" wrap="virtual"></textarea></td>
</tr>
<tr>
<th bgcolor="#e3e4ff" align="left">Paragraph</th>
<td bgcolor="#d7ffff" align="left"><textarea name="parb" cols="40" rows="8" wrap="virtual" align="top"></textarea></td>
</tr>
<tr><th bgcolor="#e3e4ff" align="left"></th>
<td bgcolor="#d7ffff" align="center"><input type="submit" name="upload" value="Add Entry" /><input type="reset" name="Reset" value="Clear Form"><input type=button value="Cancel" onClick="history.go(-1)"></td>
</tr>
</table> </center>
</form>
</body>
</html>
Hopefully I explained it so you could understand. Thanks!