How to know when the $_FILES array is empty - php

How can I know that the FILES does not contain an image, and adapt my code accordingly?
I want something like:
if(expression_that_check_FILES_array_isnot_empty)
{
do sth;
}
else
{
do sthElse;
}

Thanks Pavel Janicek for his answer in comment.
I used this:
if (!empty($_FILES)){
do sth;
}
else{
do sthElse;
}

Please try :
if($_FILES['file']['name']!="")
{
do sth;
}
else
{
do sthElse;
}

Try this
if(!empty($_FILES['file']['name']))
{
do sth;
}
else
{
do sthElse;
}

Can you try it?
if(!isset($_FILES["file"]["size"]) || ($_FILES["my_file_name"]["size"] == 0) || (!empty($_FILES["file"]["name"]) {
do sth; // Throw Error
} else {
do sthElse; //Upload
}

Related

If/Else: Adding "Or" to the If statement

I'm trying to condense my code to create less lines of PHP and was wondering if this is possible. Currently I have:
if ($_SERVER['HTTP_HOST']==domain1.com" ) {
down();
} elseif ($_SERVER['HTTP_HOST']=="domain2.com" ) {
down();
} else {
up();
}
?>
instead of having if and elseif displaying the same function, is it possible to do something like this:
if ($_SERVER['HTTP_HOST']==domain1.com" || $_SERVER['HTTP_HOST']=="domain2.com" ) {
down();
} else {
up();
}
?>
I tried the code above but my page went blank.. how do I do this if possible?
Its because of the syntax errors -
if ($_SERVER['HTTP_HOST']=="domain1.com" || $_SERVER['HTTP_HOST']=="domain2.com" ) {
down();
} else {
up();
}
or you can do this also -
$domains = array("domain1.com", "domain2.com");
if (in_array($_SERVER['HTTP_HOST'], $domains)) {
Shorter way:
$domains = ["domain1.com", "domain2.com"];
in_array($_SERVER['HTTP_HOST'], $domains) ? down() : up();
You forgot the opening " for the first $_SERVER['HTTP_HOST']==domain1.com"
try with :
if ($_SERVER['HTTP_HOST']=="domain1.com" || $_SERVER['HTTP_HOST']=="domain2.com" ) {
down();
} else {
up();
}
Fix syntax errors please
if ($_SERVER['HTTP_HOST']=="domain1.com" || $_SERVER['HTTP_HOST']=="domain2.com" ) {
down();
} else {
up();
}
?>

php function return a Boolean and use it on condition

im not sure on how i am going to explain this correctly.
I wanted a function to validate a string which i figured correctly.
But i want the function to return a boolean value.
And outside a function i need to make a condition that if the function returned false, or true that will do something. Here's my code.
i am not sure if this is correct.
<?php
$string1 = 'hi';
function validatestring($myString, $str2) {
if(!empty($myString)) {
if(preg_match('/^[a-zA-Z0-9]+$/', $str2)) {
}
}
else {
return false;
}
}
if(validatestring == FALSE) {
//put some codes here
}
else {
//put some codes here
}
?>
EDIT : Now what if there are more than 1 condition inside the function?
<?php
$string1 = 'hi';
function validatestring($myString, $myString2) {
if(!empty($myString)) {
if(preg_match('/^[a-zA-Z0-9]+$/', $str2)) {
return true;
}
else {
retun false;
}
}
else {
return false;
}
}
if(validatestring($myString, $myString2) === FALSE) {
//put some codes here
}
else {
//put some codes here
}
?>
Functions need brackets and parameter. You dont have any of them.
This would be correct:
if(validatestring($myString) === false) {
//put some codes here
}
An easier and more elegant method would be this:
if(!validatestring($myString)) {
//put some codes here
}
<?php
$string1 = 'hi';
function validatestring($myString) {
if(!empty($myString)) {
return true;
}
else {
return false;
}
}
if(validatestring($string1) === FALSE) {
//put some codes here
}
else {
//put some codes here
}
?>
Sidenote, since empty() already returns false ,you could simplify by doing:
function validateString($string){
return !empty($string);
}
if(validateString($myString){
// ok
}
else {
// not ok
}
To make a check and test later:
$check = validateString($myString);
if($check){ }
There's no need to check == false or === false, the function already returns a boolean, it would be redundant.
store $string1 to $myString in the function
myString=string1
<?php
$string1 = 'hi';
function validatestring($myString) {
myString=string1;
if(!empty($myString)) {
return true;
}
else {
return false;
}
}
if(validatestring() === FALSE) {
//put some codes here
}
else {
//put some codes here
}
?>

Add or to an if condition in PHP

I have this if condition:
if (isset($_REQUEST['altgeraet'])) {
$Altgeraet = 'OK';
} else {
$Altgeraet = 'NOK';
}
And I want when in the SQL Table Host_alt the value "KeinAlterHost" is the
$Altgeraet = 'OK'
This is what I tried but it didn't work:
if (isset($_REQUEST['altgeraet'])
OR ($resultarray['Hostname_alt'] == "KeinAlterHost")) {
$Altgeraet = 'OK';
} else {
$Altgeraet = 'NOK';
}
So is this setup right? I used the array_key_exists
if ((isset($_REQUEST['altgeraet']) OR (array_key_exists('KeinAlterHost',$resultarray['Hostname_alt'])) {
$Altgeraet = 'OK';
} else {
$Altgeraet = 'NOK';
}
You need an pipeline for OR
So the code will be this:
if (isset($_REQUEST['altgeraet']) || ($resultarray['Hostname_alt'] == "KeinAlterHost"))
The isset isn't quit right...
if (isset($_REQUEST['altgeraet']) OR ($resultarray['Hostname_alt'] == "KeinAlterHost")) {
$Altgeraet = 'OK';
} else {
$Altgeraet = 'NOK';
}
You need to quit it earlyer:
if (isset($_REQUEST['altgeraet']) OR ($resultarray['Hostname_alt'] == "KeinAlterHost")) {
$Altgeraet = 'OK';
} else {
$Altgeraet = 'NOK';
}
Because it checks if the request isset, and the hostname isset at you're old code. by the new one, it checks if the request isset, if not check if hostname == keinalterhost and then do the stuff...

PHP if statement errmsg

I have this if statment
if(!empty($URL) && ($safe===true)){
//lots of code
}
Is it possible to show different error messages depending on what condition failed?
For example if $URL is empty echo "URL empty";
and if $safe===false echo "GTFO";
Just add this to your code
else if(empty($URL)
{
echo "url empty";
}
else if($safe===false)
echo "Get Out"; // be polite ;)
if (empty($url))
{
echo "URL empty";
}
elseif ($safe === false)
{
echo "GTFO";
}
else
{
//lots of code
}
} else {
if($safe === false){
die("GTFO");
}
if (empty($url)){
echo "URL Empty.";
}
}
Yes; you could make use of an else if statement.
if (!empty($URL) && ($safe===true)) {
//lots of code
} else if (empty($URL)) {
// report that url is empty
} else if ($safe === false) {
// report that safe is false
}
Alternatively, you could just use an else statement to report that the if condition was false.
I propose the following solution. It will allow you to show multiple errors and set each condition only once (instead of having so many conditions and anti-conditions as other solutions proposed).
$errors = array();
if(empty($URL) {
$errors[] = 'URL empty';
}
if($safe !== true) {
$errors[] = 'GTFO';
}
if(empty($errors)) {
//lots of code
} else {
echo '<ul>';
foreach($errors as $error_message) {
echo '<li>' . $error_message . '</li>';
}
echo '</ul>';
}

PHP File Input Validation

How can I create a function in PHP that checks if a file input is selected? The following is what I have but its not working.
Thank you!
function mcFileUp($mcFile){
if(empty($_FILES[$mcFile]['name'])){
echo 'empty';
}
else{
echo 'Ok!';
}
}
I figured it out:
function mcFileUp($mcFile){
if(empty($mcFile))
{
echo 'empty';
}
else
{
echo 'Ok!';
}
}
mcFileUp($_FILES['mcFileUpload']['name']);
Here is a possible solution
function mcFileUp($mcFile)
{
if(strlen($_FILES[$mcFile]['name'])==0)
{
echo 'empty';
}
else
{
echo 'Ok!';
}
}
if(isset($_FILES["uploaded_file"]))
{
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0))
{
............
}
else
{
echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
}
}
else
{
echo "Error: No file uploaded";
}
Check THIS page if you want to know props and corns about the superglobal array "$_FILE". Also, you can have a look # THIS tutorial. Hope it helps you...

Categories