Uploading to an FTP

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
CEJ
Forum Newbie
Posts: 8
Joined: Tue Mar 07, 2006 9:39 pm

Uploading to an FTP

Post by CEJ »

Hello again.

This time my problem is a bit more involved, and I've done some research.

So, in a nutshell, this is what I need to do:
Upload a file from my computer to a webserver (using a script that is running on the webserver) and then have that file uploaded to another ftp server.

I did a little bit of research and found this thread:
viewtopic.php?t=19765&postdays=0&postor ... ad&start=0

However, being a novice, a lot of it doesn't make sense to me and there are a lot of stray ends.

Basically what I've gathered is that I can use an html form (?i think?) with a file input to upload the file to a temporary directory on the webserver (although I'm not quite sure how) and then upload it to the other ftp server (this I know how to do, and don't worry, I don't need to move it to a permanant directory because I want it to delete after the script is finished).

If someone could explain how to get this to work (in as laymen terms as you can make it) it would be great. It might even be the start of a tutorial for it you could write ;). But I really have to get this working, and I'll need all the help I can get!

Thanks,
-CEJ
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

work over the information in the manual to get a basic understanding and start. Post your code if you get stuck.
CEJ
Forum Newbie
Posts: 8
Joined: Tue Mar 07, 2006 9:39 pm

Post by CEJ »

Ok thanks a lot. I read through it and it was very informative ;).

So I basically just took the code that it gave as an example and tried getting it to work, but, of course, it doesn't work for me :P.

Here's what I have:

In uploadpart1.php:

Code: Select all

<html>
<body>
<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="uploadpart2.php" 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>
</body>
</html>
In uploadpart2.php:

Code: Select all

<?php
$directory = '/';
$uploadfile = $directory.basename($HTTP_POST_FILES['userfile']['name']);
echo '<pre>';
if(move_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'], $uploadfile)) {
   echo "File is valid, and was successfully uploaded.\n";
} else {
   echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($HTTP_POST_FILES);
print "</pre>";
?>
It returns:
Possible file upload attack!
Here is some more debugging info:Array
(
[userfile] => Array
(
[name] => test.txt
[type] => text/plain
[tmp_name] => /tmp/phpNjeLLv
[error] => 0
[size] => 20
)

)
I know there's probably some stupid mistake in there, but please, be patient :?

-CEJ
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you don't have write access to / (root) likely. Try getcwd() or $_SERVER['DOCUMENT_ROOT'].. whatever you use, it needs to be writable by PHP, which involves setting up permissions right. What permissions you'll need could vary from server to server, but for getting this working, 0777 is the most open setting.
CEJ
Forum Newbie
Posts: 8
Joined: Tue Mar 07, 2006 9:39 pm

Post by CEJ »

Ah yes, that was the problem.

Ok, so I've fixed that (by simply directing it to upload to the area on the webserver where I do have write access).

So now it says it uploads, and now I'm working on moving the file. I've tried the following:

Code: Select all

<?php
$directory = 'pathtomydirectory/users/cej';
$uploadfile = $directory.basename($HTTP_POST_FILES['userfile']['name']);
echo '<pre>';
if(move_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'], $uploadfile)) {
   echo "File is valid, and was successfully uploaded.\n";
} else {
   echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($HTTP_POST_FILES);
print "</pre>";
move_uploaded_file($HTTP_POST_FILES,pathtomydirectory/users/cej);
?>
However, the file doesn't appear in my folder :/.

Thanks,
-CEJ
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

hint: echo $uploadfile
CEJ
Forum Newbie
Posts: 8
Joined: Tue Mar 07, 2006 9:39 pm

Post by CEJ »

Aha! It works. Thanks a lot feyd. You've been a great help.

-CEJ
Post Reply