Jump to content

Archived

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

SieRobin

Array Help

Recommended Posts

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');

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[code]  $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;
}
} [/code]

Share this post


Link to post
Share on other sites
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:

[code]$real_extension = strrchr($filename, '.');[/code]

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

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.