With reference to: https://stackoverflow.com/questions/12211629/php-file-put-contents-not-truncating-file-in-cifs-mount

We have a Seagate BlackArmor NAS device, which is mounted on our Ubuntu 10.04 server as follows:

mount -t cifs -o defaults,credentials=/etc/smbpass/demo.smbpass,uid=1005,gid=1005,dir_mode=0770,file_mod=0660 //xxx.xxx.xxx.xxx/demo/files /var/www/mysite/src/files/

It seems that any operation that is supposed to truncate files and then insert content into those files on the NAS ends up only replacing the first few bytes of the file (up until the length of the new contents), and then the rest of the previous "old" content is still in the file.

For Example:

Contents of myfile.txt: thisisabigfatpandaonwheelsgoingdownthestreet

Run PHP script with code: file_put_contents('myfile.txt', 'blah');

Contents of myfile.txt: blahisabigfatpandaonwheelsgoingdownthestreet

The expected contents of the file is obviously blah, but that's not the case.

Using PHP's fwrite() after creating a file handle with fopen('filename', 'w'); has the same effect. ftruncate() also does not work properly. Using a shell command such as echo -n "blah" > myfile.txt has the same effect (that's how I know it's not a PHP-specific problem).

My only work-around currently is to delete the files and then create them from scratch, but I'd prefer not to change any code in my application, if possible (we only started using the NAS device recently). I'd rather apply a solution to what seems to be an OS-related/mount-related problem.

Any ideas?


I had the exact same symptoms on a samba setup of my own (Debian 6 x64 with samba 3.5.6.). I finally narrowed it down to the following option causing the exact same behaviour:

large readwrite = no

When set to yes instead (or removed, as yes is the default setting), the issues were gone.

Filed bug reports against Debain and upstream Samba:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.