HELP WITH UPLOAD SCRIPT! PLEASE HELP ME!

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
mickhal
Forum Newbie
Posts: 6
Joined: Wed Sep 10, 2008 9:16 am

HELP WITH UPLOAD SCRIPT! PLEASE HELP ME!

Post by mickhal »

I can't seem to get this script working. It connects to my database successfully, but after I upload something, it doesn't appear in my folder, which is avatars. You can access the upload form at
http://yamoto-studios.com/upload.php
The form is for people to upload their own avatars after they register at my forums which is at:
http://yamoto-studios.com/forum

And the upload form doesnt give a confirmation that it succesfully uploaded a picture. Here is the code im using:

<head>
<title>Avatar Upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>
<body bgcolor="#FFFFFF" text="464646" link="#468DD5" vlink="#468DD5" alink="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<?
//include dBase connect
include('./db.php');

//user defined variables
$sizelim = "yes"; //Do you want size limit, yes or no
$size = $avatarSize; //What do you want size limited to be if there is one
$maxHeight = $avatarHeight; //set the max height
$maxWidth = $avatarWidth; //set max width
$avatarPath = $abpath . "/avatars";
// Connect to database
$link = dbConnect();

if($action == ""){
?>
<table width="450" border="0" cellspacing="0" cellpadding="10">
<tr>
<td><center>
<font size="4" face="Verdana, Arial, Helvetica, sans-serif"><b>Avatar Upload</b></font>
</center><br>
<font face="Verdana, Arial, Helvetica, sans-serif">Image must be a .JPG
file<br>
Dimensions can't exceed <? echo $avatarWidth." wide x ".$avatarHeight." tall pixels"; ?><br>
File size can't be over <? echo $avatarSize; ?> bytes</font><br>
<form method="POST" action="upload.php" enctype="multipart/form-data">
<p><font face="Arial, Helvetica, sans-serif">Image to upload:</font><br>
<input type="file" name="img1" size="30"><br>
<input type="submit" name="submit" value="Upload">
<input type="hidden" name="action" value="uploadImage">
</form>
<br>
<center>
<font size="2" face="Arial, Helvetica, sans-serif"><a href="javascript:self.close()">close
window</a></font>
</center></td>
</tr>
</table>
<?
exit;
}
//all image types to upload
$cert1 = "image/jpeg"; //Jpeg type 1

$log = "";
$posted = time();

//begin upload
//check dimensions
$image_info = getimagesize($img1);
$img1_type = $image_info['mime'];
//compare uploaded image to our variables
if(($image_info[1]>$maxHeight) or ($image_info[0]>$maxWidth)){
$log.= "Dimensions are greater than $maxWidth x $maxHeight<br>";
}
//checks if file exists
if ($img1_name == "") {
$log.= "No file selected for upload<br>";
}
/*
if ($img1_name != "") {
//checks if file exists
if (file_exists("$avatarPath/$img1_name")) {
$log.= "File already existed<br>";
}
}
*/
//checks if files to big
if ($sizelim == "yes") {
if ($img1_size > $size) {
$log.= "Image file size was too big<br>";
}
}

//if we have errors, send them to the user and exit
if($log!=""){
echo "<table width=\"450\" border=\"0\" cellspacing=\"0\" cellpadding=\"10\">";
echo "<tr>";
echo "<td>";
echo "<center><font size=\"4\" face=\"Verdana, Arial, Helvetica, sans-serif\"><b>Avatar Upload</b></font></center><br>";
echo "<font face=\"Arial, Helvetica, sans-serif\"><b><u>Results:</u></b><br>$log<br><br><br><center><a href=\"javascript:history.back(-1)\"><< go back and try again</a><br><br><font face=\"Arial, Helvetica, sans-serif\" size=\"2\"><a href=\"javascript:self.close()\">close window</a></font></center></font>";
echo "</td>";
echo "</tr>";
echo "</table>";
exit;
}

