/    Sign up×
Community /Pin to ProfileBookmark

php ftp upload

I am using php to upload a file to an ftp server. I can establish a connection and it appears the file is being uploaded. However when I look at the file on the destination server, the file is 0 bytes. Here is the code I am using:

// variables
$ftp_server = ‘www.ftp-fileserver.com’;
$ftp_user_name = ‘username’;
$ftp_user_pass = ‘userpassword’;
$sourcefile = ‘c:foldernamefilename.gif’;
$destination_file = ‘filename.gif’;

// set up basic connection
$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);

Any help for this rookie is greatly appreciated.

Bob McP

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@tim_wilson2009Aug 07.2009 — What does your form look like?
Copy linkTweet thisAlerts:
@ryanlundAug 07.2009 — ok i think your problem is with these two lines:
[code=php]
//line 5
$sourcefile = 'c:foldernamefilename.gif';
//line 25
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
[/code]


look at the variable names for the source file....one has an underscore and one doesnt....try changing it and try again =)

Ryan
Copy linkTweet thisAlerts:
@JunkMaleAug 07.2009 — Here we go again, another one.

This is like the megaupload.com login download thingy someone else wanted and the other guy last month that wanted an FTP script...

Smells 'Kipper' to me.
Copy linkTweet thisAlerts:
@MindzaiAug 07.2009 — What's fishy about uploading via FTP using PHP? I do it every day.
Copy linkTweet thisAlerts:
@rbmsccauthorAug 07.2009 — tim---At this point, I am hard-coding the path and filename in the code to get that to work. Once I get the upload working, I will work with the variables in the form.

ryan----Ok, I will admit I feel stupid with that one. I looked at that code I thought thoroughly and never did catch that one. Sometimes it is good to have another set of eyes. Your change definitely needed changing. However, now I get the message

"FTP upload has failed" which I setup in an IF statement further down in the code below what I originally posted. Is there anything I need to check on the FTP server? I am using FTP through 1and1.com and I can use a FTP client to upload and download with no problem. Thanks

JunkMale---Not sure what you are talking about. I assure you this is a legitimate question for a legitimate web site project I am working on and I have researched and spent quite a bit of time on this trying to get it to work. I am not asking for someone to do it for me, I am trying to learn and want to understand. Thanks.
Copy linkTweet thisAlerts:
@MindzaiAug 07.2009 — Assuming you have the correct permissions need to write to the upload directory for the user you are conecting as, you may want to try using passive mode:

[code=php]ftp_pasv($conn_id, true);[/code]
Copy linkTweet thisAlerts:
@JunkMaleAug 07.2009 — What's fishy about uploading via FTP using PHP? I do it every day.[/QUOTE]
Apart from the obvious.
Copy linkTweet thisAlerts:
@rbmsccauthorAug 07.2009 — Update:

I think with your help and further testing, there is a problem with my source file syntax. My code is:

$source_file = 'c:foldernamefilename.gif';

$destination_file = 'filename.gif';

Then after connecting and log in I have:

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

Is there a problem with identifying the c: drive as the location of the source file? Since the script is executing on the server, is the identity of the c: drive causing confusion since the file is located on the local machine? Hope that makes some type of sense.
Copy linkTweet thisAlerts:
@MindzaiAug 07.2009 — Apart from the obvious.[/QUOTE]

I think you're going to have to spell it out for me, because I can't see what's remotely, let alone "obviously" odd or suspicious about asking how to use PHP's FTP functions?

Is there a problem with identifying the c: drive as the location of the source file? Since the script is executing on the server, is the identity of the c: drive causing confusion since the file is located on the local machine? Hope that makes some type of sense.[/QUOTE]

Are you saying that the file you are trying to upload is on your computer, but the script to FTP the file is on a remote server? Because that can never work. If the file doesn't exist on the machine on which the FTP script runs, the script is not going to be able to upload it, since it has no access to the file in question.
Copy linkTweet thisAlerts:
@rbmsccauthorAug 07.2009 — After I thought more about it, I could not figure out how it could work either.


I am working on a web site project in which there will be a form with information that is uploaded to a MySQL database. I have that part working. One box in the form that is put into the database is the name of a graphic file that will then be displayed in another web page. The displaying of the picture is a php file which reads the database table and creates the code to display the pictures and the information about each picture.

What I am wanting to do is to use a browse button on the form, select a file, and upload the selected file from my local computer to an ftp server.


How do web sites that allow users to upload a file set that up? Are they using something other than php? Or if anyone has a better method to do this, I appreciate that as well.

Bob McP
Copy linkTweet thisAlerts:
@MindzaiAug 07.2009 — Take a look at the sticky thread at the top of this forum.
Copy linkTweet thisAlerts:
@rbmsccauthorAug 07.2009 — Thanks

I did search previously, but somehow overlooked that one. I will do some studying of that one and try to learn from it.

Looks like another adventure in php'ing.
Copy linkTweet thisAlerts:
@JunkMaleAug 09.2009 — I think you're going to have to spell it out for me, because I can't see what's remotely, let alone "obviously" odd or suspicious about asking how to use PHP's FTP functions?[/QUOTE]

[SIZE="5"]O... b... v... i... o... u... s... l... y...[/SIZE] I couldn't have been clear about it... "Security"
×

Success!

Help @rbmscc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...