Forum Moderators: coopster

Message Too Old, No Replies

FTP with PHP doesn't work ? DO I wrong something ?

Somthing worng in the PHP script? Or something not well explained

         

Tiziano

9:28 pm on Jan 12, 2009 (gmt 0)

10+ Year Member



Dear all

I got a problem.
I have to implement a FTP system on the server of my website.
I realized a file named ftp.php that's in the directory /my so that to recall it I have to digit: http://www.example.com/my/ftp.php
This is just a test file but every time I call it, the answer is:

Warning: ftp_put(C:\bolero.mid) [function.ftp-put]: failed to open stream: No such file or directory in /web/htdocs/www.example.com/home/my/ftp.php on line 20.

The line 20 is the following one:
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);

The script is here below ( with domain username and password fake because I'm not here to make publicity of my website) ... the problem I think are the variables:

$source_file
$destination_file

But I tried many configuration of them ... for instance $source_file I tried with

"C:\bolero.mid";
"C:/bolero.mid";
"C:\\bolero.mid";
"C://bolero.mid";

even for the $destination_file I tried
"/bolero.mid";
"/my/bolero.mid";
"/web/htdocs/www.example.com/home/my/bolero.mid";

Without any positive result ... the answer stills the same ... could you help me please ? Where do I wrong ? I thank you very much in advance for the help

<?php

// set up basic connection
$ftp_server = "ftp.example.com";
$ftp_user_name = "username";
$ftp_user_pass = "passwd";
$source_file = "C:\\bolero.mid";
$destination_file = "/bolero.mid";
$conn_id = @ftp_connect($ftp_server);

// login with username and password
$login_result = @ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// check connection
if ((!$conn_id) ¦¦ (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}

// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);

// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}

// close the FTP stream
ftp_close($conn_id);
?>

[edited by: dreamcatcher at 7:33 am (utc) on Jan. 13, 2009]
[edit reason] use example.com. Thanks. [/edit]

surrealillusions

11:08 am on Jan 13, 2009 (gmt 0)

10+ Year Member



$source_file = "C:\\bolero.mid";
$destination_file = "/bolero.mid";

The source file is on C: drive, which is a local computer path. Make sure its a web based path, like

$source_file = "sources/bolero.mid"

You need to check that the paths are correct and the files are actually there.

:)

Tiziano

11:21 am on Jan 13, 2009 (gmt 0)

10+ Year Member



Ah ok ... thank you for the correction ... ;-) ... and if I would like to transfer a local file from my pc to my site via php ftp script ... is it possible ?

surrealillusions

3:28 pm on Jan 13, 2009 (gmt 0)

10+ Year Member



Im sure it is possible...theres an upload form you could use instead of building your own ftp program but the form is limited to one file at a time and anyone can use it unless its on a password protected page/area of the site.

But i dont know how to do this off the top of my head.

:)

Tiziano

3:37 pm on Jan 13, 2009 (gmt 0)

10+ Year Member



Oh it's good for me ... the important is to give this possibility and to learn how to create it ... where can I find it please ?

Tiziano

8:16 pm on Jan 13, 2009 (gmt 0)

10+ Year Member



... Sorry ... where can I find it please ?

coopster

7:58 pm on Jan 14, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Handling file uploads [php.net] describes how to use PHP to upload a file.

penders

10:23 pm on Jan 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



... and if I would like to transfer a local file from my pc to my site via php ftp script ... is it possible ?

As regards FTP... not really, at least probably not the way you are thinking(?) The FTP extension within PHP allows you to connect as an FTP client to an FTP server. Perhaps from your web server to a server elsewhere. If you were running your PHP/FTP app in a web server on your local machine then yes you could send/upload local files to a remote FTP server (providing you 'local' files are within your webspace).

However, regular file uploads are usually handled by the HTTP protocol - see coopster's post.

Tiziano

10:32 pm on Jan 14, 2009 (gmt 0)

10+ Year Member



... I understand ... it's a pity ... because I have to open a socket FTP for my users ... because in the normal way in which php is working, we have limitations due my provider ... there is no way to override them ... my provider told me clearly: you need to use ftp ... but I cannot give any credentials to my suers because it's impossibile ... and I have not the controls of the files .. anyway it's too complicated to explain ... I have to thank you very much for the help ... I will give up ;-)