Page 1 of 1

Uploading to an FTP

Posted: Sat Mar 11, 2006 7:32 pm
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

Posted: Sat Mar 11, 2006 7:37 pm
by feyd
work over the information in the manual to get a basic understanding and start. Post your code if you get stuck.

Posted: Sat Mar 11, 2006 8:06 pm
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

Posted: Sat Mar 11, 2006 8:16 pm
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.

Posted: Sun Mar 12, 2006 11:38 am
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

Posted: Sun Mar 12, 2006 11:50 am
by feyd
hint: echo $uploadfile

Posted: Sun Mar 12, 2006 12:46 pm
by CEJ
Aha! It works. Thanks a lot feyd. You've been a great help.

-CEJ