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

Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Upload a file

Post by Aravinthan »

Hi,
My guild asked recently started a in-guild league. And to ease up the leader's work I proposed to help him manage it. As he was doing everything manually changing points, adding game and all, I proposed to change the whole thing to databases. Everything went well, I've been able to add a way to add a player to the league, give him is win/defeat, give him is points. Update his recent games played. Now I am wondering how I can upload the recording of the games. Its a file that I would like to store in the database.
Here is the code I have so far:
THe form:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE>Insert New Game</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="?">
<form name="ladder Update" action="http://www.aoe3clan.com/index.php?na...1&file=update2" method="post" ENCTYPE="multipart/form-data">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
Game ID:
</td>
<td>
<input type="text" name="gameid">
</td>
</tr>
<tr>
<td>
Player 1:
</td>
<td>
<input type="text" name="player1">
</td>
</tr>
<tr>
<td>
Player 2:
</td>
<td>
<input type="text" name="player2">
</td>
</tr>
<tr>
<td>
Winner:
</td>
<td>
<input type="text" name="winner">
</td>
</tr>
<tr>
<td>
Map:
</td>
<td>
<input type="text" name="map">
</td>
</tr>
<tr>
<td>
Record File:
</td>
<td>
<input type="file" name="file" id="file" />
</td>
</tr>
<tr>
<tr>
<td>
<input type="submit" name="submit" value="Submit">
<input type="reset" name="reset" value="Reset">
</td>
</tr>
</table>
</form>
</BODY>
</HTML>
ANd the upload code:
<!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
$gameid = $_POST['gameid'];
$player1 = $_POST['player1'];
$player2 = $_POST['player2'];
$winner = $_POST['winner'];
mysql_connect("host", "username", "pass") or die(mysql_error());
mysql_select_db("aoe3clan_division1") or die(mysql_error());
$result = mysql_query("SELECT COUNT(*) AS count FROM games WHERE gameid='$gameid'") or die(mysql_error());
$row=mysql_fetch_assoc($result);
if ($row['count']>0){
echo "There is already a game with this id. <a href='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>";
mysql_close($result);

$tmpName = $_FILES[’SpecialFile’][’tmp_name’];
$filesize = $_FILES[’SpecialFile’][’size’];
$filetype = $_FILES[’SpecialFile’][’type’];

/*You can upload File in Folder **********************//////////////////
$new_image_name = "modules/Divison1/upload/".$_FILES['SpecialFile']['name'];

/**************** This new_image_name filename store in your db **********************/

if (move_uploaded_file ($_FILES['SpecialFile']['tmp_name'],$new_image_name)) {
echo 'File Uploaded Correctly....(Debugging)';
} else {
echo 'File NOT Uploaded Correctly....(Debugging (Check permissions))';
}


/*You can upload File in Folder **********************//////////////////
$fp= fopen($tmpName, ‘r’);
$content = addslashes($content);
fclose($fp);
$filename = $player1 . " vs " . $player2 . " on " . $map;
$result = mysql_query("INSERT INTO UploadedFiles (name) VALUES ('$filename')") or die(mysql_error());
//$result = mysql_query($query);

//Display Confirmation
print "</br> $filename has been succesfully uploaded</br>";
exit;
}


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.";
}
mysql_close($result);
$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>";
}
mysql_close($result);
$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>";
}
}
?>
</BODY>
</HTML>
The thing is that only the page title appears, and the page remains blank....
Thanks for your help,
Ara
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Upload a file

Post by requinix »

After the upload, do a View Source on the page and (if you can't see the problem there) post it.
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: Upload a file

Post by Aravinthan »

Ok... its pretty weird:
<!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="?">
No closing Body and Html tag......
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Upload a file

Post by requinix »

I don't see the problem; best guess is that one of the mysql_* functions is failing without an error.

Put a

Code: Select all

ini_set("display_errors", 1);
at the top of the (second) file and see if you get any error messages.

Otherwise:

Code: Select all

die("" . time());
Take that statement and stick it in the code. Move it around until you find a point where on one line you see a number and on the next you don't. It'll tell you where the problem is, hopefully that will be enough to tell what it is.
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: Upload a file

Post by Aravinthan »

After I put:
die("" . time());
After my upload script
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>";
mysql_close($result);

$tmpName = $_FILES[’SpecialFile’][’tmp_name’];
$filesize = $_FILES[’SpecialFile’][’size’];
$filetype = $_FILES[’SpecialFile’][’type’];

/*You can upload File in Folder **********************//////////////////
$new_image_name = "modules/Divison1/upload/".$_FILES['SpecialFile']['name'];

/**************** This new_image_name filename store in your db **********************/

if (move_uploaded_file ($_FILES['SpecialFile']['tmp_name'],$new_image_name)) {
echo 'File Uploaded Correctly....(Debugging)';
} else {
echo 'File NOT Uploaded Correctly....(Debugging (Check permissions))';
}


/*You can upload File in Folder **********************//////////////////
$fp= fopen($tmpName, ‘r’);
$content = addslashes($content);
fclose($fp);
$filename = $player1 . " vs " . $player2 . " on " . $map;
$result = mysql_query("INSERT INTO UploadedFiles (name) VALUES ('$filename')") or die(mysql_error());
//$result = mysql_query($query);

//Display Confirmation
print "</br> $filename has been succesfully uploaded</br>";
exit;
}
I get this:
Game 5 has been successfully inserted into the database. Details:
GameId: 5
Player1: 36Drew
Player2: Sporting_Lisbon
Winner:Sporting_Lisbon
Map:
File NOT Uploaded Correctly....(Debugging (Check permissions))
36Drew vs Sporting_Lisbon on has been succesfully uploaded
[/quote]
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Upload a file

