Jump to content


File Uploading Code [NOT WORKING]


11 replies to this topic

#1 Ben

    P2L Jedi Master

  • Publishing Betazoids
  • PipPipPipPip
  • 1,366 posts
  • Gender:Male
  • Location:VIC, Australia

Posted 10 July 2006 - 01:07 AM

Can someone please show me the problem in this code:

<?php include("db.php"); ?>

<form action="addf.php?act=add" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="50000000">
Upload File: <input name="frmFile" type="file"><br />
Version: <input type="text" name="version" id="version"><br />
Description: <input type="textarea" name="desc" id="desc"><br />
<input type="submit" value="Upload">
</form>

<?php

if($act == 'add') {

if(!(copy($_FILES['userfile']['tmp_name'], "/public_html/ArcaneLandsV2/client/" . $_FILES['userfile']['name']))) die("Unable to upload file. <a href='admin.php'>Return to admin panel</a>"); 

echo("File is valid, and was successfully uploaded. <a href='admin.php'>Return to admin panel</a>");
echo("<br /><br />Version: $version<br />Description: $description<br />File: $file<br />Date: $date");

$id = $_POST['id'];
$version = $_POST['version'];
$description = $_POST['desc'];
$file = basename($_FILES['userfile']['tmp_name']);
$date = date("jS F, Y");

mysql_query("INSERT INTO client (id, version, description, file, date) VALUES ('$id', '$version', '$description', '$file', '$date')") or die( mysql_error() );

}

?>


Everything works except it doesnt upload the file. The mysql query wasnt working and the file upload was, then I fixed the mysql query, and now the file upload doesnt work. If someone could help me I would be very grateful. This code has been extremely annoying. Thanks in advanced.

#2 coolaid

    P2L Jedi Master

  • Members
  • PipPipPipPip
  • 1,435 posts
  • Gender:Male
  • Interests:i wonder..

Posted 10 July 2006 - 01:14 AM

you're not suppose to use an exclamation point ( is not ) as a function, so remove the extra parenthesis you have :)
if(!copy($_FILES['userfile']['tmp_name'], "/public_html/ArcaneLandsV2/client/" . $_FILES['userfile']['name'])) die("Unable to upload file. <a href='admin.php'>Return to admin panel</a>");

Edited by coolaid, 10 July 2006 - 01:14 AM.


#3 Ben

    P2L Jedi Master

  • Publishing Betazoids
  • PipPipPipPip
  • 1,366 posts
  • Gender:Male
  • Location:VIC, Australia

Posted 10 July 2006 - 01:16 AM

Thanks, Ill try that out now :)

EDIT: Sorry, no luck. Heres my code now:
<?php include("db.php"); ?>

<form action="addf.php?act=add" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="50000000">
Upload File: <input name="frmFile" type="file"><br />
Version: <input type="text" name="version" id="version"><br />
Description: <input type="textarea" name="desc" id="desc"><br />
<input type="submit" value="Upload">
</form>

<?php

if($act == 'add') {

if(copy($_FILES['userfile']['tmp_name'], "/public_html/ArcaneLandsV2/client/" . $_FILES['userfile']['name'])) { 

echo("File is valid, and was successfully uploaded. <a href='admin.php'>Return to admin panel</a>");
echo("<br /><br />Version: $version<br />Description: $description<br />File: $file<br />Date: $date");

$id = $_POST['id'];
$version = $_POST['version'];
$description = $_POST['desc'];
$file = basename($_FILES['userfile']['tmp_name']);
$date = date("jS F, Y");

mysql_query("INSERT INTO client (id, version, description, file, date) VALUES ('$id', '$version', '$description', '$file', '$date')") or die( mysql_error() );

} else {

die("Unable to upload file. <a href='admin.php'>Return to admin panel</a>");

}

}

?>
Im only a newb at php, so dont blame me if I did something stupid :)

Edited by d4rkst0n3, 10 July 2006 - 01:21 AM.


#4 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 10 July 2006 - 02:01 AM

in your <form> tag you need to have enctype="multipart/form-data" otherwise it won't work.

Out of interest: $id = $_POST['id'];

you do not need to do that + there are so many security holes with that, if you set mysql up right then it will auto add an ID for you so you don't need that. And I don't even see where you are getting the ID from anyway, there is no input for it...

Matt

#5 Ben

    P2L Jedi Master

  • Publishing Betazoids
  • PipPipPipPip
  • 1,366 posts
  • Gender:Male
  • Location:VIC, Australia

Posted 10 July 2006 - 03:47 AM

Thankyou heaps! I remember taking that enctype line away because I didnt think Id need it, thats when it stopped uploading. And thanks for the tips :)

EDIT: Grr.. its still not working..
<?php include("db.php"); ?>

<form enctype="multipart/form-data" action="addf.php?act=add" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="50000000">
Upload File: <input name="frmFile" type="file"><br />
Version: <input type="text" name="version" id="version"><br />
Description: <input type="textarea" name="desc" id="desc"><br />
<input type="submit" value="Upload">
</form>

