Help with ftp upload please

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I am using a form to upload an image from my hard drive to my server but there
is something amiss cause I am continually getting an error message "FTP upload
has failed"

This is my code

if (!isset($_POST['upload'])) {
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p align="left"><font size="2">Type the file to upload here &nbsp;</p>
<input type="text" name="filename" size="25">
<input type="submit" value="Upload" name="upload" style="color: #000000;
font-family: Arial; background-color: #D56A00; border-style: solid;
border-color: #FF0066"></p>
else {
$source = "D:\My Documents\My Pictures\" . $_POST['filename'];
$destination = "/images/".$_POST['filename'];
$ftp_server = "myftpserver";
$ftp_user_name = "myusername";
$ftp_user_pass = "mypassword";
$conn_id = ftp_connect($ftp_server) or die ("Could not connect to $ftp_server");
$login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass);
if((!$conn_id)||(!$login_result)) {
echo "FTP connection has failed";
else {
echo "Connected to FTP server";
$upload = ftp_put($conn_id,$destination,$source,FTP_BINARY);
if (!$upload) {
echo "FTP upload has failed";
else {
echo "Successfully uploaded file";

Any help greatly appreciated as always

Re: Help with ftp upload please


You don't have to use the PHP ftp_* commands to upload an image from
your local PC to the server. All you need is your PHP script to receive
the posted file contents (along with original filename and MIME Type)
and then handle it suitably (either save to database or write to

Please refer for
a good description on how this is achieved using PHP.

So your code should be (untested, heck not even executed once!):

Remove leading //-. Included so that GG does not screw up indentation

//-if (empty($myfile)) {
//-    <form method="post" action="insert.php"
//-      File: <input name="myfile" type="file"><br />
//-      <input type="submit" value="Submit"><br />
//-      <input type="hidden" name="MAX_FILE_SIZE" value="30000">
//-    </form>
//-} else {
//-        $Home = '/home/unixuser/public_html/images' ;
//-        if (is_uploaded_file($myfile)) {
//-            $move_status = @move_uploaded_file($myfile,
//-           if ($move_status) {
//-    <h5>File Uploaded successfully</h5>
//-           } else {
//-    <h5 style="color:red">File upload failed</h5>
//-           }

And dont forget about the addslashes() function. You image might be
already escaped if your magic_quotes_gpc option is set in php.ini.

Re: Help with ftp upload please

*** Dynamo wrote/escribió (13 Jan 2005 05:23:12 -0800):
Quoted text here. Click to load it

I've seen many people trying to read a local file from a script running in
a remote server. Please note that this code...

Quoted text here. Click to load it

....implies that the web server is running in your PC. Is it the case?

-- Álvaro G. Vicario - Burgos, Spain
-- Thank you for not e-mailing me your questions

Site Timeline