I have a question here, tho, I've been digging here at SO, it seems I can't find the real deal;
I am trying the ff:
<?php
$filename = $trantype . $delimiter . $dateToday . $fileExtension ;
//echo $filename . '<br/>';
$fileToOpen = $filepath . $filename;
echo "File To Open: " . $fileToOpen . '<br/>';
$string = file_get_contents($fileToOpen);
//$string = file_get_contents("../transactions/o/O_20120809.xx");
$json_array = json_decode($string, true);
echo "Echo: " . $json_array[0]['itemheader_sysid'] . '<br/>';
echo "The File Contents: " . $string;
?>
The $string = file_get_contents('../transaction/o/O_20120809.xx') works smoothly on the other hand the $string = file_get_contents($fileToOpen); doesn't seem to work and is giving me the ff: error;
Warning: file_get_contents(../transactions/o/0_20120809.xx) [function.file-get-contents]: failed to open stream: No such file or directory in C:\xampp\htdocs\xx\helper\upo.php on line 19
why so?
Anyone please?
Related
I'm trying to extract the balance from a list of bitcoin addresses in a text file.
Here is my code:
<?php
$list = file("list.txt");
$file = "checked_list.txt";
foreach ($list as $address) {
$url = "https://blockchain.info/address/" . $address . "?format=json";
$json = json_decode(file_get_contents($url), true);
$FinalBalance = $json["final_balance"];
echo $address . " Balance is: " . $FinalBalance;
$data_array = array($address, $FinalBalance);
//Saving to file
$my_data = file_put_contents($file, implode(' ', $data_array) . "\n", FILE_APPEND | LOCK_EX);
sleep(5);
}
My issue here is when I have more than 1 line in list.txt it will throw an error:
Warning: file_get_contents(https://blockchain.info/address/ADDRESS-HERE
?format=json): Failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error
in C:\laragon\www\Scanner\BalanceCheckerByAddress\check.php on line 9
Warning: Trying to access array offset on value of type null
If it is only 1 single address, it will display the balance and save the data to checked_list.txt
What am I missing here?
Add this line right after foreach:
$address = trim($address);
Your problem is in function file(..) because every line in the file list.txt containts line break. And you should remove the white space before working the string $address.
Your full code should looks like this:
<?php
$list = file("list.txt");
$file = "checked_list.txt";
foreach ($list as $address) {
$address = trim($address); // Added line to remove white-space
$url = "https://blockchain.info/address/" . $address . "?format=json";
$json = json_decode(file_get_contents($url), true);
$FinalBalance = $json["final_balance"];
echo $address . " Balance is: " . $FinalBalance;
echo "<br>"; // Added line for seeing the results better in web-browser
$data_array = array($address, $FinalBalance);
//Saving to file
$my_data = file_put_contents($file, implode(' ', $data_array) . "\n", FILE_APPEND | LOCK_EX);
sleep(5);
}
I tested the code in PHP 7.4.10 and it works well.
This question already has answers here:
Cannot open file using fopen (php)
(4 answers)
Closed 2 years ago.
In my first php file I open the users.txt to read it and then write in it. Here is the code for it:
Read:
if(file_exists("users.txt")){
$handle = fopen("users.txt", "r");
if ($handle) {
...
} else {
$ERROR = "Can't open the 'users.txt' file";
}
if ($userExist === true) {
...
}
else {
...
}
} else {
...
}
write:
$file = fopen("users.txt", "a") or die("Unable to open the file");
$data = PHP_EOL. $UserId . " " . $Fname . " " . $Lname . " " . $pass;
fwrite($file, $data);
fclose($file);
In my second php file, I tried to open the same file but apprently when I do this code:
$file = fopen("users.txt", "a") or die("Unable to open the file");
if (file_exists($file)) {
$data = " " . $Phone . " " . $recoveryEmail . " " . $month . " " . $day . " " . $year . " " . $gender;
fwrite($file, $data);
fclose($file);
}
else{
echo "Can't open the file 'users.txt'";
}
When I run it everything works fine for my first php file, but for second it prints the message:
Can't open the file 'users.txt'
After you use the fopen function, the $file is a handle, not the file name.
Hence please amend as follows:
$file = fopen("users.txt", "a") or die("Unable to open the file");
if (file_exists("users.txt")) {
$data = " " . $Phone . " " . $recoveryEmail . " " . $month . " " . $day . " " . $year . " " . $gender;
fwrite($file, $data);
fclose($file);
}
else{
echo "Can't open the file 'users.txt'";
}
I have a project where I have to make a new log file if the current log file is larger than 50mb. I know that we have to test for the size of the file.
I am starting out with this $log variable:
$log =
"Branch: ".$branchDir . PHP_EOL.
"Phase: ". $phaseDir . PHP_EOL.
"Total number of results files deleted: ". count($folderCounter). PHP_EOL.
"File names: " . $filePathToDelete . PHP_EOL.
"Starting CustomerID directory name: " . $CustIDvalue . PHP_EOL;
// $log = 'this is a test' . $branch . PHP_EOL;
$dt = time();
$mysql_datetime = strftime("%Y-%m-%d %H:%M:%S", $dt);
$mysql_datetimes = preg_replace('/\s+/', '', $mysql_datetime);
$logFileName = "ResultsFileDeletion" . $mysql_datetimes . ".txt";
if (unlink($filePathToDelete)) {
//this echo string will be moved to a log file
echo "first unlink: {$filePathToDelete} was deleted <br>";
$myFile =
file_put_contents("c:\\sites\\EtonBio/sequencing/logs/{$logFileName}",
$log, FILE_APPEND);
echo 'my file: ' . $myFile . '<br>';
}
I found this code snippet:
$size = filesize($_FILES['foto']['tmp_name']);
I believe my if statement would look something like this:
if($size < 52428800) {
*creates another log file
}
I want to format the $size variable correctly and I am also not sure how to make a new log file.
$size = filesize($_FILES['foto']['tmp_name']); only works if you are uploading the file.
If you want to check a file already in you server ou can simply do
$size = filesize("path/to/file");
Also, there is a ' in your $log declaration that shouldn't be there
I'm running this code once the user submits a form. The idea is to save a new file called "message_timestamp" where timestamp is the current date and time. It need to be saved in the misc folder, which is located in the same directory as this php file.
My code:
if (isset($_POST['submitIssue'])) {
$type = $_POST['typeOfIssue'];
$prefName = $_POST['preferredName'];
$email = $_POST['userEmail'];
$details = $_POST['detailedReport'];
date_default_timezone_set('Atlantic/Bermuda');
$date = new DateTime('now');
$date = $date->format('Y-m-d_H:i:s');
$date = trim($date);
$fileName = "misc/message_" . $date;
$out = "Issue Alert!\nType of issue: " . $type . "\n\nSubmitted by " . $prefName . "\nEmail ID: " . $email . "\nSubmitted at: " . $date . "\n\nDetails: " . $details;
$myFile = fopen($fileName,"w") or die("Cannot open file!");
fwrite($myFile, $out);
fclose($myFile);
}
I've tried removing whitespaces and looking for escape characters. But as long as $date is appended to the filename, I get the error:
fopen(misc/message_2018-02-0210:47:41): failed to open stream: Invalid
argument in C:\MAMP\htdocs\A2\report.php on line 76
I can get it to work if I don't put the timestamp on it. Any suggestions?
This question already has answers here:
Turn off warnings and errors on PHP and MySQL
(6 answers)
Closed 5 years ago.
It prints on the screen when it is correctly entered, but I do not want to do anything when it is entered incorrectly
How can I do that?
<?php
header('Content-type: text/html; charset=utf8');
$api_key = 'local';
$keyword = 'test';
$url = 'test.json' . $api_key . '&' .'keyword' .'=' . $GLOBALS['q'] ;
$open = file_get_contents($url);
$data = json_decode($open, true);
$istatistikler = $data['data'];
if ($data) {
foreach ( $istatistikler as $istatistik ){
echo '<div class="right">';
echo 'Oyun modu: ' . $istatistik['title'] . '<br />' .
'Kazanma: ' . $istatistik['content'] . '<br />' .
'Kazanma: ' . $istatistik['image'] . '<br />' .
'Kazanma: ' . $istatistik['category'] . '<br />' .
'<br />' .
'<hr/>';
$karakter_simge = 'http://google.com' . $istatistik['image'] . '';
echo "<img src=".$karakter_simge." >" ;
echo '</div>';
}
}
?>
Successful output
Failed output
Warning:
file_get_contents(http://localhost/api/detail?X-Api-Key=local&keyword=a):
failed to open stream: HTTP request failed! HTTP/1.1 406 Not
Acceptable in /opt/lampp/htdocs/weather-master/php/php-api.php on line
10
"I do not want to print unsuccessfully"
thank you for your help!
This may be helpful:
$open = #file_get_contents($url);
# sign before a function name (in a call) prevents from showing any warnings (It's a bad practice though).
Good luck!
Change
$open = file_get_contents($url);
into
$open = #file_get_contents($url);
if ($open === false)
die("wrong");
The # suppresses the error message. Using die() will abort the script completely with the given message.
Alternatively, change the condition to !== false and wrap the rest of your "successful" code in its body:
$open = #file_get_contents($url);
if ($open !== false)
{
$data = json_decode...
...
...
}
I guess I overshot the goal here a little, but not even running into code that won't work properly without its data isn't a bad idea at all.