<?php

if($act == 'add') {

if(copy($_FILES['userfile']['tmp_name'], "/public_html/ArcaneLandsV2/client/" . $_FILES['userfile']['name'])) { 

echo("File is valid, and was successfully uploaded. <a href='admin.php'>Return to admin panel</a>");
echo("<br /><br />Version: $version<br />Description: $description<br />File: $file<br />Date: $date");

$version = $_POST['version'];
$description = $_POST['desc'];
$file = basename($_FILES['userfile']['tmp_name']);
$date = date("jS F, Y");

mysql_query("INSERT INTO client (version, description, file, date) VALUES ('$version', '$description', '$file', '$date')") or die( mysql_error() );

} else {

die("Unable to upload file. <a href='admin.php'>Return to admin panel</a>");

}

}

?>

Edited by d4rkst0n3, 10 July 2006 - 03:55 AM.


#6 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 10 July 2006 - 04:09 AM

what isn't working, the file upload or the adding query

==-====

hold on I can see it, I am just going for shower so will sort when i get back

Edited by deadly, 10 July 2006 - 04:09 AM.


#7 Ben

    P2L Jedi Master

  • Publishing Betazoids
  • PipPipPipPip
  • 1,366 posts
  • Gender:Male
  • Location:VIC, Australia

Posted 10 July 2006 - 04:25 AM

I said on the first post the file upload wasnt working. The query works fine.

#8 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 10 July 2006 - 04:48 AM

<?php include("db.php"); ?>

<form enctype="multipart/form-data" action="addf.php?act=add" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="50000000">
Upload File: <input name="userfile" type="file"><br />
Version: <input type="text" name="version" id="version"><br />
Description: <input type="textarea" name="desc" id="desc"><br />
<input type="submit" value="Upload">
</form>

<?php

if($act == 'add') {

	if ($_POST['submit']) {

		// state our variables

		$filename = $_FILES['userfile']['name'];
		$tmp_name = $_FILES['userfile']['tmp_name'];
		$file_size = $_FILES['userfile']['size'];
		$version = $_POST['version'];
		$description = $_POST['desc'];
		$maxsize = $_POST['MAX_FILE_SIZE'];
		$date = date("jS F, Y");
		$dir = "/public_html/ArcaneLandsV2/client/";

			if ($maxsize > $file_size) {
				
				if(copy($tmp_name, $dir.$filename)) {

					echo("File is valid, and was successfully uploaded. <a href='admin.php'>Return to admin panel</a>");
					echo("<br /><br />Version: $version<br />Description: $description<br />File: $filename<br />Date: $date");

					mysql_query("INSERT INTO client (version, description, file, date) VALUES ('$version', '$description', '$filename', '$date')") or die( mysql_error() );

				} else {

					die("Unable to upload file. <a href='admin.php'>Return to admin panel</a>");
			
				}

			} else {

				die("Unable to upload file - File to big!. <a href='admin.php'>Return to admin panel</a>");

			}

	}

}

?>

I havn't tested but should work, post any errors if you get any.

Matt

#9 Ben

    P2L Jedi Master

  • Publishing Betazoids
  • PipPipPipPip
  • 1,366 posts
  • Gender:Male
  • Location:VIC, Australia

Posted 10 July 2006 - 05:11 AM

Thanks for the retype but it still not working. Ive asked around a lot, and I've stumped a lot of php coders. The weird thing is now when i try to upload something, it doesnt say if it did or didnt work. It just loads the page the same as it was (you can try it here: http://www.remotesonline.com.au/ArcaneLand...admin/addf.php). It was working before, but when I got the query working, the file upload stopped. Thanks for your help, but nothing seems to work.

#10 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 10 July 2006 - 07:01 AM

Ok, a minor adjustment:
<?php include("db.php"); ?>

<form enctype="multipart/form-data" action="addf.php?act=add" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="50000000">
Upload File: <input name="userfile" type="file"><br />
Version: <input type="text" name="version" id="version"><br />
Description: <input type="textarea" name="desc" id="desc"><br />
<input type="submit" name="submit" value="Upload">
</form>

<?php
$act = $_GET['act'];

if($act == 'add') {

	if ($_POST['submit']) {

		// state our variables

		$filename = $_FILES['userfile']['name'];
		$tmp_name = $_FILES['userfile']['tmp_name'];
		$file_size = $_FILES['userfile']['size'];
		$version = $_POST['version'];
		$description = $_POST['desc'];
		$maxsize = $_POST['MAX_FILE_SIZE'];
		$date = date("jS F, Y");
		$dir = "/public_html/ArcaneLandsV2/client/";

			if ($maxsize > $file_size) {
				
				if(copy($tmp_name, $dir.$filename)) {

					echo("File is valid, and was successfully uploaded. <a href='admin.php'>Return to admin panel</a>");
					echo("<br /><br />Version: $version<br />Description: $description<br />File: $filename<br />Date: $date");

					mysql_query("INSERT INTO client (version, description, file, date) VALUES ('$version', '$description', '$filename', '$date')") or die( mysql_error() );

				} else {

					die("Unable to upload file. <a href='admin.php'>Return to admin panel</a>");
			
				}

			} else {

				die("Unable to upload file - File to big!. <a href='admin.php'>Return to admin panel</a>");

			}

	}

}

