Uploading Files

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

Post Reply
daniworldwide
Forum Newbie
Posts: 14
Joined: Mon May 26, 2003 12:30 pm
Location: Colorado

Uploading Files

Post by daniworldwide »

I'm making a game out of php online... I'm trying to get it so you can upload files onto the game, and the name of that file would be the $hid.

Code: Select all

<form enctype="multipart/form-data" action="upload.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="100000">
<input type="hidden" name="hid" value="$hid">
Send this file: <input name="$hid" type="file">
<input type="submit" value="Send File">
</form>
The $hid is already posted on the page, thats just that part of the code. That part of the code works fine. What I want it to do, though, is have the name be $hid. I am not farmiliar with the upload php script, so I don't know how to make it with the code I got from php.net.

Code: Select all

<?php
$hid = $_POST['hid'];

// In PHP earlier then 4.1.0, $HTTP_POST_FILES should be used instead of $_FILES.
// In PHP earlier then 4.0.3, use copy() and is_uploaded_file() instead of move_uploaded_file

$uploaddir = '/images/';

print "<pre>";
if (move_uploaded_file($_FILES['$hid']['tmp_name'], $uploaddir . $_FILES['$hid']['name'])) {
    print "File is valid, and was successfully uploaded.  Here's some more debugging info:\n";
    print_r($_FILES);
} else {
    print "Possible file upload attack!  Here's some debugging info:\n";
    print_r($_FILES);
}
?>
mod_edit: enabled bbcode and changed

Code: Select all

to

Code: Select all

for the second block[/size]
What would I have to change in there for this to work? $uploaddir = '/images/'; and on that, can you make it go to another site. I own another site but it doesn't host php, and I dont want to use too much storage on this site... Would that be possible?

Thanks, Dani
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

<input type="hidden" name="hid" value="$hid">
how is this printed?
If you left the php-block (as the code-block suggests) you have to re-enter it to print the value instead of having $hid added to the output

Code: Select all

<?php
	//some php code here
?>
<form enctype="multipart/form-data" action="upload.php" method="post">
	<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
	<input type="hidden" name="hid" value="<?php echo $hid; ?>" />
	Send this file: <input name="upload" type="file" />
	<input type="submit" value="Send File" />
</form>
in the receiving
I set the name of the <input type="file">-element to upload. There's really no need to use a dynamic name for that. The name ships with the hidden field.

Code: Select all

<?php
...
if (move_uploaded_file($_FILES['upload']['tmp_name'], $uploaddir . $hid)) { 
...
?>
if you e.g. have ftp access to the other box you might tranfer it via php_ftp
take a look at http://www.php.net/ftp
Post Reply