PHP - failed write - php

I am stuck and in need of a hand. Hope someone can help?
Anyone have any idea why I am getting "failed write" in this code?
$write_file = "/usr/home/public_html/php/users_v2.sql";
$write_handle = fopen($write_file, "w") || die("Couln't open users_v2!");
if (is_writeable($write_file)) {
if ($write_handle === FALSE) echo 'Failed handle?!';
if (fwrite($write_handle, "Hi\n") === FALSE) echo "Failed write!\n";
}
fclose($write_handle);
Thanks in advance.

By using the OR operator when creating your file handle, you are returning a boolean value depending on the operation. So $write_handle will contain true or false, instead of the file resource. A better way to open a file for writing and test that it was successful would be this:
$write_handle = fopen($write_file, 'w');
if ($write_handle === false)
{
die('Could not open file ' . $write_file);
}
Additionally, you could use the file_put_contents() function which handles the fopen(), fwrite() and fclose() for you. I only recommend this if you are executing only one write to the same file, as it will be a lot of overhead, and unless you pass the FILE_APPEND flag, it will empty the file for every write.

I have seen it used everywhere but the problem is the || die("Couln't open users_v2!");
First I added:
error_reporting(E_ALL);
to see what php is reporting for errors.
$write_handle = fopen($write_file, "w") || die("Couln't open users_v2!");
fclose($write_handle);
Returns an invalid stream handle error and file handle of 1. Without it the returned file handle is "Resource id #x".
Changing the line to:
$write_handle = fopen($write_file, "w"); // || die("Couln't open users_v2!");
and your code works fine. Gonna go post this on php.net now.

Related

Creating new files from an old one

