I have small class called 'Call' and I need to store these calls into a flat file. I've made another class called 'CallStorage' which contains an array where I put these calls into.
My problem is that I would like to store this array to disk so I could later read it back and get the calls from that array.
I've tried to achieve this using serialize() and unserialize() but these seems to act somehow strange and part of the information gets lost.
This is what I'm doing:
//write array to disk
$filename = $path . 'calls-' . $today;
$serialized = serialize($this->array);
$fp = fopen($filename, 'a');
fwrite($fp, $serialized);
fclose($fp);
//read array from serialized file
$filename = $path . 'calls-' . $today;
if (file_exists($filename)) {
$handle = fopen($filename, 'r');
$contents = fread($handle, filesize($filename));
fclose($handle);
$unserialized = unserialize($contents);
$this->setArray($unserialized);
}
Can someone see what I'm doing wrong, or what. I've also tried to serialize and write arrays that contains plain strings. I didn't manage to get that working either.. I have a Java background so I just can't see why I couldn't just write an array to disk if it's serialized. :)
Firstly, use the shorthand forms:
file_put_contents($filepath,serialize($var));
and
$var=unserialize(file_get_contents($filepath));
And then output/debug at each stage to find where the problem is.
Related
I have a csv file named data1.csv. The first time i use php to read the csv file content I can get the contents correctly. The problem is I cannot read the new data after I update the data in data1.csv. It keeps returning the previous data. What could be the wrong?
$file_handle = fopen($csvFile, 'r');
if(!$file_handle) die("Can't open file");
while (!feof($file_handle) ) {
$line_of_text[] = fgetcsv($file_handle, 0, $csv_spliter);
}
clearstatcache();
fclose($file_handle);
$file_content = $line_of_text;
Since you are retrieving data via http/https, it is most likely being cached. See Does PHPs fopen function implement some kind of cache?
I am uploading files that I need to attach to email via office365 API.
What I need is the content of the file in a variable WITHOUT storing/saving the file, how can I do that?
foreach ($request->filesToUpload as $file) {
$originalName = $file->getClientOriginalName();//working
$content = $file->getContent();//<- I need this, but not working
//$this->addAttachment($content, $originalName) //logic for later
}
Access the contents of the file like so:
$content = file_get_contents(Input::file('nameAttribute')->getRealPath());
or in other words inside that loop
$contents = file_get_contents($file->getRealPath());
Get the real path with object methods, and you may interact with it like any other file.
Waqas Bukhary,
You get that result, beucause, getContent is not a method of uploadedFiles, check the documentation.
But you have the path, so you can always read the content, like:
$path = $file->path();
$handle = fopen($path, 'r');
$content = fread($handle, filesize($path);
fclose($handle);
You can also use the Request File method if you know the name of the file field, check it here.
I have a file (sites.txt) that has two entries:
http://www.url1.com/test1.xml
http://www.url2.com/test2
Whenever I execute the below PHP code, the 'url1.com' returns false, and the 'url2.com' is loaded into $xml. The odd part is that if I interchange the URLs in the file, i.e.
http://www.url2.com/test2
http://www.url1.com/test1.xml
It loads both. Both URLs are valid XML documents. Why does the order matter here?
Code:
if (file_exists('sites.txt')) {
$file_handle = fopen("sites.txt", "r");
while (!feof($file_handle)) {
$site = fgets($file_handle);
$xml[] = simplexml_load_file($site);
}
fclose($file_handle);
}
try changing your text file to a csv then explode contents of the file on the delimiter:
http://www.url1.com/test1.xml,
http://www.url2.com/test2
$file = fopen("sites.txt", "r");
$files = explode(",", $file);
Sounds like there are some other things going on in addition to this but that you may have that sorted out...
I'm trying to make this save a file and it creates the file, but it's always empty. This is the code for it:
<?php
$code = htmlentities($_POST['code']);
$i = 0;
$path = 'files/';
$file_name = '';
while(true) {
if (file_exists($path . strval($i) . '.txt')) {
$i++;
} else {
$name = strval($i);
$file_name = $path . $name . '.txt';
break;
}
}
fopen($file_name, 'w');
fwrite($file_name, $code);
fclose($file_name);
header("location: index.php?file=$i");
?>
I echoed out $code to make sure it wasn't empty, and it wasn't. I also tried replacing
fwrite($file_name, $code);
with this:
fwrite($file_name, 'Test');
and it was still empty. I have written to files a couple of times before in PHP, but I'm still really new to PHP and I have no idea whats wrong. Could someone tell me what I'm doing wrong or how to fix this? Thanks
Reading/Writing to/from a file or stream requires a resource handle:
$resource = fopen($file_name, 'w');
fwrite($resource, $code);
fclose($resource);
The resource handle $resource is essentially a pointer to the open file/stream resource. You interact with the created resource handle, not the string representation of the file name.
This concept also exists with cURL as well. This is a common practice in PHP, especially since PHP didn't have support for OOP when these methods came to be.
Take a look of the samples on php.net
I have two php files: one is called key.php and the other is the function that validates the key. I want to regularly write to the key.php file and update the key from the validator.php file.
I have this code:
$fp = fopen('key.php', 'w');
$fwrite = fwrite($fp, '$key = "$newkey"');
What I'm trying to do is set the $key variable in the file key.php to the value of $new key which is something like $newkey = 'agfdnafjafl4'; in validator.php.
How can I get this to work (use fwrite to set a pre-existing variable in another file aka overwrite it)?
Try this:
$fp = fopen('key.php', 'w');
fwrite($fp, '$key = "' . $newkey . '"');
fclose($fp);
This will "overwrite" the variable in a literal sense. However, it won't modify the one you're using in your script as it runs, you'll need to set it ($key = somevalue).
More to the point, you really should be using a database or a seperate flat text file for this. Modifying php code like this is just plain ugly.
for_example, you have YOUR_File.php, and there is written $any_varriable='hi Mikl';
to change that variable to "hi Nicolas", use like the following code:
<?php
$filee='YOUR_File.php';
/*read ->*/ $temmp = fopen($filee, "r"); $contennts=fread($temp,filesize($filee)); fclose($temmp);
// here goes your update
$contennts = preg_replace('/\$any_varriable=\"(.*?)\";/', '$any_varriable="hi Jack";', $contennts);
/*write->*/ $temp =fopen($filee, "w"); fwrite($temp, $contennts); fclose($temp);
?>