Jump to content

Fairly basic php question.


pinacoladaxb

Recommended Posts

I'm sure I could find this information by doing a google search, but I honestly don't know what to search for. I'm trying to make a script that interacts with an application I'm making. What I need is an upload script. The application finds the file and connects to the script. For example: www.flufferfluff.com/games/battles/upload.php?file=C:\Documents and Settings\Laima\My Documents\image.bmp. Every time I search for the answer, I get an example with a form in it. Like this:

 

<!-- The data encoding type, enctype, MUST be specified as below -->

<form enctype="multipart/form-data" action="__URL__" method="POST">

<!-- MAX_FILE_SIZE must precede the file input field -->

<input type="hidden" name="MAX_FILE_SIZE" value="30000" />

<!-- Name of input element determines name in $_FILES array -->

Send this file: <input name="userfile" type="file" />

<input type="submit" value="Send File" />

</form>

 

How would I modify that? Thanks.

Link to comment
Share on other sites

Actually, here's an even better script I found (only requires one php file):

 

<form enctype="multipart/form-data" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="2048000">
File: <input name="userfile" type="file" /><br />
<input type="submit" value="Upload" />
</form> <?php
if (@is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
copy($_FILES["userfile"]["tmp_name"], "images/" . $_FILES["userfile"]["name"]);
echo "<p>File uploaded successfully.</p>";
}
?> 

 

...but it still uses a form.

Link to comment
Share on other sites

Well, I guess I may as well give you the full story.

 

I'm making a game that includes a level editor. Since all of the files are going to be stored online, I need an upload script. For each level made, 2 files are uploaded: an array and a screenshot. I plan to upload the array in /games/battles/levels and the screenshot in /games/battles/images. I really don't know how to further explain this situation.

Link to comment
Share on other sites

What do you mean by "an array is uploaded"?  And for the image uploading, you need to basically use the form that you have already found (notice the encytype attribute on the form tag!), and you need to do stronger file upload checks and store the path to the image in the database.

Link to comment
Share on other sites

Aah! Slow down. I'm not that smart. I'm new to all of this.

 

By an "array," I mean a file with data stored in it. It can be opened with notepad, but under its properties, it says: Type of file: File.

 

You say I can use the same code I found? How do I change it? What do you mean by "you need to do stronger file upload checks and store the path to the image in the database." I am really lost here.

Link to comment
Share on other sites

Let me sort of plot this out for you so you can see what I mean, then we can get to code after you understand this:

 

1) You present a form to the user.  It has 2 upload spots: 1 file, and 1 image.

 

2) The user selects the files and submits.

 

3) On the PHP side, you take these files, make sure they're of the appropriate type and size, and move them to their final directory, because uploaded files are put in a "temporary" directory until you move them.

 

4) Take the path name to the file and the image and store them in the database in the same row, so they're associated.  Then, they can have an ID that corresponds to certain level, if you want.

 

Link to comment
Share on other sites

I actually understand all of that, but step 4 isn't needed. I already have that sorted out in my app. The thing is, the user shouldn't ever see the upload page. In fact, the user may not even know that the level is being uploaded. It should be hidden.

 

EDIT: The way the game finds the level image is by getting $Player_Name . "&" . $Level_Name . ".bmp". Getting the array is almost the same thing, just without the ".bmp." Of course, it isn't php I am using, but you get how it works. The "&" is used as a delimiter.

Link to comment
Share on other sites

Well, with the software I use, it's not pure scripting. You are able to use pre-made C++ scripts that can be submitted by anyone, so there are hundreds of them. One that I am using is an array script. It is able to create a file that stores the data and interprets the file so that I can easily make a loop to add the game elements to the X and Y coordinates of the game map corresponding to the array file. If you want to see how it looks, I made a demo that I sent to a few friends:

http://www.pauliukonis.com/miscfiles/The%20Battles%20of%20Fluffer%20Fluff%20Meadows%20-%20Level%20Editor.exe

Link to comment
Share on other sites

YES.  ::) lol. I didn't think I was asking for much. I'll give you some screenshots in a second.

 

By the way, I can make a Java file out of it, but my game uses specific windows directories and uses some scripts that can't be run on macs. Sorry.

Link to comment
Share on other sites

Screenshots:

 

Level selection screen:

edt1.PNG

 

Submitting a level (for inclusion in the final game. This has nothing to do with what we're talking about.)

edt2.PNG

 

Making a level:

edt3.PNG

 

So there you go. That's not what the game looks like. Just the demo.

Link to comment
Share on other sites

YES.  ::) lol. I didn't think I was asking for much. I'll give you some screenshots in a second.

 

By the way, I can make a Java file out of it, but my game uses specific windows directories and uses some scripts that can't be run on macs. Sorry.

 

Ok, I see what you are attempting to do, and if your game allows a place to call php code, you can use php to call FTP functions to send files to the server in the background, without a form having to be submitted.  Contrary to the message title, this is not a basic question.

 

To point you in the right direction, checkout www.php.net and specifically search for FTP to see what I mean.

 

 

Link to comment
Share on other sites

I've tried many things, but none seem to work to upload something to my server. Am I doing this right? Here's something I tried:

 

<?
$name = "hi.txt";
$filename = "C:\Documents and Settings\Laima\My Documents\hi.txt";

//-- Code to Transfer File on Server Dt: 06-03-2008 by Aditya Bhatt --//
//-- Connection Settings
$ftp_server = "pauliukonis.com"; // Address of FTP server.
$ftp_user_name = "pauliuko"; // Username
$ftp_user_pass = "32443rewfdf"; // Password
$destination_file = ""; //where you want to throw the file on the webserver (relative to your login dir)

$conn_id = ftp_connect($ftp_server) or die("<span style='color:#FF0000'><h2>Couldn't connect to $ftp_server</h2></span>"); // set up basic connection

$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass) or die("<span style='color:#FF0000'><h2>You do not have access to this ftp server!</h2></span>"); // login with username and password, or give invalid user message

if ((!$conn_id) || (!$login_result)) { // check connection
// wont ever hit this, b/c of the die call on ftp_login
echo "<span style='color:#FF0000'><h2>FTP connection has failed! <br />";
echo "Attempted to connect to $ftp_server for user $ftp_user_name</h2></span>";
exit;
} else {
//echo "Connected to $ftp_server, for user $ftp_user_name <br />";
}

$upload = ftp_put($conn_id, $destination_file.$name, $filename, FTP_BINARY);// upload the file
if (!$upload) {// check upload status
echo "<span style='color:#FF0000'><h2>FTP upload of $filename has failed!</h2></span> <br />";
} else {
echo "<span
style='color:#339900'><h2>Uploading $name Completed
Successfully!</h2></span><br /><br />";
}
ftp_close($conn_id); // close the FTP stream

?>

 

EDIT: I forgot to censor my password. Glad I caught that!!  :D

 

EDIT 2: Here's the page I got the code from:

http://us2.php.net/manual/en/function.ftp-put.php

Link to comment
Share on other sites

Don't apologize. You're the one helping me.

 

There is no forum. Just an upload. Like I said, it is possible that the user will never know their levels are stored online if they aren't very bright.

On the game side, the user makes a level. When they save it, the game brings the array file and screenshot to http://flufferfluff.com/games/battles/upload.php. Then, the script uploads the files from the user's computer. The way the script knows where the files are located is they are always located in the SAME spot. For example, they could be always located in C:\Program Files\Game_Title\levels. I just have no idea how to do that. The script I found should do the trick, but I think I did something wrong.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.