Upload a file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: Upload a file

Post by infolock »

It's because you are missing a curly brace at the end to close the last else statement. Right now you only have 2 at the end, make it 3 and it will work. Thus why proper indention pays off ;)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Upload a file

Post by John Cartwright »

It's also helpful to post error messages or output, instead of saying it simply isn't working :)
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Upload a file

Post by mmj »

Code: Select all

mysql_close();
if ($winner == $player1) {
  $result = mysql_query("SELECT * FROM player_stats WHERE name='$player1'") or die(mysql_error());
If you use mysql_close then you have to open it again.

Just put mysql_close at the end of the script.
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: Upload a file

Post by Aravinthan »

It gives me this error:
Game 13 has been successfully inserted into the database. Details:
GameId: 13
Player1: 36Drew
Player2: Sporting_Lisbon
Winner:Sporting_Lisbon
Map:
Access denied for user 'aoe3clan'@'localhost' (using password: NO)
Meaning that the game was inserted but the players weren't updated....
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Upload a file

Post by Syntac »

Looks like your authentication failed.
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: Upload a file

Post by Aravinthan »

Yeah but, I used the same connection for the both, I jsut changed the database name....
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: Upload a file

Post by Aravinthan »

OK I have been able to fix that error, but My file isnt getting uploaded to my mysql tables:

Code: Select all

 
<?php
$gameid = $_POST['gameid'];
$player1 = $_POST['player1'];
$player2 = $_POST['player2'];
$winner = $_POST['winner'];
$fileName= $player1 . ' vs ' . $player2 . ' on ' . $map;
$tmpName  = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileType = $_FILES['file']['type'];
 
$link = mysql_connect ("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("aoe3clan_division1") or die(mysql_error());
$sql="SELECT COUNT(*) AS count
        FROM games
        WHERE gameid='$gameid'";
$result=mysql_query($sql);
$row=mysql_fetch_assoc($result);
 
if ($row['count']>0){
   echo "There is already a game with this id. <a href='http://www.aoe3clan.com'>Home</a>";
   }
else {
  mysql_query("INSERT INTO games (gameid, player1, player2, winner, map) VALUES ('$gameid', '$player1', '$player2', '$winner', '$map')") or die(mysql_error());
  print "Game $gameid has been successfully inserted into the database. Details:</br>GameId: $gameid</br> Player1: $player1</br> Player2: $player2</br> Winner:$winner</br> Map:$map</br>";
 
$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
 
if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}
 
include 'library/config.php';
include 'library/opendb.php';
 
$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
 
mysql_query($query) or die('Error, query failed');
include 'library/closedb.php';
 
echo "File $fileName uploaded<br>";
 
This is what I get:
Game 653 has been successfully inserted into the database. Details:
GameId: 653
Player1: 36Drew
Player2: Sporting_Lisbon
Winner:Sporting_Lisbon
Map:
File 36Drew vs Sporting_Lisbon on uploaded
36Drew has successfully received 1 points. View the Ladder
Sporting_Lisbon has successfully received 3 points. View the Ladder
Normally the file should be uploaded but I when I try to output all the files:

Code: Select all

 
mysql_select_db ("aoe3clan_division1", $link);
$Result = mysql_query("SELECT * FROM upload where name like '36Drew'  order by id DESC",$link)
or die(mysql_error());
while ($Row = mysql_fetch_row($Result))
{
 
printf ("<tr><td align='center'>%s</td><td align='center'>%s</td><td align='center'>%s</td></tr>",
$Row[0], $Row[1], $Row[3]);
}
?>
 
There is nothing that gets displayed....
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Upload a file

Post by mmj »

You shouldn't be using addslashes.

Post all of the current code.
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: Upload a file

Post by Aravinthan »

Ok here it is:

Code: Select all

 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE>Player Updated</TITLE>
<META NAME="Generator" CONTENT="TextPad 4.6">
<META NAME="Author" CONTENT="?">
<META NAME="Keywords" CONTENT="?">
<META NAME="Description" CONTENT="?">
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#FF0000" VLINK="#800000" ALINK="#FF00FF" BACKGROUND="?">
<?php
ini_set("display_errors", 1);
$gameid = $_POST['gameid'];
$player1 = $_POST['player1'];
$player2 = $_POST['player2'];
$winner = $_POST['winner'];
$fileName= $player1 . ' vs ' . $player2 . ' on ' . $map;
$tmpName  = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileType = $_FILES['file']['type'];
 
$link = mysql_connect ("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("aoe3clan_division1") or die(mysql_error());
$sql="SELECT COUNT(*) AS count
        FROM games
        WHERE gameid='$gameid'";
$result=mysql_query($sql);
$row=mysql_fetch_assoc($result);
 
if ($row['count']>0){
   echo "There is already a game with this id. <a href='http://www.aoe3clan.com'>Home</a>";
   }
else {
  mysql_query("INSERT INTO games (gameid, player1, player2, winner, map) VALUES ('$gameid', '$player1', '$player2', '$winner', '$map')") or die(mysql_error());
  print "Game $gameid has been successfully inserted into the database. Details:</br>GameId: $gameid</br> Player1: $player1</br> Player2: $player2</br> Winner:$winner</br> Map:$map</br>";
 
$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
 
if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}
 
include 'library/config.php';
include 'library/opendb.php';
 
$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
 
mysql_query($query) or die('Error, query failed');
include 'library/closedb.php';
 
echo "File $fileName uploaded<br>";
echo $size;
echo $type;
 
if ($winner == $player1) {
  $result = mysql_query("SELECT * FROM player_stats WHERE name='$player1'") or die(mysql_error());
  while($row = mysql_fetch_array( $result )) {
    $oldp1 = $row['points'];
    $newp1 = '3';
    $point = $oldp1 + $newp1;
    $oldgp1 = $row['gp'];
    $newgp1 = '1';
    $gp = $oldgp1 + $newgp1;
    $oldwins1 = $row['wins'];
    $newwins1 = '1';
    $wins1 = $oldwins1 + $newwins1;
    mysql_query("UPDATE player_stats SET points='$point', gp='$gp', wins='$wins1' WHERE name='$player1'") or die(mysql_error());
    print "$player1 has successfully received $newp1 points.";
  }
 
  $result = mysql_query("SELECT * FROM player_stats WHERE name='$player2'") or die(mysql_error());
  while($row = mysql_fetch_array( $result )) {
    $oldp2 = $row['points'];
    $newp2 = '1';
    $point = $oldp2 + $newp2;
    $oldgp2 = $row['gp'];
    $newgp1 = '1';
    $gp = $oldgp1 + $newgp1;
    $oldloss2 = $row['loss'];
    $newloss2 = '1';
    $loss2 = $oldloss2 + $newloss2;
    mysql_query("UPDATE player_stats SET points='$point', gp='$gp', defeats='$loss2' WHERE name='$player2'") or die(mysql_error());
    print "$player2 has successfully received $newp2 points. ";
  }
}else {
  $result = mysql_query("SELECT * FROM `player_stats` WHERE name='$player1'") or die(mysql_error());
  while($row = mysql_fetch_array( $result )) {
    $oldp1 = $row['points'];
    $newp1 = '1';
    $point = $oldp1 + $newp1;
    $oldgp1 = $row['gp'];
    $newgp1 = '1';
    $gp = $oldgp1 + $newgp1;
    $oldloss1 = $row['defeats'];
    $newloss1 = '1';
    $loss1 = $oldloss1 + $newloss1;
    mysql_query("UPDATE player_stats SET points='$point', gp='$gp', defeats='$loss1' WHERE name='$player1'") or die(mysql_error());
    print "$player1 has successfully received $newp1 points. <a href='http://www.aoe3clan.com/test/ladder.php'> View the Ladder</a>";
  }
 
  $result = mysql_query("SELECT * FROM player_stats WHERE name='$player2'") or die(mysql_error());
  while($row = mysql_fetch_array( $result )) {
    $oldp2 = $row['points'];
    $newp2 = '3';
    $point = $oldp2 + $newp2;
    $oldgp2 = $row['gp'];
    $newgp2 = '1';
    $gp = $oldgp2 + $newgp2;
    $oldwins2 = $row['wins'];
    $newwins2 = '1';
    $wins2 = $oldwins2 + $newwins2;
    mysql_query("UPDATE player_stats SET points='$point', gp='$gp', wins='$wins2' WHERE name='$player2'") or die(mysql_error());
    print "</br>$player2 has successfully received $newp2 points. <a href='http://www.aoe3clan.com/test/ladder.php'> View the Ladder</a>";
  }
}
}
mysql_close();
?>
</BODY>
</HTML>
 
User avatar
chopsmith
Forum Commoner
Posts: 56
Joined: Thu Nov 13, 2008 10:40 am
Location: Red Bank, NJ, USA

Re: Upload a file

Post by chopsmith »

quick note, like someone said earlier, you need a closing "}" at line 37 (I assume).

Lines 48 and 49. Think it's better to use include("fileName") rather than include 'fileName'. Have you checked to make sure the includes are working?

Line 52. I think you should the values portion should be (and at many places throughout the code):

Code: Select all

 
"VALUES ('{$fileName}', '{$fileSize}', '{$fileType}', '{$content}')"; //note the {} (or can use . appropriately)
 
Just some thoughts. Didn't go through too thoroughly. Hope it's helpful.
User avatar
chopsmith
Forum Commoner
Posts: 56
Joined: Thu Nov 13, 2008 10:40 am
Location: Red Bank, NJ, USA

Re: Upload a file

Post by chopsmith »

Also, have you checked to see if the file actually exists on the system now? And, have you looked at the database to see if there are any records or anything?
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: Upload a file

Post by Aravinthan »

I did all you told me chopsmith, now it stays blank. THe page doesnt get loaded, not even the title. Here is the code that is showed for this page:

Code: Select all

 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=utf-8"></HEAD>
<BODY></BODY>
<META content=DLWIXOQLDS name=SKYPE_FRAMEID>
<META content=DLWIXOQLDS name=SKYPE_FRAMEID></HTML>
 


And how can I check if the file exists? I am echoing the file size and type. So I am guessing if file size>0 that means there something that got loaded no?
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Upload a file

Post by mmj »

chopsmith wrote:quick note, like someone said earlier, you need a closing "}" at line 37 (I assume).
No, actually he isn't missing a close brace. :wink:
chopsmith wrote:Lines 48 and 49. Think it's better to use include("fileName") rather than include 'fileName'. Have you checked to make sure the includes are working?
Not exactly correct. The latter's use is more widespread (including the php manual).
chopsmith wrote:Line 52. I think you should the values portion should be (and at many places throughout the code):

Code: Select all

 
"VALUES ('{$fileName}', '{$fileSize}', '{$fileType}', '{$content}')"; //note the {} (or can use . appropriately)
 
Its helps a bit with readability and is better when using assoc arrays but shouldn't really make a difference.

@OP:

Speaking of good practice:
1. don't use addslashes, read up on it if you want to know why. use mysql_real_escape_string.
2. use sprintf for queries. better for security and readability.

since you are using or die on all your queries then they must be going through. Use something like phpmyadmin to see whats going on in the db.
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: Upload a file

Post by Aravinthan »

I get this: Error QUery failed.....
what is the problem. And I used phpmyadmin and found out that some data inserted in the db.
And I would like to know now is is this section of the code correct(its for displaying the datas and allowing viewrs to download it):

Code: Select all

 
$Result = mysql_query("SELECT * FROM upload where name like '36Drew'  order by id DESC",$link)
or die(mysql_error());
while ($Row = mysql_fetch_row($Result))
{
 
printf ("<tr><td align='center'>%s</td><td align='center'>%s</td><td align='center'>%s</td></tr>",
$Row[0], $Row[1], $Row[3]);
}
?>
 
What I want to do is show the record files that contains 36Drew in it.... is it possible?
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Upload a file

Post by mmj »

Aravinthan wrote:I get this: Error QUery failed.....
what is the problem. And I used phpmyadmin and found out that some data inserted in the db.
And I would like to know now is is this section of the code correct(its for displaying the datas and allowing viewrs to download it):

Code: Select all

 
$Result = mysql_query("SELECT * FROM upload where name like '36Drew'  order by id DESC",$link)
or die(mysql_error());
while ($Row = mysql_fetch_row($Result))
{
 
printf ("<tr><td align='center'>%s</td><td align='center'>%s</td><td align='center'>%s</td></tr>",
$Row[0], $Row[1], $Row[3]);
}
?>
 
What I want to do is show the record files that contains 36Drew in it.... is it possible?

Code: Select all

SELECT * FROM upload WHERE name LIKE '%36Drew%' ORDER BY id DESC
Post Reply