We hope you enjoy your visit.

You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Image upload error; PHP
Topic Started: Feb 3 2007, 10:28 AM (551 Views)
Friiks
Member Avatar
I have lost the will to live, simply nothing more to give
[ *  * ]
Well, no questions I have except for this - could anyone tell me whats wrong with my code?
Code:
 
<?php
session_start();
include 'config.php';
$photo=$_POST['pic'];
$file = $_FILES['userfile']['tmp_name'];
$valid = array (".png",".jpg",".jpeg",".gif",".ico",".tif");
////////////////////////////////////////
$nameext= $_FILES['userfile']['name'];
$ext = strtolower(strrchr($nameext, "."));
$namei=$_SESSION['username'].$ext;
////////////////////////////////////////
if ($photo=='avatar'){
$path='images/avatars/'.$namei;}
elseif ($photo=='propic'){
$path='images/photos/'.$namei;}

$image_dimensions = getimagesize($file);
$height = $image_dimensions[1];
$width = $image_dimensions[0];

if ($photo=='avatar'){

if (in_array($ext, $valid){
if (($width>64) || ($height>64)){
$text='Image dimensions are too big';
header('Location: index.php?i=avatar');
}else{
if(copy($file, $path))
{
mysql_query("UPDATE `users` SET avatar='".$path."' WHERE id='".$_SESSION['id']."'");
header('Location: index.php?i=usrinfo&id='.$_SESSION["id"].'');
}
else{
header('Location: index.php?i=avatar');
}
}
else{
header('Location: index.php?i=avatar');
}
}

}

if ($photo=='photo'){

if (in_array($ext, $valid){
if (($width>175) || ($height>200)){
header('Location: index.php?i=avatar');
}else{
if(copy($file, $path))
{
mysql_query("UPDATE `users` SET avatar='".$path."' WHERE id='".$_SESSION['id']."'");
header('Location: index.php?i=usrinfo&id='.$_SESSION["id"].'');
}
else{
header('Location: index.php?i=avatar');
}
}
else{
header('Location: index.php?i=avatar');
}
}

}
?>


And here's the form
Code:
 

<form action="avaup.php" method="POST" enctype="multipart/form-data">
Select a file <input type="file" name="userfile" size="18"><br>
<select name='pic' class='select'>
<option value='avatar'>Avatar</option>
<option value='propic'>Profile picture</option>
</select><br>
<input type='submit' name='submit' value='Submit'>
</form>

When I submit the form it just gives me a blank page..so I just don't know whats wrong with it =/

Thanks, Matt.
Offline Profile Quote Post Goto Top
 
Ben
Member Avatar
Quantum-locked when observed.

Make sure that your form is has the enctype="multipart/form-data" attribute, or else file uploads won't work through it. You may want to post the form's code as well for us to see. :)

Oh, and instead of copy() I might avail myself of move_uploaded_file(). You can see PHP.net's page on handling file uploads and Practical PHP Programming's page on handling file uploads for more information.
Offline Profile Quote Post Goto Top
 
Friiks
Member Avatar
I have lost the will to live, simply nothing more to give
[ *  * ]
The form is with correct enctype, know that much about upload forms. Um and it's not copy() as I tried move_uploaded_file() and still nothing. Could it be the headers?
I really don't know whats wrong and I'm really tired of it after 4hours messing with it..
Offline Profile Quote Post Goto Top
 
IceMetalPunk
Member Avatar
We are all IMPerfect. Be proud!
[ *  *  *  *  *  * ]
It's hard to follow all your IF-ELSE conditions, but it seems like there's a case in which the location header isn't sent. I'd use the header() call at the end, OUTSIDE of all conditions, and use the conditions only to set a variable used as the location. This way, there will always be a redirection, and your problem should be solved.

Plus, setting the URL variable to something like http://www.google.com in the begining of the code will let you know if the current situation isn't matching one of your conditions (i.e. if the variable isn't changed in any conditions, it'll go to google instead of some other page).

-IMP ;) :)
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Technology Chat · Next Topic »
Add Reply