//Checks if file is an image
if ($img1_type == $cert1) {

$img1_name = "$userdetails[0]" . "-" . $posted . ".jpg";
@copy($img1, "$avatarPath/$img1_name") or $log .= "Couldn't copy image to server<br>";

//make the avatar link for the db
$avatarLink = "$installDirectory"."avatars/"."$img1_name";

// set avatar location using the username cookie
$query = "UPDATE forumUsers SET avatarURL = '$avatarLink' WHERE username = '$userdetails[0]'";
// Execute query
if(!mysql_query($query)) {
echo mysql_error($link);
}
if($log == ""){
$log .= "<b>Image was uploaded!</b><br>";
}
}else{
$log .= "File is not a non-progressive JPG image<br>";
}
// Close link to database server
mysql_close($link);
?>
<table width="450" border="0" cellspacing="0" cellpadding="10">
<tr>
<td>
<center><font size="4" face="Verdana, Arial, Helvetica, sans-serif"><b>Avatar Upload</b></font></center>
<br>
<?
echo "<font face=\"Arial, Helvetica, sans-serif\"><b><u>Results:</u></b><br>$log<br><br><br><center><a href=\"javascript:self.close()\">close window</a></center></font>";



Someone help me please!
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

Re: HELP WITH UPLOAD SCRIPT! PLEASE HELP ME!

Post by Darkzaelus »

You have put the check for if it was uploaded or not after it is displayed on the screen, you haven't wrapped your form variables in $_POST[''] tag, or $_FILE[''] tags in this case, i will paste the new code below. The correct usage for getting the mime type is $_FILES['img1']['type'].

Code: Select all

 
<head>
<title>Avatar Upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>
<body bgcolor="#FFFFFF" text="464646" link="#468DD5" vlink="#468DD5" alink="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<?
//include dBase connect
include('./db.php');
 
//user defined variables
$sizelim = true; //Do you want size limit, yes or no
$size = $avatarSize; //What do you want size limited to be if there is one
$maxHeight = $avatarHeight; //set the max height
$maxWidth = $avatarWidth; //set max width
$avatarPath = $abpath . "/avatars";
// Connect to database
$link = dbConnect();
 
if($action){
?>
<table width="450" border="0" cellspacing="0" cellpadding="10">
<tr>
<td><center>
<font size="4" face="Verdana, Arial, Helvetica, sans-serif"><b>Avatar Upload</b></font>
</center><br>
<font face="Verdana, Arial, Helvetica, sans-serif">Image must be a .JPG
file<br>
Dimensions can't exceed <? echo $avatarWidth." wide x ".$avatarHeight." tall pixels"; ?><br>
File size can't be over <? echo $avatarSize; ?> bytes</font><br>
<form method="POST" action="upload.php" enctype="multipart/form-data">
<p><font face="Arial, Helvetica, sans-serif">Image to upload:</font><br>
<input type="file" name="img1" size="30"><br>
<input type="submit" name="submit" value="Upload">
<input type="hidden" name="action" value="uploadImage">
</form>
<br>
<center>
<font size="2" face="Arial, Helvetica, sans-serif"><a href="javascript&#058;self.close()">close
window</a></font>
</center></td>
</tr>
</table>
<?
exit;
}
//all image types to upload
$cert1 = "image/jpeg"; //Jpeg type 1
 
$log = "";
$posted = time();
 
//begin upload
//check dimensions
$image_info = $_FILES['img1'];
$img1_type = $image_info['type'];
$img1_name = $image_info['name'];
$img1_dimen = getimagesize($image_info['tmp_name']);
$img1_size = filesize($image_info['tmp_name']);
//compare uploaded image to our variables
if(($img1_dimen[1]>$maxHeight) or ($img1_dimen[0]>$maxWidth)){
$log.= "Dimensions are greater than $maxWidth x $maxHeight<br>";
}
//checks if file exists
if (!$img1_name) {
$log.= "No file selected for upload<br>";
}
/*
if ($img1_name != "") {
//checks if file exists
if (file_exists("$avatarPath/$img1_name")) {
$log.= "File already existed<br>";
}
}
*/
//checks if files to big
if ($sizelim) {
if ($img1_size > $size) {
$log.= "Image file size was too big<br>";
}
}
 
