Jump to content


Photo

Array Help


  • Please log in to reply
8 replies to this topic

#1 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 11 July 2006 - 02:05 AM

Anyone find anything wrong with this array? The script isn't doing it what I ask it to, was before but now it just magically doesn't work.

$extension=array('image/gif', 'image/pjpeg', 'image/x-png');

#2 Prismatic

Prismatic
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego

Posted 11 July 2006 - 02:12 AM

Post your code please

and use the Code tags when you do  :P

#3 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 11 July 2006 - 02:16 AM

My code is very jumbled.. what it is, is a picture upload script, it worked great before but now it's doing what it did before.. instead of putting the extension at the end of the picture name, it puts "array". I modified the script to output the users ID number and the extension. Like I said, it worked before, but now it doesn't.. and I never altered the code after it did actually work. So my user ID number is 1 therefore if the picture I uploaded was a jpg it would read as 1.jpg. You asked for the code, don't expect amazement lol.

#4 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 11 July 2006 - 02:17 AM

  	$player=$_SESSION['player'];
	$userstats="select * from users where playername='$player'";
	$userstats2=mysql_query($userstats) or die("Could not query players");
	$userstats3=mysql_fetch_array($userstats2);
	if($_POST['upload']) {
		if($_FILES['image']['name'] == "")
		{
   		 	echo "<table class='table' align='center'><tr class='headline'><td><center>Upload Image</center></td></tr>
		<tr class='mainrow'><td><center>Please select a file to upload!<br>
			 [<a href='settings.php'>Settings</a>]</center></td></tr></table>";
   		 	exit;
		}
		$extension=array('image/gif', 'image/pjpeg', 'image/x-png');
		$file="userimages/$userstats3[ID]$extension";
		if(!in_array($_FILES['image']['type'], $extension))
		{
   		 	echo "<table class='table' align='center'><tr class='headline'><td><center>Upload Image</center></td></tr>
		<tr class='mainrow'><td><center>That file type is not allowed!<br>
			 [<a href='settings.php'>Settings</a>]</center></td></tr></table>";
   		 	exit;
		}
		$max_filesize = 102400;
		$max_filesize_kb = ($max_filesize / 1024);

		if($_FILES['image']['size'] > $max_filesize)
		{
    		echo "<table class='table' align='center'><tr class='headline'><td><center>Upload Image</center></td></tr>
		<tr class='mainrow'><td><center>Your file is too large, files may be up to $max_filesize_kb kb.<br>
			 [<a href='settings.php'>Settings</a>]</center></td></tr></table>";
    		exit;
		}
		$imagesize = getimagesize($_FILES['image']['tmp_name']);
		$imagewidth = $imagesize[0];
		$imageheight = $imagesize[1];
		$maxwidth = 500;
		$maxheight = 500;
		if($imagewidth > $maxwidth || $imageheight > $maxheight)
		{
    		 echo "<table class='table' align='center'><tr class='headline'><td><center>Upload Image</center></td></tr>
			 <tr class='mainrow'><td><center>Your file is too large, files may be up to $maxwidth px x $maxheight px in size.<br>
			 [<a href='settings.php'>Settings</a>]</center></td></tr></table>";
   			 exit;
		}
		if ($userstats3['uimage']=="") {
		move_uploaded_file($_FILES['image']['tmp_name'], $file)
		or die ("Couldn't upload ".$_FILES['image']['name']."\n");
		mysql_query("UPDATE users set uimage='$file' where ID='$userstats3[ID]'");
		echo "<table class='table' align='center'><tr class='headline'><td><center>Upload Image</center></td></tr>
		<tr class='mainrow'><td><center>Your image has been successfully uploaded.<br>
			 [<a href='settings.php'>Settings</a>]</center></td></tr></table>";
			 exit;
		} else {
		echo "<table class='table' align='center'><tr class='headline'><td><center>Upload Image</center></td></tr>
		<tr class='mainrow'><td><center>You already have an image uploaded, delete your current image if you'd like a new image.<br>
			 [<a href='settings.php'>Settings</a>]</center></td></tr></table>";
			 exit;
		}
	}


#5 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 11 July 2006 - 07:57 PM

No one eh? K thanks.

#6 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 11 July 2006 - 08:10 PM

the reason is exactly as it says - $extension is an array, as you can see in the line you were asking about.  to get the right extension, you'd have to figure out what the file's extension is, most likely with strrchr:

$real_extension = strrchr($filename, '.');

and replace $extension with $real_extension when you define $file.  note that you'll also have to replace $filename with whatever contains the file's actual name.

#7 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 11 July 2006 - 08:20 PM

Ok, so what I have to do is replace $extension with $real_extension when I define $file, but what exactly do I do with extension?

#8 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 11 July 2006 - 08:30 PM

I think I've got it :] thank you

#9 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 11 July 2006 - 08:33 PM

no problem.

for the record, $extension is the array of file types you deem acceptable, and is used in the if() immediately following $file's definition.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users