2

Below is a script I am using to modify some files with placeholder strings. The .htaccess file sometimes gets truncated. It's about 2,712 bytes in size before editing and will vary in size after editing depending on the length of the domain name. When it gets truncated, it ends up around 1,400 bytes in size.

$d_parts = explode('.', $vals['domain']);
$ftpstring = 'ftp://' . $vals['username']
        . ':' . $vals['password']
        . '@' . $vals['ftp_server']
        . '/' . $vals['web_path']
;
$stream_context = stream_context_create(array('ftp' => array('overwrite' => true)));

$htaccess = file_get_contents($ftpstring . '.htaccess');
$htaccess = str_replace(array('{SUB}', '{DOMAIN}', '{TLD}'), $d_parts, $htaccess);
file_put_contents($ftpstring . '.htaccess', $htaccess, 0, $stream_context);

$constants = file_get_contents($ftpstring . 'constants.php');
$constants = str_replace('{CUST_ID}', $vals['cust_id'], $constants);
file_put_contents($ftpstring . 'constants.php', $constants, 0, $stream_context);

Is there a bug in file_get_contents(), str_replace(), or file_put_contents()? I have done quite a bit of searching and haven't found any reports of this happening for others.

Is there a better method of accomplishing this?

SOLUTION

Based on Wrikken's response, I started using file pointers with ftp_f(get|put), but ended up with zero length files being written back. I stopped using file pointers and switched to ftp_(get|put), and now everything seems to be working:

$search = array('{SUB}', '{DOMAIN}', '{TLD}', '{CUST_ID}');
$replace = explode('.', $vals['site_domain']);
$replace[] = $vals['cust_id'];
$tmpfname = tempnam(sys_get_temp_dir(), 'config');

foreach (array('.htaccess', 'constants.php') as $file_name) {
    $remote_file = $dest_path . $file_name;
    if (!@ftp_get($conn_id, $tmpfname, $remote_file, FTP_ASCII, 0)) {
        echo $php_errormsg;
    } else {
        $contents = file_get_contents($tmpfname);
        $contents = str_replace($search, $replace, $contents);
        file_put_contents($tmpfname, $contents);
        if (!@ftp_fput($conn_id, $remote_file, $tmpfname, FTP_ASCII, 0)) {
            echo $php_errormsg;
        }
    }
}

unlink($tmpfname);
2
  • What does the truncated version of the file look like? Commented Nov 4, 2010 at 17:03
  • @Pekka - It's simply missing the last part of the file. I have several lines of AddType declarations and truncation usually ends up in the middle of one of those lines. Commented Nov 4, 2010 at 17:06

1 Answer 1

2

With either passive of active ftp, I've never had much luck file using the file-family of functions with the ftp wrappers, usually with that kind of truncation problem. I usually just revert to the ftp functions with passive transfers, which do make it harder to switch, but work flawlessly for me.

Sign up to request clarification or add additional context in comments.

4 Comments

Do you have example code for a get->edit->put type of process?
Using a tandem of tempnam for a temporary file, and ftp_fget, altering the data in your temporary file, and an ftp_fput when you're finished would do the trick.
I updated my post. I am ending up with zero length files now.
You probably forgot to use fseek to set the pointer back to beginning in that case... (fseek($handle,0)), but indeed, ftp_put/get would also work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.