Hello, I am having problems with the ftp_put() function. I can login via shell prompt.

Does anyone know why the ftp_put() function may not work? I went to the php site and followed the directions. I am connecting because I am not getting an error. I am also logging in with the username and password because I am not getting an error here as well. But so far no matter what I do, the file will not upload.

Any help with this would be great.

$query	= "select Server, UserName, Password, Destination_Path, ServerType from company";

$result	= @mysql_query($query);

$row	= @mysql_fetch_array($result);

if (!empty($row["Server"]))
{
$hostip = gethostbyname($row["Server"]);
$conn_id = ftp_connect($hostip);

// set up basic connection
//$conn_id = ftp_connect($row["Server"]);

// login with username and password
$login_result = ftp_login($conn_id, $row["UserName"], $row["Password"]);

// check connection
if ((!$conn_id) || (!$login_result))
{
$value = "FTP connection has failed!<br>
Attempted to connect to ".$row["Server"]." for user ".$row["UserName"];
exit;
}

// IMPORTANT!!! turn passive mode on
//tried this and it did not help
//ftp_pasv ( $conn_id, true );





//parameters are ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY)

$upload = ftp_put($conn_id, $row["Destination_Path"]."/".strtolower(trim($HTTP_POST_FILES['userfile']['name'])), strtolower(trim($HTTP_POST_FILES['userfile']['name'])), FTP_BINARY);

if (!$upload)
echo $upload."<br><br>Did not upload to ftp server.";

// close the FTP stream
ftp_close($conn_id);
}

