Uploading a local file to site FTP server using PHP
Moderator: General Moderators
Uploading a local file to site FTP server using PHP
Dear All
Can anyone tell me briefly how to upload a file from my computer (local file) to my remote FTP server for my site, using PHP.
For all of the resources I have looked at on this subject (including my PHP and MySQL Manual, and online), none of them seem to specifically cover this point, more transferring files from one remote server to another, or they don't really clarify exactly what direction they pertain to be an 'upload' or a 'download'.
I am using the file path and name given by a FILE input object, the contents of which have been posted from a form on the previous page.
Thank you very much for any help
Yours sincerely,
Mark
Can anyone tell me briefly how to upload a file from my computer (local file) to my remote FTP server for my site, using PHP.
For all of the resources I have looked at on this subject (including my PHP and MySQL Manual, and online), none of them seem to specifically cover this point, more transferring files from one remote server to another, or they don't really clarify exactly what direction they pertain to be an 'upload' or a 'download'.
I am using the file path and name given by a FILE input object, the contents of which have been posted from a form on the previous page.
Thank you very much for any help
Yours sincerely,
Mark
The following code I have obtained is unfortunately producing an error. 'target.bin' is I am assuming the destination file, and $_POST['photo1_upload'] is the contents of the FILE input object on a form on the previous page. This variable contains the pathname of the file local to my computer, which I want to upload to my FTP server to the file 'target.bin'.
Is reports that parameter 3 in ftp_put is an invalid FTP resource.
Can anyone interpret this problem?
Many thanks
Mark
Is reports that parameter 3 in ftp_put is an invalid FTP resource.
Can anyone interpret this problem?
Code: Select all
if(!$success = ftp_put($conn, 'target.bin', $_POSTї'photo1_upload'], FTP_BINARY))
{
echo 'Error: Could not upload file to server';
ftp_quit($conn);
exit();
}Mark
-
coreycollins
- Forum Commoner
- Posts: 67
- Joined: Sun Feb 01, 2004 1:04 pm
- Location: Michigan
-
coreycollins
- Forum Commoner
- Posts: 67
- Joined: Sun Feb 01, 2004 1:04 pm
- Location: Michigan
Let me know if this helps... This is how I did it and it works like a champ:
Code: Select all
<?
if(isset($_POST["action"]))
{
if($_POST["action"] == "upload")
{
// the file uploaded from the form
$filename = $HTTP_POST_FILES['myfile']['tmp_name'];
// set up basic connection
$conn_id = ftp_connect("ftp.coreycollins.com");
// login with username and password
$login_result = ftp_login($conn_id, "user", "password");
// upload a file
if (ftp_put($conn_id, "test.txt", $filename, FTP_ASCII)) {
echo "successfully uploaded $filename\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
}
}
?>Code: Select all
<FORM action="ftpUpload.php" method="post" enctype="multipart/form-data">
<INPUT Type="hidden" name="action" value="upload">
<INPUT TYPE="file" NAME="myfile" SIZE=30> <BR>
<INPUT TYPE="submit" NAME="Upload File" Value="Upload File">
</FORM>Corey - I'm using $_FILES['photo1_upload']['tmp_name'] but echo tells me it's empty. $HTTP_POST_FILES I don't think is used anymore under my version of PHP.
As I said I'm a newbie to uploading - can you tell me what tmp_name is, and the mechanics of downloading and uploading? What is the direction of uploading under PHP - my computer to the server on which my site is stored (and on which PHP is running), or the server on which my site is stored (and on which PHP is running) to my computer? Confusion!
Mark
As I said I'm a newbie to uploading - can you tell me what tmp_name is, and the mechanics of downloading and uploading? What is the direction of uploading under PHP - my computer to the server on which my site is stored (and on which PHP is running), or the server on which my site is stored (and on which PHP is running) to my computer? Confusion!
Mark
-
coreycollins
- Forum Commoner
- Posts: 67
- Joined: Sun Feb 01, 2004 1:04 pm
- Location: Michigan
Don't change the myfile and tmpname. Those are PHP file functions. Here is a link that talks about them:
http://www.php.net/features.file-upload
http://www.php.net/features.file-upload
Right, the following code seems to work:
However, whilst the /tmp/test.mp3 file appears in the directory, and the script seems to successfully find local file $filename on my computer, the following messages are produced by the code:
Is there any obvious error in my approach?
Thanks very much
Mark
Code: Select all
$conn = ftp_connect("$host");
if(!$conn)
{
echo 'Error: could not connect to FTP server. Please try again later.';
exit();
}
echo "Connect to $host.<br />";
// log in to host
@ $result = ftp_login($conn, $user, $pass);
if(!$result)
{
echo "Error: Could not log on as $user<br />";
ftp_quit($conn);
exit();
}
echo "Logged in as $user.";
echo "Uploading file:";
$filename = $_POSTї'photo1_upload'];
echo $filename;
if(!$success = ftp_get($conn, '/tmp/test.mp3', $filename, FTP_BINARY))
{
echo 'Error: Could not upload file to server';
ftp_quit($conn);
exit();
}
echo 'File downloaded successfully';
ftp_quit($conn);Whilst the file /tmp/test.mp3 appears, it is 0 bytes in size and the code produces this error output.Connect to ftp.XXXX.biz.
Logged in as XXXX.Uploading file:D:\\BreakXL7.mp3
Warning: ftp_get(): Error opening /tmp/test.mp3 in \\XXXX\domains\s\XXXX.biz\user\htdocs\XXXX.php on line 2405
Error: Could not upload file to server
Is there any obvious error in my approach?
Thanks very much
Mark
-
coreycollins
- Forum Commoner
- Posts: 67
- Joined: Sun Feb 01, 2004 1:04 pm
- Location: Michigan
My guess is the file is still on your computer. The reason I did it like I did above is because you have to send the file to the web server where the php code is getting executed. I don't think this is going to work for the file name:
You're going to most likely do the form and the submission like I had above.
Code: Select all
$filename = $_POST['photo1_upload'];-
southeastweb
- Forum Newbie
- Posts: 14
- Joined: Sun Mar 21, 2004 3:28 am
- Location: Florida
I might be able to help
Do you want to upload via a web interface from your computer? What types of files are you looking to transfer?
J
J