Hello
This is My code , Will Upload a file to my site and write the name of the file to my Database ..
The Quastion is , the Uploading Is Opetional , I do not want the user get error after sumbit, that no file was Uploaded , since its not Optional ..
2- this code is updating to the MySQL , so the problem i have , if there already a name exist and the user did not upload file , the code updates the row with the embty value .
i wanted it that if the user did not upload file to keep the "file" row as is no change to the name , and the Change only if the user Uploaded file ..
form
xoops_load('xoopsformloader');
$sform = new XoopsThemeForm(_MD_TEST_ADD, "form", "upload.php");
$sform->setExtra("enctype='multipart/form-data'");
$sform->addElement(new XoopsFormText(_MD_TEST_NAME, 'name', 50, 30));
$sform->addElement(new XoopsFormFile(_MD_TEST_ATTACHMENTS, 'xoops_upload_file', 0), false);
$button_tray = new XoopsFormElementTray('' ,'');
$button_tray->addElement(new XoopsFormButton('', 'submit', _MD_TEST_SEND, 'submit'));
$sform->addElement($button_tray);
$sform->display();
upload.php
//Upload things
$uploading=($_FILES['xoops_upload_file']['name']);
$id= HtmlSpecialchars($_GET['id']);
include_once '../../../class/uploader.php';
$allowed_mimetypes = array('image/gif', 'image/jpeg');
$maxfilesize = 10485760;
$target = "upload/";
$uploader = new XoopsMediaUploader($target, $allowed_mimetypes, $maxfilesize);
if ($uploader->fetchMedia($_POST["xoops_upload_file"][0])) {
$uploader->setTargetFileName($uploader->mediaName);
if (!$uploader->upload()) {
redirect_header(XOOPS_URL . "/modules/test/index.php" , 6, $uploader->getErrors());
} else {
// echo ""._MD_TEST_ADDED."";
}
} else {
redirect_header(XOOPS_URL . "/modules/test/index.php" , 5, $uploader->getErrors());
}
}
//Upload things
$query =mysql_query("UPDATE ".$xoopsDB->prefix("test")." SET name='$name', file='$file' WHERE id='$id' ")
or die("can't edit the test tble");