?>

Try that. I'm at school atm so i didnt check it out properly, but wait until i get home or maybe in IT later i will look at it better :)

ps. I would be very suprised if one of us php guys here cant get it working :) So dont worry :)

Edited by .Matt, 10 July 2006 - 07:02 AM.


#11 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 10 July 2006 - 05:31 PM

K.I.S.S. = Keep It Simple Stupid.
<?php include("db.php"); ?>

<form method="post" action="addf.php?act=add" enctype="multipart/form-data">
Upload File: <input name="frmFile" type="file"><br />
Version: <input type="text" name="version" id="version"><br />
Description: <input type="textarea" name="desc" id="desc"><br />
<input type="submit" name="sub" value="Upload">
</form>

<?php
if($_GET['act'] == 'add' && $_POST['sub']){
	$id = $_POST['id'];
	$version = $_POST['version'];
	$file = $_FILES['frmFile']['name'];
	$description = $_POST['desc'];
	$date = date('jS F, Y');
	if(!move_uploaded_file($_FILES['frmFile']['tmp_name'], '/public_html/ArcaneLandsV2/client/' . $file)){ // move_uploaded_file() php > 4.0.3
		if(!copy($_FILES['frmFile']['tmp_name'], '/public_html/ArcaneLandsV2/client/' . $file)) // copy() any other version
			die('Unable to upload file. <a href="admin.php">Return to admin panel</a>');
	}

	echo 'File is valid, and was successfully uploaded. <a href="admin.php">Return to admin panel</a>'
		.'<br /><br />Version: '.$version.'<br />Description: '.$description.'<br />File: '.$file.'<br />Date: '.$date;
	mysql_query("INSERT INTO client (id, version, description, file, date) VALUES ('$id', '$version', '$description', '$file', '$date')") or die(mysql_error());
}
?>
I'd list what all i did, but i'm to tired, so just let me know how it goes.

View Postcoolaid, on Jul 10 2006, 12:13 AM, said:

you're not suppose to use an exclamation point ( is not ) as a function, so remove the extra parenthesis you have :)
The point of the parentheses is for order-of-ops, not as a function. It just helps group things together... although, i never use parentheses just for a function as it's pointless. But when comparing stuff, then it makes more sense.

Edited by rc69, 10 July 2006 - 05:32 PM.


#12 Ben

    P2L Jedi Master

  • Publishing Betazoids
  • PipPipPipPip
  • 1,366 posts
  • Gender:Male
  • Location:VIC, Australia

Posted 11 July 2006 - 01:09 AM

View Post.Matt, on Jul 10 2006, 10:01 PM, said:

Ok, a minor adjustment:
<?php include("db.php"); ?>

<form enctype="multipart/form-data" action="addf.php?act=add" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="50000000">
Upload File: <input name="userfile" type="file"><br />
Version: <input type="text" name="version" id="version"><br />
Description: <input type="textarea" name="desc" id="desc"><br />
<input type="submit" name="submit" value="Upload">
</form>

<?php
$act = $_GET['act'];

if($act == 'add') {

	if ($_POST['submit']) {

		// state our variables

		$filename = $_FILES['userfile']['name'];
		$tmp_name = $_FILES['userfile']['tmp_name'];
		$file_size = $_FILES['userfile']['size'];
		$version = $_POST['version'];
		$description = $_POST['desc'];
		$maxsize = $_POST['MAX_FILE_SIZE'];
		$date = date("jS F, Y");
		$dir = "/public_html/ArcaneLandsV2/client/";

			if ($maxsize > $file_size) {
				
				if(copy($tmp_name, $dir.$filename)) {

					echo("File is valid, and was successfully uploaded. <a href='admin.php'>Return to admin panel</a>");
					echo("<br /><br />Version: $version<br />Description: $description<br />File: $filename<br />Date: $date");

					mysql_query("INSERT INTO client (version, description, file, date) VALUES ('$version', '$description', '$filename', '$date')") or die( mysql_error() );

				} else {

					die("Unable to upload file. <a href='admin.php'>Return to admin panel</a>");
			
				}

			} else {

				die("Unable to upload file - File to big!. <a href='admin.php'>Return to admin panel</a>");

			}

	}

}

?>

Try that. I'm at school atm so i didnt check it out properly, but wait until i get home or maybe in IT later i will look at it better :D

ps. I would be very suprised if one of us php guys here cant get it working :) So dont worry :D
Thanks for your help! It works fine now. I dont know why it wouldnt work before :S. Finally I can finish the website :P





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users