My file contains this content (1.txt):
user.php?id=XXXXXX
user.php?id=XXXXXX
user.php?id=XXXXXX
user.php?id=XXXXXX
user.php?id=XXXXXX
text/numbers
user.php?id=XXXXXX
user.php?id=XXXXXX
and so on ...
(XXXXXX = numbers)
It's possible to take only user.php?id=XXXXXX and copy them to another file, without coping unnecessary text because file contains about 50 000 lines ?
<?php
$file = fopen('source.txt', 'rb');
$newfile = fopen('target.txt', 'wb');
while(($line = fgets($file)) !== false) {
if(strpos($line, 'user.php') !== false) {
fputs($newfile, $line);
}
}
fclose($newfile);
fclose($file);
?>
This code hasn't been tested, but I think it will work properly.
<?php
//Get all the matches from the file
$fileContents = file_get_contents('1.txt');
preg_match_all('/user.php\?id=[0-9]{6}/', $fileContents, $matches);
//Output to new file
$fh = fopen('output.txt', 'w+');
foreach ($matches['0'] as $match) {
fputs($fh, $match."\r\n");
}
fclose($fh);
?>
<?php
error_reporting(0);
$fs=fopen("1.txt", "r");
$ft=fopen("2.txt", "w");
if ($fs==NULL)
{
echo "Can't Open Source File ...";
exit(0);
}
if ($ft==NULL)
{
echo "Can't Open Destination File ...";
fclose ($fs);
exit(1);
}
else
{
while ($ch=fgets($fs))
fputs($ft, $ch);
fclose ($fs);
fclose ($ft);
}
//echo "File Handling successfully ...";
?>
suppose you want to copy the contents of "abc.php" file to "working_file.php"
place the following code in working_file.php , it will simply copy all the content.
<div>
<?php
$file = fopen("abc.php", "r") or die("Unable to open file!");
echo fread($myfile,filesize("abc.php"));
fclose($file);
?>
</div>
Related
I have a file input.txt that contains file names that I need to open and read data. I have written the following php code and I get the failed to open stream: No such file or directory when it tries to open with variable $files, i.e., the second fopen is failing.
$handle = fopen("/home/user/input.txt", "r");
if($handle) {
while(($files = fgets($handle)) !== false) {
print $files;
$filename = fopen($files,"r");
print $filename;
}
}
input.txt content:
/home/user/file_1
/home/user/file_2
/home/user/file_3
/home/user/file_4
file_1,file_2,file_3 and file_4 are in /home/user/
I am not sure what I am doing wrong.
My guess is that the file lines contains whitespaces (e.g. \r), to remove them we'll use trim()
function open_files_from_file_list()
{
$handle = fopen("/home/user/input.txt", "r");
if(!$handle)
return;
while(($line = fgets($handle)) !== false)
{
$line=trim($line);
print $line;
if (!file_exists($line))
{
print ' does not exists';
continue;
}
$filename = fopen($line,"r");
print $filename;
}
}
I want to write a script that checks if the file exists on the remote Server or not.
If file exists Write url in a file found.txt. How i can Read many urls from a txt file and Write the url to a different txt file?
This is my code:
<pre>
$sourcePath = "http://www.example.net/file.txt";
$AgetHeaders = #get_headers($sourcePath);
if (preg_match("|200|", $AgetHeaders[0])) {
$found = fopen('found.txt', "w") or die("Unable to open file!");
fwrite($found, $sourcePath);
fclose($found);
} else {
echo "Not found!";
}
</pre>
If you store urls in text file one per line you could manage it like this:
$handle = fopen("AddressList.txt", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$sourcePath = $line;
$AgetHeaders = #get_headers($sourcePath);
if (preg_match("|200|", $AgetHeaders[0])) {
$found = fopen('found.txt', "w") or die("Unable to open file!");
fwrite($found, $sourcePath);
fclose($found);
} else {
echo "Not found!";
}
}
} else {
// error opening the file.
}
fclose($handle);
I'm trying to write a nickname from teamspeak to a file.
It goes through all the checks, but when I check the file afterwards, nothing gets written.
$file = "test.txt";
$fh = fopen($file, 'w');
if(file_exists($file))
{
echo "exist";
}else{
echo "absent";
}
if(is_writable($file))
{
echo "writable";
}else {
echo "cant write";
}
if (false === $fh) {
throw new RuntimeException('Unable to open log file for writing');
}
foreach($Summoners as $su){
$name = $su["TsNICK"];
echo $name;
fwrite($fh, $name);
echo "Wrote to file";
}
fclose($fh);
I can't really tell why your code isn't working, but you should consider rewriting it to this:
$file = "test.txt";
if (false === ($fh = fopen($file, 'w'))) {
throw new RuntimeException('Unable to open log file for writing');
}
foreach($Summoners as $su) {
$name = $su["TsNICK"];
echo $name;
fwrite($fh, "$name\n");
}
fclose($fh);
If you wish to always append to a file, you would need mode 'a' instead of 'w'.
I need to create a database table from here:
So, I copied the same file to a server folder using curl command. I need to retrieve 00-00-00 (hex) XEROX CORPORATION, 00-00-01 (hex) XEROX CORPORATION etc. from the above text file. I need to copy only the hex values and the first line of organisation to another text file.
How can I do it with PHP?
<?php
// open the input and the output
$fp = fopen("http://standards.ieee.org/develop/regauth/oui/oui.txt","r");
$out = fopen("somefile.txt","w");
while($rec = fgets($fp)){
// check to see if the line contains '(hex)'
if (strpos($rec, '(hex)') !== false){
// if so write it out
fputs($out, $rec."\n");
}
}
fclose($fp);
fclose($out);
Try this one
<?php
$i = 1;
$a_line = "";
$first_line = "";
$handle = #fopen("/tmp/inputfile.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
if($i == 1)
// here you have first line
$first_line = $buffer;
else {
// check the $buffer contains the substring (hex) and XEROX CORPORATION
// if it contains put it in another variable
$a_line .= $buffer;
}
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
// finally write the $first_line to a header file
// then write $a_line to another file
?>
<?php
error_reporting(0);
$fs=fopen("1.txt", "r");
$ft=fopen("2.txt", "w");
if ($fs==NULL)
{
echo "Can't Open Source File ...";
exit(0);
}
if ($ft==NULL)
{
echo "Can't Open Destination File ...";
fclose ($fs);
exit(1);
}
else
{
while ($ch=fgets($fs))
fputs($ft, $ch);
fclose ($fs);
fclose ($ft);
}
?>
$fp = fopen('log.txt', 'w');
fwrite($fp, 'Missing gallery image for: ' . $row['toolbar_id'] . '\n');
The code above is not writing to the file. the $row['toolbar_id'] is a value from a for each loop. Any suggestions? There is no PHP error, as the file does open as I have debugged that part.
Try this for extra surety
ini_set('display_errors', 'On');
error_reporting(E_ALL);
$fp = fopen('log.txt', 'ab');
if (false === $fp) {
throw new RuntimeException('Unable to open log file for writing');
}
$bytes = fwrite($fp, 'Missing gallery image for: ' . $row['toolbar_id'] . PHP_EOL);
printf('Wrote %d bytes to %s', $bytes, realpath('log.txt'));
fclose($fp);
Edit: Changed the "write" flag (w) to "append" (a) as truncating a log file doesn't sound like a great idea
https://bugs.php.net/bug.php?id=48607
there is a php bug with fwrite and ftp which means the last chunks of files sometimes dont get written when fclose is called directly after fwrite
putting a sleep(1); before fclose fixes the issue, or in your case logging to a file before fclose can also stop it
posting for future reference!
<?php
$filename = 'log.txt';
$somecontent = "Missing gallery image for: ";
if($row['toolbar_id'] != "")
{
$somecontent .= $row['toolbar_id'];
}
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>
Try this code... !!