Parsing and Writing Files in PHP - php

I'm attempting to open a directory full of text files, and then read each file line-by-line, writing the information in each line to a new file. Within each text file in the directory I'm trying to iterate, the information is formed like:
JunkInfo/UserName_ID_Date_Location.Type
So I want to open every one of those text files and write a line to my new file in the form of:
UserName,ID,Date,Location,Type
Here's the code I've come up with so far:
<?php
$my_file = 'info.txt';
$writeFile = fopen($my_file, 'w') or die('Cannot open file: '.$my_file); //implicitly creates file
$files = scandir('/../DirectoryToScan');
foreach($files as $file)
{
$handle = #fopen($file, "r");
if ($handle)
{
while (($buffer = fgets($handle, 4096)) !== false)
{
$data = explode("_", $buffer);
$username = explode("/", $data[0])[1];
$location = explode(".", $data[3])[0];
$type = explode(".", $data[3])[1];
$stringToWrite = $username . "," . $data[1] . "," . $data[2] . "," . $location . "," . $type;
fwrite($writeFile, $stringToWrite);
}
if (!feof($handle))
{
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
}
fclose($writeFile);
?>
So my problem is, this doesn't seem to work. I just never get anything happening -- the output file is never written and I'm not sure why.

There is one potential issue with the scandir() line:
$files = scandir('/../DirectoryToScan');
The path begins with a /, which means that it is looking in the root of the server. So, the directory it's trying to read is /DirectoryToScan. To fix it, you can just remove the leading /. Of course, this could be a sample path for this example and may not actually apply to reality, or maybe you really do have a directory in the root of your system named that - in these cases, feel free to ignore this bit =P.
The next thing is when you're using fopen() on the files you're iterating through. scandir() returns the name of the file, not the full path. You'll need to concat the directory name and the file each time:
$dir = '../DirectoryToScan/';
$files = scandir($dir);
foreach($files as $file) {
$handle = #fopen($dir . $file, "r");
I'm currently running an older version of PHP, so directly-accessing array indexes from return-functions, such as with explode("/", $data[0])[1], doesn't work for me (it was added in PHP 5.4).
Other than that, the rest of your code looks like it should work fine (minus any potential logic/data errors that I may have overlooked).

Related

fopen in php 7 with change different operating system

i create simple code that check if file exists and create one if return false
in windows every thing is good and code work but when i upload the code to linux
server not work because every file created twice
in this order.
if (file_exists(self::COOKIES_FOLDER.DS.$email . ".txt") === false) {
$fh = fopen(self::COOKIES_FOLDER.DS.$email . ".txt", 'w');
fclose($fh);
}
Seems $email contains some spaces in the end. it always good to trim emails and usernames.
$file = self::COOKIES_FOLDER . DS . trim($email) . ".txt";
if (file_exists($file) === false) {
$fh = fopen($file, 'w');
fclose($fh);
}

create txt file list from php

I have bunch of images file (.jpg) in a folder, then I want to list them to a single file text, I using php (xampp in windows).
This for list images name in my browser (it's working):
<?php
ob_start();
$file='F:\images\upload\google\ready_45';
foreach (glob($file."\*.jpg") as $filenames) {
echo $filenames."<br />";
}
?>
This for create text file called 'images_list.txt' (not working):
<?php
ob_start();
$file='F:\images\upload\google\ready_45';
foreach (glob($file."\*.jpg") as $filenames) {
$my_file = 'image_list.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
$data = echo $filenames."<br />";
fwrite($handle, $data);
}
?>
When I execute that script, appear warning message
"
Parse error: syntax error, unexpected 'echo' (T_ECHO) in D:\xampp\htdocs\rename_file_php\try_list_img.php on line 7"
If line 7, I change
$data = $filenames;
The file 'images_list.txt' will created, but only fill one image name listing in the file. Can anyone help me?
Sorry for my bad english.
Open file once and try to write data once:-
<?php
ob_start();
$file='F:\images\upload\google\ready_45';
$data = '';
foreach (glob($file."\*.jpg") as $filenames) {
$data .= $filenames."\n";
}
$my_file = 'image_list.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
fwrite($handle, $data);
fclose($handle);
?>
You want to fopen() the file only once, so outside the loop. Otherwise you overwrite the content again and again. Take a look at this modified version:
<?php
$folder = 'F://images/upload/google/ready_45';
$my_file = 'image_list.txt';
$handle = fopen($my_file, 'w') or die("Cannot open file: ". $my_file);
foreach (glob($folder . "/*.jpg") as $filename) {
$data = $filename . PHP_EOL;
fwrite($handle, $data);
}
fclose($handle);
One certainly could simplify that. For example by simply imploding the list of matched file names with a linebreak and then writing the result in one go:
<?php
$folder = 'F://images/upload/google/ready_45';
$data = implode(PHP_EOL, glob($folder . "/*.jpg"));
$my_file = 'image_list.txt';
$handle = fopen($my_file, 'w') or die("Cannot open file: ". $my_file);
fwrite($handle, $data);
fclose($handle);
However the first (loop based) approach allows more flexibility, for example filtering or escaping.
Side notes:
using a normal slash as folder delimiter (/) instead of the insane backslash (\\) natively used in MS-Windows will save you a lot of hassle. PHP can work with both on a MS-Windows platform.
using a line break instead of the html linewrap makes more sense when writing into a file in most cases. Using PHP_EOL instead of a hard coded line break (\r\n) will make your code portable for systems using different types of line breaks (only MS-Windows uses \r\n for that).
I took the liberty to also fix some indentation and code styling issues. It definitely makes sense if programmers loosely agree on some standard to enhance readability of code.
This is all you need:
// Creates a newline separated list from the array returned by glob()
$files = glob ($folder.'/*.jpg');
$files = implode (PHP_EOL, $files);
∕∕ This is all that is needed to write something to a file.
file_put_contents ($my_file, $files);
The fopen() and all that is old, old code, which should be avoided whenever possible. Not only is this method simpler and easier to read, but it's also generally faster.
Note that you might want to wrap an IF statement around the last line, in order to handle any errors with writing that might crop up.
Following code is working fine, I have a images folder which is having some image files, please change the folder path and try the below code
<?php
ob_start();
$file='images';
foreach (glob($file."\*.jpg") as $filenames) {
$my_file = 'image_list.txt';
$handle = fopen($my_file, 'a') or die('Cannot open file: '.$my_file);
$data = $filenames."\r\n";
fwrite($handle, $data);
}
?>

My php script won't open files that aren't in its root directory

This is a php script for a user login system that I am developing.
I need it to read from, and write to, the /students/students.txt file, but it won't even read the content already contained in the file.
<?php
//other code
echo "...";
setcookie("Student", $SID, time()+43200, "/");
fopen("/students/students.txt", "r");
$content = fread("/students/students.txt", filesize("/students/students.txt"));
echo $content;
fclose("/students/students.txt");
fopen("/students/students.txt", "w");
fwrite("/students/students.txt", $content."\n".$SID);
fclose("/students/students.txt");
//other code
?>
You are not using fopen() properly. The function returns a handle that you then use to read or edit the file, for example:
//reading a file
if ($handle = fopen("/students/students.txt", "r"))
{
echo "info obtained:<br>";
while (($buffer = fgets($handle))!==false)
{ echo $buffer;}
fclose($handle);
}
//writing/overwriting a file
if ($handle = fopen("/students/students.txt", "w"))
{
fwrite($handle, "hello/n");
fclose($handle);
}
Let me know if that worked for you.
P.S.: Ty to the commentators for the constructive feedback.
There are many ways to read/write to file as others have demonstrated. I just want to illustrate the mistake in your particular approach.
fread takes a file handle as param, NOT a string that represents the path to the file.
So your line:
$content = fread("/students/students.txt", filesize("/students/students.txt")); is incorrect.
It should be:
$file_handle = fopen("/students/students.txt", "r");
$content = fread($file_handle, filesize("/students/students.txt"));
Same thing when you write contents to file using fwrite. Its reference to the file is a File Handle opened using fopen NOT the filepath. when opening a file using fopen() you can also check if the $file_handle returned is a valid resource or is false. If false, it means the fopen operation was not successful.
So your code:
fopen("/students/students.txt", "w");
fwrite("/students/students.txt", $content."\n".$SID);
fclose("/students/students.txt");
Needs to be re-written as:
$file_handle = fopen("/students/students.txt", "w");
fwrite($file_handle, $content."\n".$SID);
fclose($file_handle);
You can see that fclose operates on file handles as well.
File Handle (as per php.net):
A file system pointer resource that is typically created using fopen().
Here are a couple of diagnostic functions that allow you to validate that a file exists and is readable. If it is a permission issue, it gives you the name of the user that needs permission.
function PrintMessage($text, $success = true)
{
print "$text";
if ($success)
print " [<font color=\"green\">Success</font>]<br />\n";
else
print(" [<font color=\"red\">Failure</font>]<br />\n");
}
function CheckReadable($filename)
{
if (realpath($filename) != "")
$filename = realpath($filename);
if (!file_exists($filename))
{
PrintMessage("'$filename' is missing or inaccessible by '" . get_current_user() . "'", false);
return false;
}
elseif (!is_readable($filename))
{
PrintMessage("'$filename' found but is not readable by '" . get_current_user() . "'", false);
return false;
}
else
PrintMessage("'$filename' found and is readable by '" . get_current_user() . "'", true);
return true;
}
I've re-written your code with (IMO) a cleaner and more efficient code:
<?php
$SID = "SOMETHING MYSTERIOUS";
setcookie("Student", $SID, time()+43200, "/");
$file = "/students/students.txt"; //is the full path correct?
$content = file_get_contents($file); //$content now contains /students/students.txt
$size = filesize($file); //do you still need this ?
echo $content;
file_put_contents($file, "\n".$SID, FILE_APPEND); //do you have write permissions ?
file_get_contents
file_get_contents() is the preferred way to read the contents of a
file into a string. It will use memory mapping techniques if supported
by your OS to enhance performance.
file_put_contents
This function is identical to calling fopen(), fwrite() and
fclose() successively to write data to a file. If filename does not
exist, the file is created. Otherwise, the existing file is
overwritten, unless the FILE_APPEND flag is set.
Notes:
Make sure the full path /students/students.txt is
correct.
Check if you've read/write permissions on /students/students.txt
Learn more about linux file/folder permissions or, if you don't access to the shell, how to change file or directory permissions via ftp
Try to do this:
fopen("students/students.txt", "r");
And check to permissions read the file.

Getting Error on fopen in PHP

I'm writing a simple text editor for a template, and I've gotten the opening, displaying, and editing part handled. Every time I try to save it though, it keeps giving me an error on the fopen() function.
I'm getting the files with this:
$dir = "./uploads/post-templates";
$files = scandir($dir);
while($files[0] == "." || $files[0] == "..") {
array_shift($files);
}
Then a simple loop handles displaying filenames in a select menu:
<?php foreach($files as $f) { echo "<option name='file' value=" . $f . " class='file'>" . $f . "</option>";}; ?>
Lastly it is all appended into the textarea using a short jQuery function. Alas, when it comes to executing the script to save the file, I get an error every single time. I've tried using relatives, absolutes, and http for the directory, and the filename and path are echoing properly each time.
///different file!!!!
$f = $_POST['file'];
$c = $_POST['content'];
$dir = "./uploads/post-templates/";
$file = $dir . $f;
echo $file;
$fo = fopen($file, "w") or die("opening error");
fwrite($fo, $c) or die("writing error");
fclose($f);
NOTE: For testing purposes only.
I wrote a test script and it was successful.
With the values that you have Mike, try using my script below with your present incoming values.
Plus this line gave me an error from your original code: fclose($f);
Error: when using fclose($f);
Warning: fclose() expects parameter 1 to be resource, string given in...
It should read as fclose($fo);
TEST CODE:
<?php
$f = "thefile.txt";
$c = "the content";
$dir = "./test/";
$file = $dir . "/" . $f;
echo $file; // echos the file name at this point
$fo = fopen($file, "w") or die("opening error");
fwrite($fo, $c) or die("writing error");
fclose($fo);
// shows the contents of the written file on screen
$contents = file_get_contents($file);
echo $contents;
?>

php zip file not created despite no erros, no permission problems, no empty folders, no

Okay. Every file exists, is readable, and is writable. ZIP produces no errors, and outputs: "numfiles: 140 status:0".
the code reads a log, checks for specific text, then imports a number of images into a zip folder. everything runs great except the zip folder is always empty. I've read a lot of threads about this, and they all were resolved by changing permissions, modifying paths and checking for read/write/exist/errors. but... nothing has worked. whats up?
<?php
$file = fopen("log.log", "r") or exit("Unable to open file!");
$zip = new ZipArchive();
$filename = "E:/Web Sites/whatever/order_stream/images.zip";
$try_file = $zip->open($filename,ZIPARCHIVE::OVERWRITE);
if ($try_file !== true) {
exit("cannot open <$filename>\n");
}
while(!feof($file)) {
$line = fgets($file);
$results = explode(": ", $line);
if ($results[0] == "Copying" || $results[0] == "File already exists, overwriting") {
$file_name = substr($results[1],19);
$to_zip = "E:/Web Sites/whatever/catalog/pictures/".$file_name;
$to_zip = trim($to_zip);
if (file_exists($to_zip)) {
$zip->addFile($to_zip);
}
}
}
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
$zip->close();
fclose($file);
?>
The ZipArchive::addFile() method accepts the path to the file as its first parameter, but not all paths are created equal. addFile() method silently rejects (bug?) the file and you never know what went wrong. An alternative approach would be:
// $zip->addFile($file);
$content = file_get_contents($file);
$zip->addFromString(pathinfo ( $file, PATHINFO_BASENAME), $content);
In addition to getting the code working, file_get_contents() also generates decent error messages if you made an error in the path. In this example
$file = $full_directory_path_ending_with_slash.$filename;
Since in the above question, you have the parts in your fingers, the code could simply become:
$content = file_get_contents($to_zip);
$zip->addFromString($filename, $content);

Categories