Thanks

    Before anybody wastes any time on this, help us out by removing the '@' signs and using die() statements and using mysql_error(). Then execute it again and tell us what happens.

      I have no MYSQL errors, I have even echoed my $row fields and the query executes, I get a result and I have the results I am looking for.

      With that said, it is now not a waste of your time. 😉

        OK. You've told us what isn't happening, but you haven't really told us what IS happening. What output are you getting? Any errors?

        I'd really like to be able to help. Some debugging echo statements also might be useful.

          Ok, what is happening is pretty much nothing 🙂

          I have echoes if I don't connect, or do not get a successful login and I have tested those. I seem to be connecting and logging in fine. But the ftp_put() call shows the echo "Did not upload to ftp server."

          I do not know what else to tell you as far as what is happening. I am not getting any errors and the file is not getting uploaded.

          -my server var holds the URl of the server ex. 'test.com'

          • my destination file var holds the path temp

          so the destination path when I echo it is temp/my file

          so the ftp_put() is getting passed

          ftp_put($conn_id, 'temp/myfile', 'myfile', ftp_binary);

          This is the first time I have ever used this function so please bear with me. I have researched it and no one seems to have this problem. Most people complain that the file seems to get uploaded but its file size is 0. Right now, my page runs and nothing happens. My permissions on the server are correct, the folder I am trying to upload to exists, the username and pass are correct and I can login via shell prompt as well.

          Any ideas?

            Shouldn't you be using $HTTP_POST_FILES['userfile']['tmp_name'] instead of $HTTP_POST_FILES['userfile']['name']?

              Originally posted by kburger
              Shouldn't you be using $HTTP_POST_FILES['userfile']['tmp_name'] instead of $HTTP_POST_FILES['userfile']['name']?

              You know, I am not sure. Like I said this function is new to me. I thought I would pass in the actual file name I am uploading .

                Have you tried hard-coding it while testing to see what happens?

                $upload = ftp_put($conn_id, 'temp/myfile', '/home/username/myfile', FTP_BINARY);

                  Yeah. The file I am uploading is local obviously so in the ftp_put() I am just passing in the filename say "test.txt' for the source file.

                  I may be screwing up the destination or remote path. Not sure. I have tried these:

                  //The folder I am trying to upload to on my server once logged in is public_html/temp

                  so when I pass in my destination parameter into ftp_put() I have tried these

                  "public_html/temp/test.txt"
                  "/public_html/temp/test.txt"
                  "temp/test.txt"
                  "/temp/test.txt"
                  "test.txt"
                  and finally "/test.txt"

                  I have also tried swapping the $HTTP_POST_FILES['userfile']['name'] with $HTTP_POST_FILES['userfile']['tmp_name'] and it did not help

                  this is basically what I am passing in now, I have hardcoded everything but my filename.

                  $upload = ftp_put($conn_id, public_html/temp/".strtolower(trim($HTTP_POST_FILES['userfile']['name'])), strtolower(trim($HTTP_POST_FILES['userfile']['name'])), FTP_BINARY);

                  Any ideas? Thank you again for your time

                    5 days later

                    Stop bumping, it is impolite.

                    The source file for ftp_put should be $FILES['userfile']['tmp_name'] and the destination file should be $FILES['userfile']['name']

                      laserlight wrote:

                      Stop bumping, it is impolite.

                      The source file for ftp_put should be $FILES['userfile']['tmp_name'] and the destination file should be $FILES['userfile']['name']

                      Oh really? It is impolite?? Would you rather I just repost it over and over again??? I have been using forums long enough to know that the latter is impolite.

                      Anyone else have a comment on this? I would love to hear it. Am I wrong for simply typing bump after my post is a few pages deep as opposed to just reposting it? If the problem has not been solved for me, then I still need answers. And I can promise you, most people do not view or help posts that are 6 to 12 pages deep. You will also notice that there is at least a day if not 2 or 3 days between each bump. I am not posting recreationally, I occasionally get stuck on things and it is for my job so I can not just forget about it!!!

                      BTW your solution did not help with my problem.

                      :mad:

                        Oh really? It is impolite?? Would you rather I just repost it over and over again??? I have been using forums long enough to know that the latter is impolite.

                        Both are impolite - just be patient and wait, eventually someone will answer if they find it interesting. Of course, you could bump by adding more content, e.g. what else you tried in the meantime.

                        And I can promise you, most people do not view or help posts that are 6 to 12 pages deep.

                        Unnecessary bumping increases the number of posts in a thread for no good reason.

                          laserlight wrote:

                          Unnecessary bumping increases the number of posts in a thread for no good reason.

                          I don't get it. Are you truly bothered by the int increase of a post count?? Seriously how big of an issue is this and why are we talking about it?? I mean come on, we are talking about a few bytes here. You are making something out of nothing.

                          And I am doing the research and posting as I find more info. What is funny is your post complaining about bump increased the count and took up more resources than "Bump" did.

                          Now people have to read past this crap just to get back to my problem that still exists. Thanks

                            Are you truly bothered by the int increase of a post count?? Seriously how big of an issue is this and why are we talking about it??

                            It's not an issue really - I wouldnt even have mentioned it if not because I happened to have worked with PHP's FTP functions and decided to lend you a hand, and mention the anti-bumping netiquette in passing.

                            What is funny is your post complaining about bump increased the count and took up more resources than "Bump" did.

                            I take it that you completely ignored my suggestion on what to use for the source and destination filenames?

                            If you are so unreceptive to my help, then so be it.

                              I did not ignore your solution...

                              tripwater wrote:

                              BTW your solution did not help with my problem.

                              I have no idea what I am doing wrong. I get no errors. The permissions are correct...but the file does not get uploaded. This has been my problem for over a week now.

                                I did not ignore your solution...

                                Then post your updated code.

                                  $query	= "select Server, UserName, Password, Destination_Path, ServerType from company";
                                  
                                  $result	= @mysql_query($query);
                                  
                                  $row	= @mysql_fetch_array($result);
                                  
                                  
                                  
                                  if (!empty($row["Server"]))
                                  {
                                  $hostip = gethostbyname($row["Server"]);
                                  
                                  $conn_id = ftp_connect($hostip);
                                  
                                  
                                  // set up basic connection
                                  //$conn_id = ftp_connect($row["Server"]);
                                  
                                  
                                  
                                  // login with username and password
                                  $login_result = ftp_login($conn_id, $row["UserName"], $row["Password"]);
                                  
                                  // check connection
                                  if ((!$conn_id) || (!$login_result)) 
                                  {
                                  $value = "FTP connection has failed!<br>
                                  Attempted to connect to ".$row["Server"]." for user ".$row["UserName"];
                                  exit;
                                  }
                                  
                                  // IMPORTANT!!! turn passive mode on
                                  ftp_pasv ( $conn_id, true );
                                  
                                  
                                  
                                  
                                  
                                  
                                  //parameters are ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY)					
                                  if (!ftp_put($conn_id, "public_html/temp/".strtolower(trim($HTTP_POST_FILES['userfile']['name'])), strtolower(trim($HTTP_POST_FILES['userfile']['tmp_name'])), FTP_BINARY))
                                  echo "<br><br>Did not upload to ftp server.";
                                  
                                  

                                  in the destination path I have tried the root folder (just giving the file name) as well as just public_html dir and none of these work. Iwas told to try passive mode on and I tried it both ways and neither seem to help.