How can I expand Diablo84`s nice script on uploading files with a checkbox that let users
choose a default image if they have no image to upload.Something like:
if ((empty($file_name)) and (empty($no_image)))
{
$error[] = "No file has been selected";
}
I have placed a default.jpg image in the uploadfolder.
-=-=-=-=-=-=-=-=-=-=-=-=-=
-=-=-=-=-=
-=-=-=-=-=
-
<?php
if(array_key_exists('submi
t',$_POST)
) {
$allowed_files = array('image/gif','image/p
jpeg','ima
ge/jpeg','
image/jpg'
,'image/pn
g');
$file_max = 51200; //1mb = 1048576 | 512kb = 524288 | 50kb = 51200
$upload_path = $_SERVER['DOCUMENT_ROOT'].
"/pictures
/"; //upload folder - make sure this folder has write permission!!!!!
$file_name = $_FILES['upload']['name'];
$error = array();
if (empty($file_name)) {
$error[] = "No file has been selected";
}
else {
if (!in_array($_FILES['upload
']['type']
,$allowed_
files)) $error[] = "File type is not allowed";
if ($_FILES['upload']['size']
> $file_max) $error[] = "File size is too big";
if (file_exists($upload_path.
$file_name
)) $error[] = "A file with this name already exists";
}
if (count($error) == 0) {
move_uploaded_file($_FILES
['upload']
['tmp_name
'], $upload_path.$file_name) or die("Error Completing Request");
echo "File Upload Complete<br>";
}
else {
echo "The following errors occured\n<br>";
foreach ($error as $var) echo " - $var<br>\n";
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="uploadfile" enctype="multipart/form-da
ta">
<input name="upload" type="file" size="40">
<input name="submit" type="submit" value="Upload">
===========---------------
----------
==========
==========
==========
=
<input name=\"no_image\" type=\"checkbox\" value=\"default.jpg\">
=============-------------
----------
==========
==========
==========
===
</form>
$fb = $_FILES['upload']['name'];
$fb = $no_image;
$db->sql_query("INSERT INTO far VALUES (NULL,'$username','$fb','$
f1','$f2')
");
And if the user activate the checkbox this will change the insert query to $fb = $no_image
Or do you have a better solution?