Gaurav Pandey
Gaurav Pandey

Reputation: 2796

fwrite in php not working in binary mode

I am a .net guy and have got a code which works on one of our servers but not on the other, not even on my local machine. The code snippet is given below:

//$flocal1 = fopen($MVfile_name.".txt", "w");
//@fwrite($flocal1, "my name is gaurav pandey!!!");
//opening file
$flocal = fopen($MVfile_name, $mode);

//writing to file

if (!(@fwrite($flocal, $contents))) 
{

//writing error if writing operation failed

  exit(NotUpload);

}

//closing file fclose($flocal);

As we can see, I tried same code with writing a text file with a string and that worked fine, but with binary files, I am getting the error.

Upvotes: 0

Views: 1625

Answers (1)

Emil Vikström
Emil Vikström

Reputation: 91922

If you're on a Windows server, use the b flag to fopen.

fopen($filename, 'wb');

This should already by set by PHP according to the manual, but maybe you are on an old server where this isn't the case.

Upvotes: 1

Related Questions