I need to have a page that will allow uploading of an image. Please help:

<?php
$fp = ftp_connect("ftp://ftp." . $domain);
$login = ftp_login($fp, $userid, $login_password);
$copy = ftp_fput($fp, "/directory/".$file, $file, FTP_BINARY);
$exit = ftp_quit($fp);

if ( $fp && $login && $copy && $exit ) {
// bunch of commands if it works
}
else {
// bunch of commands if it fails
}
?>

The $domain variable is, of course, the domain name (domain.com), and I have conatenated it with the protocol and prefix "ftp://ftp.". For a test I just coded in echo $fp;, to make sure it worked, and, sure enough, it returned "1", so it's connecting without a problem. It also exits nicely ($exit).

The $file var. is a "file" input type parsed from a form.
However, I the following error message for the $login and the $copy variables:

Warning: Unable to find ftpbuf 0 in .../www.domain.com/file.php on line 64

Warning: Unable to find ftpbuf 0 in .../www.domain.com/file.php on line 65

Of course, I've changed the real domain and dirs to domain.com, just for the purpose of example and security.πŸ™‚

    You have a funny way of testing/debugging your scripts!
    You are going to ask many questions on this forum.
    The problem with your script is that probably that you are passing
    the temporary file name of the uploaded file as the destination file also!
    Also all directories must exist!

    Try the code bellow:

    <?php
    $server = 'ftp.server.com';
    $user = 'anonymous'; 
    $pass = 'anonymous@nowhere.com';
    
    $source_file = $file;	// the temporary filename of the uploaded file
    $destination_file = "/directory/".$file_name;	// dest. directory + the original name of the uploaded file
    
    if (!$ftp_connect = @ftp_connect($server))
    	{
    	die("Could not connect to $server");
    	}
    if (!$ftp = @ftp_login ($ftp_connect, $user, $pass))
    	{
    	die ("Authentication failed");
    	}
    if (!@ftp_pasv($ftp_connect, TRUE))
    	{
    	echo "Passive mode could not be turned on!";
    	}
    if (!@ftp_put($ftp_connect, "$destination_file", "$source_file", FTP_BINARY))
    	{
    	echo "Could not upload $source_file to $destination_file!";
    	}
    else
    	{
    	echo "The file is uploaded!";
    	}
    ftp_quit($ftp_connect);
    ?>
    

      Thanks, but I pretty much figured out why the the connection wouldn't open -- there's no protocol ([url]ftp://[/url]) that is supposed to be within the ftp_connect funtion, which I find somewhat strange.

      I just cannot get it to copy the file. It is always telling me that there was "an error while opening drive:\path\path\file.ext". HelpπŸ˜•

        Did you test my code? There are more differences in my code that just the "protocol" compared to yours!

        The "$file"-variable - if it's coming from a uploaded file trough POST,
        then it will hold the name of the temporary file on the system + the path and
        also the driver letter if the system is windows.
        Considering this, let's see how things will turn out:

        $file = "c:\windows\temp\tmp_uploaded_file.ext"; // temporary file

        and when you do:
        ftp_fput($fp, "/directory/".$file, $file, FTP_BINARY);

        ... you are actually trying to copy the local file "c:\windows\temp\tmp_uploaded_file.ext"
        to the remote file "/directory/c:\windows\temp\tmp_uploaded_file.ext"

        ... and this is not possible, or is it?

          I've got everything working up until ftp_put.
          I'm way ahead of you with the full path thingπŸ˜‰ I've used substr and strstr to parse the filename from the full path

          path: drive:\path\path\file.ext
          result: file.ext

          I think my problem is that, for some reason, it will not read from the local file...why? (just FYI, I do have enctype="multipart\form-data" in the <form> tag for the upload)

            If you have:
            $file = "file.ext";

            ... is the file already in the same folder as the
            script that will upload it to the ftp server?

            Can you show the full code?

              Thanks for all your help, but I've got it figured out, for the most part πŸ˜‰

                Write a Reply...