PHPverse 2025

Voting

: seven plus one?
(Example: nine)

The Note You're Voting On

timgolding_10 at hotmail dot com
17 years ago
An example of using the system to call the file command on a linux server. This script detects whether a user posted file is a jpeg, gif or png

<?PHP

$accepted_types
=array("JPEG" , "GIF", "PNG");

// The temporary filename of the file in which the uploaded file was stored on the server.
if(!empty($_FILES["uploadedfile"]))
{
$uploaddir = $_SERVER['DOCUMENT_ROOT']."/images/";
$uploaddir.=basename( $_FILES['uploadedfile']['name']);

//verfiy file using linux FILE command
$last_line = system('file '.escapeshellarg($_FILES['uploadedfile']['tmp_name']), $retval);

//get the file extension returned through magic database
$splitvals=explode(' image data' , $last_line);
$vals=explode(':', $splitvals[0]);
$vals[1]=str_replace(' ','', $vals[1]);

if (
in_array($vals[1], $accepted_types))
{
echo
$vals[1].' was accepted <br />';
if(!
file_exists($uploaddir)){
//Copy the file to some permanent location
if(move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], $uploaddir))
{
echo
$uploaddir." was uploaded! <br />";
}
else
{
echo
"There was a problem when uploding the new file, please contact admin about this.";
}
}
else echo
'This file already exists in DB please rename file before uploading';
}
}else echo
$_FILES['uploadedfile']['error'].'<br />';
?>

<< Back to user notes page

To Top