Forum Moderators: coopster

Message Too Old, No Replies

$ FILES not showing up with empty()

         

quasi

8:03 pm on Jan 30, 2008 (gmt 0)

10+ Year Member



I have some issues with some form data. Ive used this same code on other machines but I can't extract the data on the machine I'm currently working on. Here's some code and results.

The following snippet:
=================================================
<?
function processImage()
{
?>
<pre>
<?
print_r($_POST);
print_r($_FILES);
print_r($HTTP_POST_FILES);
print_r($_COOKIE);
?>
</pre>
<?

$imageName = empty($_FILES['picture']['name'])? die ("$user, You must enter a file! <BR> <a href='javascript:history.back()'>BACK</a>") : mysql_real_escape_string($_FILES['picture']['name']);

echo "IMAGENAME: $imageName";
...
========================================
Results with:
========================================

Array
(
[description] => pp
[type] => 1
[submitButtonName] => Submit Image
)
Array
(
[picture] => Array
(
[name] => cinder.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpyOBVJF
[error] => 0
[size] => 151172
)

)
Array
(
[ausUID] => 7
)

IMAGENAME:

===========================================
The $imageName variable is not getting filled but you can see it in the array.

file_uploads is on in php.ini

Any suggestions?

coopster

8:35 pm on Jan 30, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yeah, dump it instead of printing it. I'm guessing mysql_real_escape_string [php.net] is returning boolean
FALSE
otherwise you would likely see your image name being printed out there.
var_dump [php.net]($imageName);

quasi

8:52 pm on Jan 30, 2008 (gmt 0)

10+ Year Member



Okay I changed:
...
$imageName = empty($_FILES['picture']['name'])? die ("$user, You must enter a file! <BR> <a href='javascript:history.back()'>BACK</a>") : mysql_real_escape_string($_FILES['picture']['name']);
========
To:
...
$imageName = empty($_FILES['picture']['name']);
var_dump ($imageName);
echo "IMAGENAME: $imageName";

And now I get:
======================

Array
(
[description] => y
[type] => 1
[submitButtonName] => Submit Image
)
Array
(
[picture] => Array
(
[name] => cinder.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpuhKieg
[error] => 0
[size] => 151172
)

)
Array
(
[ausUID] => 7
)

bool(false) IMAGENAME:

coopster

8:59 pm on Jan 30, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yeah, but that is what you would expect to see! empty [php.net] returns
FALSE
if the variable has a non-empty and non-zero value. And your variable has a non-empty, non-zero value. The logic you have says if the variable is empty then exit the script and dump the message with a back button. However, if it is true, escape the string for further processing. Go back to your original logic and dump the var where you were printing it out earlier.

quasi

9:16 pm on Jan 30, 2008 (gmt 0)

10+ Year Member



Okay, I now have:
=====================
var_dump($_FILES['picture']['name']);

$imageName = empty($_FILES['picture']['name'])? die ("$user, You must enter a file! <BR> <a href='javascript:history.back()'>BACK</a>") : mysql_real_escape_string($_FILES['picture']['name']);

?><BR><?

var_dump ($imageName);

=======================
Result is:
=======================

Array
(
[description] => y
[type] => 1
[submitButtonName] => Submit Image
)
Array
(
[picture] => Array
(
[name] => cinder.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpgcIKz5
[error] => 0
[size] => 151172
)

)
Array
(
[ausUID] => 7
)

string(10) "cinder.jpg"
bool(false)

=================
Wasn't sure I understood you correctly so I put it in two places.

cameraman

9:41 pm on Jan 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To use mysql_real_escape_string() you need an open connection to mysql - do you have a connection open?

quasi

9:54 pm on Jan 30, 2008 (gmt 0)

10+ Year Member



Ahh, that looks like it was the problem. Apparently I had opened a connection previously in other sites and never performed a close so this problem never showed up until now.

Thanks

coopster

3:48 am on Jan 31, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



That's why I was encouraging you to dump your variables ;)

I would also encourage you to turn up your error_reporting [php.net] during testing (not in your LIVE environment though) so these errors will be more prominent while you develop and test.

Glad you got it sorted!