//if we have errors, send them to the user and exit
if($log){
echo "<table width=\"450\" border=\"0\" cellspacing=\"0\" cellpadding=\"10\">";
echo "<tr>";
echo "<td>";
echo "<center><font size=\"4\" face=\"Verdana, Arial, Helvetica, sans-serif\"><b>Avatar Upload</b></font></center><br>";
echo "<font face=\"Arial, Helvetica, sans-serif\"><b><u>Results:</u></b><br>$log<br><br><br><center><a href=\"javascript&#058;history.back(-1)\"><< go back and try again</a><br><br><font face=\"Arial, Helvetica, sans-serif\" size=\"2\"><a href=\"javascript&#058;self.close()\">close window</a></font></center></font>";
echo "</td>";
echo "</tr>";
echo "</table>";
exit;
}
 
//Checks if file is an image
if ($img1_type == $cert1) {
 
$img1_name = "$userdetails[0]" . "-" . $posted . ".jpg";
@copy($image_info, "$avatarPath/$img1_name") or $log .= "Couldn't copy image to server<br>";
 
//make the avatar link for the db
$avatarLink = "$installDirectory"."avatars/"."$img1_name";
 
// set avatar location using the username cookie
$query = "UPDATE forumUsers SET avatarURL = '$avatarLink' WHERE username = '$userdetails[0]'";
// Execute query
if(!mysql_query($query)) {
echo mysql_error($link);
}
if(!$log){
$log .= "<b>Image was uploaded!</b><br>";
}
}else{
$log .= "File is not a non-progressive JPG image<br>";
}
// Close link to database server
mysql_close($link);
?>
<table width="450" border="0" cellspacing="0" cellpadding="10">
<tr>
<td>
<center><font size="4" face="Verdana, Arial, Helvetica, sans-serif"><b>Avatar Upload</b></font></center>
<br>
<?
echo "<font face=\"Arial, Helvetica, sans-serif\"><b><u>Results:</u></b><br>$log<br><br><br><center><a href=\"javascript&#058;self.close()\">close window</a></center></font>";
 
I haven't tested it completly, so it might need work. Oh yes, true/false for $sizelim is a bit more sensible :P

Cheers, Darkzaelus
mickhal
Forum Newbie
Posts: 6
Joined: Wed Sep 10, 2008 9:16 am

Re: HELP WITH UPLOAD SCRIPT! PLEASE HELP ME!

Post by mickhal »

hmmm, the code isn't working. The upload still does the same thing as before...
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

Re: HELP WITH UPLOAD SCRIPT! PLEASE HELP ME!

Post by Darkzaelus »

Do you have an IDE and access to the apache error logs? They prove so useful when debugging.

Cheers, Darzkaelus
mickhal
Forum Newbie
Posts: 6
Joined: Wed Sep 10, 2008 9:16 am

Re: HELP WITH UPLOAD SCRIPT! PLEASE HELP ME!

Post by mickhal »

I don't know what that is.
mickhal
Forum Newbie
Posts: 6
Joined: Wed Sep 10, 2008 9:16 am

Re: HELP WITH UPLOAD SCRIPT! PLEASE HELP ME!

Post by mickhal »

Want me to give you the source files for my forum so you can try and fix it? Because there are some stuff that's wrong with it. You can't go to a thread directly, it keeps saying " No thread specified" and you can't upload images in threads either. These are the main problems that's wrong with it.
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

Re: HELP WITH UPLOAD SCRIPT! PLEASE HELP ME!

Post by Darkzaelus »

Send it over in private messages if possible. Will look at it in about an hour.

Cheers, Darkzaelus
mickhal
Forum Newbie
Posts: 6
Joined: Wed Sep 10, 2008 9:16 am

Re: HELP WITH UPLOAD SCRIPT! PLEASE HELP ME!

Post by mickhal »

Can anyone fix the script?
Post Reply