I have a simple file uploader on my index.php page which it gets posted to my addnew.php page along with 2 other input fields like this below:
<form action="addnew.php" method="post" enctype="multipart/form-data">
<input type="text" name="title" placeholder="Title">
<input type="text" name="location" placeholder="location">
<input type="file" name="file">
<input type="submit" value="add">
</form>
my question is that when it sends to the file upload, it sends a generateRandomString function to replace the original file name, so if the form field is empty, it still sends a string of 10 random characters to my database. My question is that how can I set a if/else statement so that it does not send the random characters to my database if the upload field is empty. Here's the idea that I have, but it's obviously not working..
if (!empty($_FILES['file'])=="") {
$sql="INSERT INTO tabletry (title, location)
VALUES ('".addslashes($title)."', '".addslashes($location)."')";
} else {
$ext = pathinfo($_FILES['file']['name'])['extension'];
$nFn = generateRandomString(). ".$ext";
move_uploaded_file($_FILES['file']['tmp_name'],"pictures/".$nFn);
function generateRandomString($legnth = 10) {
return substr(str_shuffle("abcdefghijklmnopqrstuvwxyz"),0, $legnth);
}
$sql="INSERT INTO tabletry (title, location, mainimage)
VALUES ('".addslashes($title)."', '".addslashes($location)."', '{$nFn}')";
}
any help would be amazing..