$_FILES['abc']['name'] ... empty?

[php]if (empty($_FILES[‘uploadedfile’][‘name’])) {
$ext = 0; }
else {[/php]
I’m trying to say that if the person did not upload a file, then I should set the variable $ext to zero. The else is just so that I can process the file if they did upload one.

Am I using the empty function wrong, or the $_FILES array wrong? For whatever reason, it seems to always set $ext=0, even if I DO upload a file.

Hi there,

Try:
[php]if(trim($_FILES[‘uploadedfile’][‘name’]) == ‘’)
{
$ext = 0;
}
else
[/php]

Unfortunately that suggestion isn’t working. Upon further digging, the situation only gets weirder. Is this a contradiction or what?
[php]$var = $_SESSION[‘user_id’];
if ($ext==0){
echo 'No image



';
echo $ext; //used for debugging only
}
else {
echo “”."



";}[/php]
Guess what it outputs?
"No image

png"
It says the extension is png, not ZERO!! But I just asked it to only output the extension if it was zero!!!

okay, nevermind. If anyone is reading this thread wondering what the answer was, it has to do with how PHP treats variables defined as zero. I just set $ext = ‘none’ if they didn’t upload a file, and then I can use the variable $ext in logic statements to see if it is none. Like if($ext == ‘none’) …that works. When you use a zero, PHP gets upset, probably has to do with it being loosely defined and what not. It’s not very intuitive if you’re a n00b. If anyone more experienced wants to elaborate, go ahead, but if not, my own issue has been resolved.

Hi there,

You can also use the notation NULL or FALSE in a Boolean sense as well as zero is considered nothing or FALSE.

Hope this extra bit sheds some light.

if (count($_FILES[‘uploadedfile’][‘name’])>0) {
$ext = 0; }
else {

I haven’t used it but I think this will also work.

Sponsor our Newsletter | Privacy Policy | Terms of Service