Uploader and Array Questions ?

Ask about a PHP problem here.
Post Reply
Alg0r1thm
Posts: 64
Joined: Mon Sep 12, 2011 7:57 pm

Uploader and Array Questions ?

Post by Alg0r1thm »

Hi guys ,

I coding a simple uploader , but I stuck in one step , I wanna use the filter for my uploader so the uploader filters some extensions ,
I no the ways such as using mime to get the type of the content ,
<html>
<head>
<title>Uploader</title>
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/form-data" >
<input type="file" name="myfile" />
<input type="submit" name="submit" value="Upload" />
</form>
<?php
$path = 'uploads/';
if(!is_dir($path))
{
	mkdir($path);
}

if(isset($_POST['submit']))
{
	$file_path = $path.$_FILES['myfile']['name'];
	if(!eregi('', $_FILES['myfile']['name']))
	{
		if(!empty($_FILES['myfile']['name']))
		{
			if(move_uploaded_file($_FILES['myfile']['tmp_name'], $file_path))
			{
				echo 'Your file has been uploaded successfully!';
			}
			else
			{
				echo 'Error in uploading the file';
			}
		}
		else
		{
			if(empty($_FILES['myfile']['name']))
			{
				echo 'You did not select any file ... .';
			}
		}
	}
	else
	{
		echo 'Oh, invalid file!';
	}
}
?>
</body>
</html>
As you can see the code , here I used this function :
if(!eregi('', $_FILES['myfile']['name']))
To filter the extension but I don't know this annoying regular expression stuff !!!!!! ( It's annoying me badly if you have any effective solution or program that I can give the regex and get the result introduce me with )

I can also use something to walk through the array but I cut php for sometime and I've done some python stuff so I forgot how to walk through and array for example associative array and check the value of the array with specific value that I've inserted !

I mean for example I wanna check username and password in this array :
<?php
$username = 'Jacek';
$password = 'Betterphp';

$list = array('Jacek'=>':D','Jacek'=>':(','Jacek'=>'Why Jacek ?','Jacek'=>'Betterphp');

if($username == $list and $password == $list)
{
	// Do something with jacek here :D
}
?>
As you can see I wanna check username and password in this array how can I do this ?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Uploader and Array Questions ?

Post by jacek »

        $file_path = $path.$_FILES['myfile']['name'];
        if(!eregi('', $_FILES['myfile']['name']))
        {
                if(!empty($_FILES['myfile']['name']))
                {
That made me wince slightly.

For god sake, do not use ereg functions they are outdated and have been replaced by the preg_ functions. You do not even need regular expressions for this since you can jut defined an array of allowed extensions and then use in_array() to check the actual extension against that. You should also check if the file name is not empty first before you try to use it.
Image
Post Reply