Asked  8 Months ago    Answers:  5   Viewed   72 times

I upload XML file through FTP:

$ftp = "ftp";
$username = "username";
$pwd = "password";
$filename = $_FILES[$xyz][$abc];
$tmp = $_FILES['file']['tmp_name'];
$destination = "/Content/EnquiryXML ";

$connect = ftp_connect($ftp)or die("Unable to connect to host");
ftp_login($connect,$username,$pwd)or die("Authorization Failed");
echo "Connected!<br/>";              

if(!$filename)
{
    echo"Please select a file";
}

else
{
    ftp_put($connect,$destination.'/'.$filename,$tmp,FTP_ASCII)or die("Unable to upload");
    echo"File successfully uploaded to FTP";
}

I want to send the XML file created using DOMDocument to a FTP server but I am not able.

The ftp_put returns false.

 Answers

29

Most typical cause of problems with ftp_put (or any other transfer command like ftp_get, ftp_nlist, ftp_rawlist, ftp_mlsd) is that PHP defaults to the active mode. And in 99% cases, one has to switch to the passive mode, to make the transfer working. Use the ftp_pasv function.

$connect = ftp_connect($ftp) or die("Unable to connect to host");
ftp_login($connect, $username, $pwd) or die("Authorization failed");
// turn passive mode on
ftp_pasv($connect, true) or die("Unable switch to passive mode");

See also:

  • PHP ftp_put fails with "Warning: ftp_put (): PORT command successful"
  • my article on the active and passive FTP connection modes.
Wednesday, March 31, 2021
 
madphp
answered 8 Months ago
66

try an other version much easier. It is based in PHP 7 and is ready to run phpunit tests in one comand dunit

$ dstop $ git checkout builder $ . ./aliases.sh $ dup $ dunit

Wednesday, March 31, 2021
 
the_e
answered 8 Months ago
87

The ZipArchive does not support URL wrappers.

And your code does not make much sense anyway:

  • You first upload $localfile to FTP server as /htdocs/file.zip

    $file = '/htdocs/file.zip';
    ftp_fput($conn_id, $file, $fp, FTP_ASCII)
    
  • And then you try to open /htdocs/file.zip, as it it were a local file

    $zip->open($file);
    

    But such local file does not exists.

  • And then you try to extract that non existing file to FTP URL. And that's not supported.

    See ZipArchive::open(): support stream wrappers. It's about open, but if open does not support wrappers, the extactTo won't either (it's a way more difficult to support). See a comment by cmb:

    Anyhow, ZipArchive::open() is not supposed to accept any stream wrapper URLs, but only real file paths. Its documentation doesn't tell otherwise, and neither does the man page on "Supported Protocols and Wrappers"[1]:

    | PHP comes with many built-in wrappers for various URL-style | protocols for use with the filesystem functions [...]

    However, ZipArchive::open() is not a filesystem function for that matter.

    So, actually, this is not a bug, not even a documentation bug in the strict sense. Therefore I'm changing to feature request.


As your code is just wrong, its difficult to guess, what you are actually trying to do. I can imagine these two possibilities.

  • You wanted to upload a ZIP file to FTP server and unzip it there. It's simply not possible to unzip a ZIP file on an FTP server.

  • You wanted to extract a local ZIP to FTP server. While it may seem that it's possible with use of URL wrapper in the ZipArchive:extractTo call, it's not. As I've shown above. Nor there is any other way to extract local ZIP file to FTP server with some simple one-liner in PHP.

    All you can do, is to extract the ZIP file locally (on the web server); and then upload it file-by-file to the FTP server.

    • Create a temporary folder and extract (ZipArchive::extractTo) the ZIP archive there.

    • Upload the temporary folder to the FTP server.

      See PHP - ftp_put + Copy Entire Folder Structure.

    • Delete the temporary folder.


Also note that you upload the file using ASCII mode. A ZIP format is binary. By uploading binary file in ASCII mode, you damage it.

ftp_fput($conn_id, $file, $fp, FTP_ASCII);
Saturday, May 29, 2021
 
Gigamegs
answered 5 Months ago
48

You should always wrap your function to check for it's existence to prevent issues when included. And remove the closing PHP tag ?> from included files, to prevent end-of-line (EOL) lexing issues.

send_notification.php

<?php
if (false === function_exists('sendNotification')) {
    function sendNotification() {
        echo "I HATE THIS";
    }
}

index.php

<?php
require_once __DIR__ . '/send_notification.php';
sendNotification();
//...

You should also only use include_once or require_once notation on function/config pages to prevent overriding/duplication of variable values/functions when used.

Saturday, May 29, 2021
 
Precastic
answered 5 Months ago
65

You should set the mode with ftp_put to be FTP_BINARY:

ftp_put($conn_id, $remote_file, $file, FTP_BINARY); 

This is mandatory since ASCII mode checks whether the line endings differ on client/server (your case, since you are likely on windows and the server runs unix) and tries to convert them (rnn). In BINARY mode files are being sent as is.

Wednesday, August 4, 2021
 
Jesse
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share