Pregunta

I want to wite to a text file at the inputed text position using PHP. I tried using fseek to point the wite position to the inputed number, but it saves as an empty file instead.

<?php
$testData = "testdata";
$testPosition = 3;
$fileReference = fopen("test.txt", "w");
fseek($fileReference, $testPosition);
fwrite($fileReference, $testData);
fclose($fileReference);
?>

How would I get the script to wite to the text file at the specified position correctly?

¿Fue útil?

Solución

The w flag only allows for writing to a file. Try replacing the w flag with a r+ flag. This will allow for read/write to a file without truncating the file. the read /write is needed to allows you to go search for your caret and write to where it is. for more information on the fopen function please see php.net: fopen()

Otros consejos

The w flag has this functionality:

'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

This could be why you're getting an empty file.

However, on my local server, this works fine. This makes me think your permissions may not be correct. For example, you might not have write permissions. Try and ensure that you have the correct permissions on this file.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top