Post by requinix »

Okay, so have you checked permissions then?
The upload directory needs 0??7, like 0777 = read/write/execute access for Other.
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: Upload a file

Post by Aravinthan »

Yh I gave the permission but I still get the same error message....
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Upload a file

Post by aceconcepts »

I'd be very careful with the permissions you set - 0777 ultimately allows anyone to execute in that directory. And if you're on a shred server then it's even worse.

Read up on the right permissions for you.
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: Upload a file

Post by Aravinthan »

Even with 0777 it doesnt work....
If it aint safe what permission should I give?
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: Upload a file

Post by infolock »

If you set the permissions to 777, it's not a permissions problem #1. It's a pathing issue.

Secondly, if you don't set it to 777, then you will either be able to write the directory and not be able to view the file, or you won't be able to write to the directory but can see the file (meaning your upload will fail).

Finally, the myth of setting a directory to 777 if false so long as you a) properly check the mime type of the file being uploaded, b) do a series of moving and renaming (move uploaded file to safe directory outside of htdocs, rename it to like an autoinc prefix, with the correct suffix).

Only users who can access your directory with FTP or SSH can harm your server, and if they can do that you're screwed anyways.

As it was so perfectly explained to me back in the day, setting 777 and worrying about it is like keeping the doors __INSIDE__ your house unlocked.


Edit: Also, check to make sure you set the directory where you want to upload the file to 777, and that it has the same GROUP name as the other directories before it. Otherwise, as stated before, you're path is wrong. As a matter of fact, try putting a ./ in the beginning of the path, and also echo out what dir() gives you. My suspicion is that you are gonna see a problem.
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: Upload a file

Post by Aravinthan »

dir() echo's out absolutely nothing......
I am thinking maybe its more of my coding that it's bugging? If someone can help me re-buid it, that would be awesome!


P.S- I would need a way to download those files also....
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: Upload a file

Post by infolock »

That's because dir isn't just a function by itself. See this Link

An easier way is to just call getcwd()
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: Upload a file

Post by Aravinthan »

echo getcwd() . "\n";
doesnt echo anything, so I was thinkin maybe there is a mistake here:
<?php
ini_set("display_errors", 1);
$gameid = $_POST['gameid'];
$player1 = $_POST['player1'];
$player2 = $_POST['player2'];
$winner = $_POST['winner'];
$link = mysql_connect ("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("aoe3clan_division1") or die(mysql_error());
$result = mysql_query("SELECT COUNT(*) AS count FROM games WHERE gameid='$gameid'") or die(mysql_error());
$row=mysql_fetch_assoc($result);
if ($row['count']>0){
echo "There is already a game with this id. <a href='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>";
mysql_close($result);

$tmpName = $_FILES[’SpecialFile’][’tmp_name’];
$filesize = $_FILES[’SpecialFile’][’size’];
$filetype = $_FILES[’SpecialFile’][’type’];

/*You can upload File in Folder **********************//////////////////
$new_image_name = "/www/modules/Divison1/upload/".$_FILES['SpecialFile']['name'];

/**************** This new_image_name filename store in your db **********************/

if (move_uploaded_file ($_FILES['SpecialFile']['tmp_name'],$new_image_name)) {
echo 'File Uploaded Correctly....(Debugging)';
} else {
echo 'File NOT Uploaded Correctly....(Debugging (Check permissions))';
}
Last edited by Aravinthan on Sat Nov 08, 2008 8:48 pm, edited 1 time in total.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: Upload a file

Post by infolock »

if getcwd doesn't echo anything out, you are either not making it to that line (indicating an error), or you are saving the echo statement in the wrong file (maybe you have a local copy and a remote and are getting them mixed up).

Also, in the example you show you are putting your database connection information in there.. never do that. change it now to be user/pass instead of the actual values.

Secondly, i don't see in there where you are echoing out the getcwd function. are you sure this isn't showing anything and that you aren't just overlooking it?
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: Upload a file

Post by Aravinthan »

This is the code:
/**************** This new_image_name filename store in your db **********************/

if (move_uploaded_file ($_FILES['SpecialFile']['tmp_name'],$new_image_name)) {
echo 'File Uploaded Correctly....(Debugging)';
} else {
echo 'File NOT Uploaded Correctly....(Debugging (Check permissions))';
}


/*You can upload File in Folder **********************//////////////////
$fp= fopen($tmpName, ‘r’);
$content = addslashes($content);
fclose($fp);
$filename = $player1 . " vs " . $player2 . " on " . $map;
$result = mysql_query("INSERT INTO UploadedFiles (name) VALUES ('$filename')") or die(mysql_error());
//$result = mysql_query($query);

//Display Confirmation
print "</br> $filename has been succesfully uploaded</br>";
exit;
}
echo getcwd() . "\n";
Post Reply