while (($line = fgets($handle)) !== false) {
//look for the first payor block
if(strpos($line, 'N1*PR*') !== false || $block_start) {
$header_end = true; $block_start = true;
//see if the block finished
if(strpos($line, 'CAS*CO*45*20.43**253*1.27~') !== false) {
$block_start = false;
$payor_blocks[$count] .= $line;
$count++;
}
$payor_blocks[$count] .= $line;
} else {
//append to the header
if($header_end) {
$footer .= $line."\n";
} else {
$header .= $line."\n";
}
}
}
//get payor blocks and create a file foreach payor
$new_files = array();
foreach($payor_blocks as $block) {
$filename = $file . "_" . $count;
$count++;
$new_files[] = array(
'name' => $filename,
'content' => $header."\n".$block."\n".$footer
);
//loop through new files and create them
foreach($new_files as $new_file) {
$myfile = fopen($file, "x");
fwrite($myfile, $new_file['content']);
//close the file
fclose($myfile);
I have the code above, it's suppose to be able to open an original file called "$file" and create a new file then close it, However its not creating and when I run it, i get this warning error:
Warning: fopen(362931550.1a): failed to open stream:
File exists in /script2.php on line 90 Warning:
fwrite() expects parameter 1 to be resource,
boolean given in /script2.php on line 94 Warning:
fclose() expects parameter 1 to be resource, boolean
given in /script2.php on line 96
Any help is kindly appreciated.
I have one file named: 362931550.1a
I did a code that splits them at certain areas, (its pretty long to post), when i run the script I see it on my browser but it doesn't create 2 new files in the folder.
Your file open mode is incorrect.
From php.net documentation:
'x' Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING [...]
You should probably use 'w' mode:
'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.
The script failed to open a stream with the fopen() function and return a boolean. The function fwrite() become the boolean value but need a resource.
The reason is that you only create files with the x-modifier in the stream.
Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it.
You see in the PHP manual more informations about the stream-modes (PHP manual).
To prevent this message check if the value isn't false.
$stream = fopen("file.txt", "x");
if($stream === false) {
echo "Error while open stream";
}
//here your code

PHP: Missing records when writing to file

My telecom vendor is sending me a report each time a message goes out. I have written a very simple PHP script that receive values via HTTP GET. Using fwrite I write the query parameter to a CSV file.The filename is report.csv with the current date as a prefix.
Here is the code :
<?php
error_reporting(E_ALL ^ E_NOTICE);
date_default_timezone_set('America/New_York');
//setting a the CSV File
$fileDate = date("m-d-Y") ;
$filename = $fileDate."_Report.csv";
$directory = "./csv_archive/";
//Creating handle
$handle = fopen($filename, "a");
//These are the main data field
$item1 = $_GET['item1'];
$item2 = $_GET['item2'];
$item3 = $_GET['item3'];
$mydate = date("Y-m-d H:i:s") ;
$pass = $_GET['pass'];
//testing the pass
if (isset($_GET['pass']) AND $_GET['pass'] == "password")
{
echo 'Login successful';
// just making sure the function could write to it
if (!$handle = fopen($directory.$filename, 'a')){
echo "Cannot open file ($filename)";
exit;
}
//writing the data I receive through query string
if (fwrite($handle, "$item1,$item2,$item3,$mydate \n") === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
fclose($handle);
}
else{
echo 'Login Failure please add the right pass to URL';
}
?>
The script does what I want, but the only problem is inconsistency, meaning that a good portion of the records are missing (about half the report). When I log to my account I can get the complete report.
I have no clue of what I need to do to fix this, please advice.
I have a couple of suggestions for this script.
To address Andrew Rhyne's suggestion, change your code that reads from each $GET variable to:
$item1 = (isset($_GET['item1']) && $_GET['item1']) ? $_GET['item1'] : 'empty';
This will tell you if all your fields are being populated.
I suspect you problem is something else. It sounds like you are getting a seperate request for each record that you want to save. Perhaps some of these requests are happening to close together and are messing up each other's ability to open and write to the file. To check if this is happening, you might try using the following code check if you opened the file correctly. (Note that your first use of 'fopen' in your script does nothing, because you are overwriting $handle with your second use of 'fopen', it is also opening the wrong file...)
if (!$handle = fopen($directory.$filename, 'a')){
$handle = fopen($directory.date("Y-m-d H:i:s:u").'_Record_Error.txt', 'a');
exit;
}
This will make sure that you don't ever lose data because of concurrent write attempts. If you find that this is indeed you issue, you can delay subsequent write attempts until the file is not busy.
$tries = 0;
while ($tries < 50 && !$handle = fopen($directory.$filename, 'a')){
sleep(.5);//wait half a second
$tries++;
}
if($handle){
flock($handle);//lock the file to prevent other requests from opening the file until you are done.
} else {
$handle = fopen($directory.date("Y-m-d H:i:s:u").'_Record_Error.txt', 'a');//the 'u' is for milliseconds
exit;
}
This will spend 25 seconds, trying to open the file once every half second and will still output your record to a unique file every time you are still unable to open the file to write to. You can then safely fwrite() and fclose() $handle as you were.

Check if a file is writable before writing to it

I'm trying to create a script which will check if a file is writable before writing to it,
Making sure the script doesn't exit prematurely.
I've gotten this far
$meta =stream_get_meta_data($file);
while(!is_writable($meta['uri'])){
sleep(rand(0,3));
$meta=stream_get_meta_data($file);
echo("sleeping\n");
}
$csv = fopen($file, 'a+')or die("can't open file");
When I test the script with $file open, it blocks on the sleeping part even after $file is closed.
I'm fairly new to PHP, so there might be a processing paradigm that i'm not aware of.
Any help would be very welcome.
EDIT : The reason I entered this into a while loop is to continually check if the file is open or not. Hence it should only exit the while loop once the file is finally writable.
The sleep is simply to replicate a person trying to open the file.
its is_writable ( string $filename )
$filename = 'test.txt';
if (is_writable($meta['uri']) {
echo 'The file is writable';
} else {
echo 'The file is not writable';
}
is_writable(<your_file>)
This should do the trick?
http://www.php.net/manual/en/function.is-writable.php
--
Also you can use
#fopen(<your_file>, 'a')
If this returns false, file is not writiable
Using touch():
if (touch($file_name) === FALSE) {
throw new Exception('File not writable');
}
You probably should not be using a while loop just to check if the file is writable. Maybe change your code around a bit to something like this:
$meta =stream_get_meta_data($file);
if (is_writable($file)){
sleep(rand(0,3));
$meta=stream_get_meta_data($file);
echo("sleeping\n");
}
$csv = fopen($file, 'a+')or die("can't open file");
However since I do not know what your main goal is you could do it like this:
$meta =stream_get_meta_data($file);
while(!is_writable($file)){
sleep(rand(0,3));
$meta=stream_get_meta_data($file);
echo("sleeping\n");
}
$csv = fopen($file, 'a+')or die("can't open file");

php - feof error

The file that I'm trying to read is a pgp-encrypted file. This is part of the process to decrypt it and I'm actually attempting to read the contents into a string so that I can then decrypt it. I'm not sure if that's the core problem here or not, but I'm getting an error:
Warning: feof(): supplied argument is not a valid stream resource
Here's the file code:
if($handle = opendir($dir)) {
while( false !== ($file = readdir($handle))) {
if($file != "." && $file != "..") {
$fhandle = fopen($file, "r");
$encrypted = '';
$filename = explode('.',$file);
while(!feof($fhandle)) {
$encrypted .= fread($fhandle, filesize($file));
}
fclose($fhandle);
$decrypted = $filename[0].'.txt';
shell_exec("echo $passphrase | $gpg --passphrase-fd 0 -o $decrypted -d $encrypted");
}
}
}
Learn to debug your code.
supplied argument is not a valid stream resource means passed variable contains unexpected value. So, we can make a logical conclusion, that a function returning this variable had fail.
So. we have to check fopen($file, "r"); what makes it fail? may be we can check if a file exists? And so on.
This is called debugging and you cannot program without it.
Though it seems very strange.
Because fopen should throw an error as well.
You should check the fopen call to make sure you're file has actually been opened. Check it's return value.
As for fixing it you're working directory is likely different than $dir. You probably need
fopen("$dir/$file","r");
unless you change to the directory first.
edit clarified that the code sample was a possible solution to the problem, not code to check the return value.
You need to check your return values. The error indicates that $fhandle doesn't contain a valid file handle - it probably contains false, which fopen returns on failure.
See http://ca.php.net/manual/en/function.fopen.php

suppress error using fread()

I wrote a script for screen pops from our soft phone that locates a directory listing for the caller but occasionally they get "Can't read input stream" and the rest of the script quits.
Does anyone have any suggestions on how to suppress error the error message and allow the rest of the script to run? Thanks!
$i=0;
$open = fopen("http://www.411.ca/whitepages/?n=".$_GET['phone'], "r");
$read = fread($open, 9024);
fclose($open);
eregi("'/(.*)';",$read,$got);
$tv = ereg_replace('[[:blank:]]',' ',$got[1]);
$url = "http://www.411.ca/".$tv;
while ($name=="unknown" && $i < 15) { ## try 15 times before giving up
$file = # fopen($fn=$url,"r") or die ("Can't read input stream");
$text = fread($file,16384);
if (preg_match('/"name">(.*?)<\/div>/is',$text,$found)) {
$name = $found[1];
}
if (preg_match('/"phone">(.*?)<\/div>/is',$text,$found)) {
$phone = $found[1];
}
if (preg_match('/"address">(.*?)<\/div>/is',$text,$found)) {
$address = $found[1];
}
fclose($file);
$i++;
}
You need to check your return values, specifically for fopen.
Something like this:
$file = fopen($fn=$url,"r");
if ($file === false) {
// Unable to open stream, handle error
}
Then you need to decide how to handle the error that occurs when fopen can't read from the URL you're giving it. I doubt you actually want to simply suppress the error and allow the script to continue without the data it needs to do something meaningful. You could pause briefly and attempt to re-open the file, or direct the user to a more friendly error page instead of dying with a bare "Cannot open file" message.

Categories