|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-11-22 09:04 UTC] pascalholscher at gmail dot com
Description:
------------
A bug with str_replace & fwrite
Test script:
---------------
function installNewerVersion($newversion) {
$docroot = $_SERVER['DOCUMENT_ROOT'];
// url naar zip (update site)
$src = 'http://support.telsec.nl/tms_updates/'.$newversion.'.zip';
//locatie voor het zip bestand
$dest = $docroot.'/tmp/'.$newversion.'.zip';
// open de locaties
$fsrc = fopen($src,'r');
$fdest = fopen($dest,'w+');
// kopieƫr bestand van locatie a naar b
$len = copy($src,$dest);
// sluit locaties
fclose($fsrc);
fclose($fdest);
$command = 'x "'.$docroot.'/tmp/'.$newversion.'.zip" -aoa -o"'.$docroot.'/tmp/" && exit';
$programDir = $docroot."/../7za.exe";
if(file_exists('tmp/upgrade_'.$newversion.'.php')) {
require_once('tmp/upgrade_'.$newversion.'.php');
}
$filename = $_SERVER['DOCUMENT_ROOT'].'/product_version.php';
if(!$openfile = fopen($filename, 'rb+')) {
echo 'Kan bestand niet openen.';
}
$data = file_get_contents($filename);
$data1 = str_replace('3.3.4', '3.3.5', $data);
rewind($openfile);
fwrite($openfile, $data1);
fclose($openfile);
if(isset($_COOKIE['checked'])) {
setcookie("checked", "", time() - 3600);
}
echo "<script type='text/javascript'>
alert('De update is geslaagd.');
</script>";
return $len;
}
Expected result:
----------------
Show the output/alertbox "update succes"
Actual result:
--------------
No output of echo's or alerts at all.
Extra info:
This is a function included in another file when I comment out the fwrite
the script shows the output but doesn't write the data in the correct way.
So there are some issues with str_replace & fwrite.
I'm using php 4.4.4 as that is the version the firmware is using where we want to let users implent an auto update.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 01:00:01 2025 UTC |
Yes this is happening with php 5 also but it's not a bug it seems as: $version = explode('.', VERSION); $new = explode('.', $newversion); $data1 = preg_replace('{'.$version[2].'}', '.$new[2].', $data); $version[2] represents the old version number ending in this example a 4 $new[2] represents the new version in this example 5 The thing is when I run it the changes are made but all php errors are not displayed on the screen anymore same goes for the echo's etc. Once I make the line like this: $data1 = preg_replace('{'.$version[2].'}', '{'.$new[2].'}', $data); It works but only adds some unnesecary and not planned {} to it.Preg expressions start and end with a delimiter. Using { and } have special meanings there. The correct regex you want looks like this: $data1 = preg_replace('/\{'.$version[2].'\}/', '{'.$new[2].'}', $data); No bug here.That doesn't work like I want I will show you what I mean: $newversion = '3.3.5'; define(VERSION, "3.3.4"); // this is the line in the included version file $filename = $docroot.'/product_version.php'; if(!$openfile = fopen($filename, 'rb')) { echo 'Kan bestand niet openen.'; } $data = file_get_contents($filename); $version = explode('.', VERSION); $new = explode('.', $newversion); $data1 = preg_replace($version[2], $new[2], $data); fwrite($openfile,$data1); fclose($openfile); // my expected result of data1 = making the 3.3.4 in version.php to 3.3.5 the new version but what happends is it replaces the file number what I want but it doesn't show the echo's on my screen like I want. And when I quote this function then the echo's are showing on screen.Sorry, this is not a support forum. You have clearly not read the preg documentation. Once again, you have to use a delimiter character at the beginning and end of your expression. When you do preg_replace($version[2] there is clearly no delimiter. You would need to do preg_replace('/'.$version[2].'/' to make it into a valid pattern. However, for straight replacements like that, just use str_replace().