Jump to content

Strpos() + Array Problem


iMiles

Recommended Posts

Hey there,

 

I'm making a file upload php page. I needs to make sure the file being uploaded has an allowed extension. Allowed extensions are stored in a array called $allow. I'm trying to use strpos().

 

Upload.php:

<?php

if (isset($_POST['upload'])) {

	// Properties of the file
	$name = $_FILES["filename"]["name"];
	$type = $_FILES["filename"]["type"];
	$size = $_FILES["filename"]["size"];
	$temp = $_FILES["filename"]["tmp_name"];
	$allow = array('.java', '.class');
	$error = $_FILES["filename"]["error"];

	if ($error > 0) {

		echo "Error uploading file! Code $error.";

	} else {

		if (strpos($name, $allow) == false) { // Of course this won't work, but what will?

			echo "File type not allowed!";

		} else {

			move_uploaded_file($temp, "games/" . $name);	

			echo "Successfully uploaded game.";

		}

	}

}

?>

<p>

<html>

<p>

<form action = 'upload.php' method = 'POST' enctype = 'multipart/form-data'>
<input type = 'file' name = 'filename'>
<p>
<input type = 'submit' name = 'upload' value = 'Upload'>
</form>

</html>

 

Any help would be appreciated.

 

Link to comment
https://forums.phpfreaks.com/topic/207559-strpos-array-problem/
Share on other sites

 

EDIT: D'oh! I see Ken beat me, but I'll throw this out there since I already typed it out and tested it . . .  >:(  :P

 

$_FILES['filename']['name'] = 'damn.jpg';
$name = array();
$name = explode( '.', $_FILES['filename']['name'] );
$ext = array_pop($name);
if( $ext == 'java' || $ext == 'class' ) {
   // extension meets specs
} else {
   //extension doesn't meet specs.
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.