Viewed   614 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

5

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.
Sunday, December 18, 2022
 
ofir_d
 
5

The reason why unserialize() fails with:

$ser = 'a:2:{i:0;s:5:"héllö";i:1;s:5:"wörld";}';

Is because the length for héllö and wörld are wrong, since PHP doesn't correctly handle multi-byte strings natively:

echo strlen('héllö'); // 7
echo strlen('wörld'); // 6

However if you try to unserialize() the following correct string:

$ser = 'a:2:{i:0;s:7:"héllö";i:1;s:6:"wörld";}';

echo '<pre>';
print_r(unserialize($ser));
echo '</pre>';

It works:

Array
(
    [0] => héllö
    [1] => wörld
)

If you use PHP serialize() it should correctly compute the lengths of multi-byte string indexes.

On the other hand, if you want to work with serialized data in multiple (programming) languages you should forget it and move to something like JSON, which is way more standardized.

Monday, December 19, 2022
2

Example of html code with enctype:

<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="myfile" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>

Do you have enctype in Form tag? Without the attribute your file won't be send.

For debugging of $_FILES array. Try

var_dump($_FILES);

to see if the data are correct.

Your variable:

$_FILES['tmp_name'] 

is used wrongly because $_FILES contain files not a one file. Therefore you should write:

$_FILES['your-name-in-html-form']['tmp_name'] // your-name-in-html-form = myfile in the example above

Have a look on the example: http://www.zymic.com/tutorials/php/creating-a-file-upload-form-with-php/

Thursday, December 1, 2022
 
3

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.

Friday, October 7, 2022
3

Here you go:

$ftp = ftp_connect($host, $port, $timeout);
ftp_login($ftp, $user, $pass);
 
$ret = ftp_nb_put($ftp, $dest_file, $source_file, FTP_BINARY, FTP_AUTORESUME);

while (FTP_MOREDATA == $ret)
    {
        // display progress bar, or something
        $ret = ftp_nb_continue($ftp);
    }
 
// all done :-)

Error handling omitted for brevity.

Please note: you have to have ext-ftp installed and enabled.

Friday, December 9, 2022
 
rogchap
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :