Log in

View Full Version : system() function



traq
11-08-2009, 02:07 AM
okay, a little background info:

I'm trying to validate the mime type of a file (I want only images) uploaded to a server running PHP 5.2.11 (no mime_content_type() or finfo_open(FILEINFO_MIME_TYPE) support), and I ran across this method of using the system() function to return the mime type:

system("file -bi 'filename.jpg'");
and it works great. However, even when I assign it to a variable (e.g., $mimeType = system("file -bi 'filename.jpg'");), the result is automatically output to the browser. So, if I write a script like so:


$mimeType = system("file -bi 'filename.jpg'");
if(preg_match('/image/', $mimeType)){ echo 'filename.jpg is an image file'; }
else{ echo 'WARNING: filename.jpg is NOT an image file'; }

the browser displays:


image/jpegfilename.jpg is an image file

.
.
...Well, I guess that's more than "a little" background info.

But does anyone have an idea of how to hide the output from system() from being displayed?

Is there a better way to check the mime type (WITHOUT mime_content_type() or finfo_open())?

thanks, everyone.:cool:

thetestingsite
11-08-2009, 03:15 AM
Use exec (http://www.php.net/exec) instead of system. By doing this, it will get rid of the output of system to the browser. As far as a better way to check mime type, no clue off the top of my head.

Hope this helps nonetheless.

traq
11-08-2009, 03:31 AM
well, that works, so good enough unless someone else has a better idea. thanks!