Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tony811

macrumors newbie
Original poster
Feb 8, 2012
2
0
Hi i have just finished a tutroial on how to upload a file using PHP however i am getting a very simple error i understand and just cant resolve the error it is

Notice: Use of undefined constant extension - assumed 'extension' in C:\wamp\www\File Upload\upload.php on line 11 (Highlighted in Red)

I know its to do with variable extension not being declared however my PHP coding skills are limited. And help would be great.



upload.php code is
<?php

$uploaddir = "uploads/images";
$allowed_ext = "jpg, JPG, png, gif";
$max_size = "500000000";
$max_width = "4000";
$max_height = "4000";


$extension = pathinfo($_FILES['file']['name']);
$extension = $extension[extension];
$allowed_paths = explode(", ",$allowed_ext);

for($i = 0; $i < count($allowed_paths); $i++){
if ($allowed_paths[$i] == "$extension"){
$ok = "1";
}
}

if ($ok == "1"){
if($_FILES['file']['size'] > $max_size)
{
print "File size is to big !";
exit;
}

if($max_width && $max_height){
list($width, $height, $type, $w)=
getimagesize($_FILES['file']['tmp_name']);
if($width > $max_width || $height > $max_height)
print "File height and or width are too big ! ";
exit;
}
}

if(is_uploaded_file($_FILES['files']['tmp_name']))
{
move_uploaded_file($_FILES['files']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
print "Your file has been uploaded successfully !";
}

else
{
print "Incorrect file extension !";
}
?>


index.php

<table width="500" height ="500" border="5" align = "center" cellpadding="10">
<tr>
<tr>
<td valign="top">
<form action ="upload.php" method = "post" enctype="multipart/form-data">
File:<input type="file" name ="file" size "30"><input type="submit" value="upload">
</form>
</td>
</tr>
</table>


Any suggestions would be much appricated

Thanks in Advance
 

tony811

macrumors newbie
Original poster
Feb 8, 2012
2
0
Thanks Darth , i just get a blank page and no messages the error has gone however i dont believe it is doing its function.
 

Darth.Titan

macrumors 68030
Oct 31, 2007
2,905
753
Austin, TX
  • You didn't enclose your if clause in curly braces on the extension check, so your script was exiting every time. Hence, no errors.
  • You mistyped the $_FILES['file']['tmp_name'] a couple of times.
Try this:
PHP:
<?php
$uploaddir = "uploads/images";
$allowed_ext = array(
	'jpg',
	'jpeg',	// It's a good idea to include this version of the jpeg extension as well.
	'JPG',
	'png',
	'gif'
);
$max_size = "500000000";
$max_width = "4000";
$max_height = "4000";


$info = pathinfo($_FILES['file']['name']);
$extension = $info['extension'];

if (!in_array($extension,$allowed_ext))
{
	print "Incorrect file extension !";
	exit;
}

if($_FILES['file']['size'] > $max_size)
{
	print "File size is too big !";
	exit;
}

if($max_width && $max_height)
{
	list($width, $height, $type, $w)=
		getimagesize($_FILES['file']['tmp_name']);

	if($width > $max_width || $height > $max_height)
	{
		print "File height and or width are too big ! ";
		exit;
	}
}

if (move_uploaded_file($_FILES['file']['tmp_name'],
	$uploaddir.'/'.$_FILES['file']['name']))
{
	print $_FILES['file']['name']." has been uploaded successfully !";
}

else
{
	print "There was a problem uploading ".$_FILES['file']['name'];
}
Hint: make sure your upload directory has the correct permissions set so the web server can write to it.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.