php download file, reading problem - php

<?php
function saveFile($url, $filename) {
$data = file_get_contents($url);
$fh = fopen($filename, 'w') or die("can't open file");
fwrite($fh, $data);
fclose($fh);
}
$myFile = "images.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
$data = explode("\n", $theData);
$count = count($data);
for($i=0;$i<=$count;$i++) {
saveFile($data[$i], basename($data[$i]));
}
?>
This is my code and every time i replace a dynamic file name it prints error
Warning: fopen(10017226314.jpg ) [function.fopen]: failed to open stream: Invalid argument in C:\xampp\htdocs\download\download.php on line 4
can't open file
What should be the cause? please help. Thanks

Please replace your fopen function parameter from w to r+
function saveFile($url, $filename) {
$data = file_get_contents($url);
$fh = **fopen($filename, 'r+')** or die("can't open file");
fwrite($fh, $data);
fclose($fh);
}

Related

Code is only saving the last entry of the loop

I want to filter one text file by writing the results to a second text file.
I have a little bit of code and it doesn't work like it should work, it is only writing the LAST line of the first text file into a separate backup text file.
Code:
//filter the ingame bans
$search = "permanently";
$logfile = "ban_list.txt";
$timestamp = time();
// Read from file
$file = fopen($logfile, "r");
while( ($line = fgets($file) )!= false)
{
if(stristr($line,$search))
{
$cache_ig = "ingamebanlist.txt";
$fh = fopen($cache_ig, 'w') or die("can't open file");
$content = "\n";
fwrite($fh, $content);
$content = $line;
fwrite($fh, $content);
fclose($fh);
}
}
I personally do not see any errors in my code, please help.
REMEMBER: It does kind of work, but it only writes the LAST line of the ban_list.txt file into the ingamebanlist.txt file...
What happens with your code is that you open(using write mode), write and close inside your loop so it will only ever write 1 entry then overwrite it until the last entry, thus only saving the last item.
What you want is to have it outside the loop like this:
<?php
$search = "permanently";
$logfile = "ban_list.txt";
$cache_ig = "ingamebanlist.txt";
$timestamp = time();
$read = fopen($logfile, "r") or die("can't read file");
$write = fopen($cache_ig, 'w') or die("can't write to file");
while(($line = fgets($read)) !== false)
{
if(stristr($line,$search))
{
fwrite($write, $line . "\n");
}
}
fclose($write);
fclose($read);
An alternative solution would be to use a instead of w at your fopen since w will:
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.
While a will:
Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
Which would allow your code to work as is without any changes but the line:
$fh = fopen($cache_ig, 'w') or die("can't open file");
To:
$fh = fopen($cache_ig, 'a') or die("can't open file");

How can I keep previous data in a text file?

So basically, I have a form that opens a php file when submitted, i have the php writing to a file, but it will not continue adding values, the text file only has one "1" inside of it when it is supposed to have a "1" inside for every time the form has been submitted. Here is my code.
<?php
$myFile = "Data.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "1";
fwrite($fh, $stringData);
fclose($fh);
?>
Any ideas?
Either open in append only format as Robert Rozas stated: fopen($myFile, 'a+');
Or get the contents and do the append manually some pseudo[esk]-code:
$contents = file_get_contents("somefile.txt");
//generate what to write
$contents .= $whatIGenerated;
$success = file_put_contents("somefile.txt", $contents);
Change this one line to this:
$fh = fopen($myFile, 'a') or die("can't open file");
Using w means write, using a means append.

How to add line to the file without delete

I want to write a line to the end of the file.
My code is:
$myFile = "serialkey.txt";
$fh = fopen($myFile, 'w');
$space = "\r\n";
$stringData = "my new data";
fwrite($fh, $stringData.$space);
fclose($fh);
But when I used this code it deleted all the file and replace "my new data", I want it will not delete my file and append my data to it.
You have it set to write instead of append in
$fh = fopen($myFile, 'w');
It should be
$fh = fopen($myFile, 'a');
Hope this helps.
To insert text without over-writing the end of the file, you'll have to open it for appending (a+ rather than w)
$fh = fopen($myFile, 'a+');

php file read function

I am using the following file read
$myFile = "file.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;
problem is file.txt is generating dynamically and is in different folder so how do I call it in fopen ?
I am trying
$fh = fopen(/path/to/$myFile, 'r');
but obviously its not working and giving me error
PHP Parse error: syntax error, unexpected '/',
how to rectify this and include my path ?
This is a simpler way:
$theData = file_get_contents("/path/to/file/" . $myFile);
First off, it'd be simper just to use file_get_contents()
As to the file path, that needs to be a string, i.e.:
$fh = fopen("/path/to/".$myFile, 'r');
$myPath = "/path/to/file/";
$myFile = "file.txt";
$fh = fopen($myPath.$myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;
I haven't tested it, but I guess it will work that way...
I have tried to solve this problem in my own way. Hope this will help you.
<?php
$myFile = "folder/file.txt";
$myHandle = fopen($myFile, "r");
$myData = fread($myHandle, filesize($myFile));
$rows = explode(" ", $myData);
foreach($rows as $row)
{
print $row;
}
fclose($myHandle);
?>

Is it possible to do "if file exists then append, else create new file" shorter than this

I have the following code but I'm trying to shorten it about one or two lines, as I believe the if is unnecessary. Is there any way the code below can shortened to a singular line?
if(file_exists($myFile))
{
$fh = fopen($myFile, 'a');
fwrite($fh, $message."\n");
}
else
{
$fh = fopen($myFile, 'w');
fwrite($fh, $message."\n");
}
if (file_exists($myFile)) {
$fh = fopen($myFile, 'a');
fwrite($fh, $message."\n");
} else {
$fh = fopen($myFile, 'w');
fwrite($fh, $message."\n");
}
fclose($fh);
==
if (file_exists($myFile)) {
$fh = fopen($myFile, 'a');
} else {
$fh = fopen($myFile, 'w');
}
fwrite($fh, $message."\n");
fclose($fh);
==
$fh = fopen($myFile, (file_exists($myFile)) ? 'a' : 'w');
fwrite($fh, $message."\n");
fclose($fh);
== (because a checks if the file exists and creates it if not)
$fh = fopen($myFile, 'a');
fwrite($fh, $message."\n");
fclose($fh);
==
file_put_contents($myFile, $message."\n", FILE_APPEND);
...of course, file_put_contents() is only better if it is the only write you perform on a given handle. If you have any later calls to fwrite() on the same file handle, you're better going with #Pekka's answer.
Umm... why? a already does what you need out of the box.
Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
$method = (file_exists($myFile)) ? 'a' : 'w';
$fh = fopen($myFile,$method);
fwrite($fh, $message."\n");
fopen(). mode a all you need.
$fh = (file_exists($myFile)) ? fopen($myFile,'a') : fopen($myFile,'w');
fwrite($fh, $message."\n");
$fh = file_exists($myFile) ? fopen($myFile, 'a') : fopen($myFile, 'w');
fwrite($fh, $message."\n");
The append mode already does just what you describe. From the PHP manual page for fopen:
'a': Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
According to the php manual this should be enough. See the description of "a"
fopen($myFile, "a");
fwrite($fh, $message."\n");
I believe the a (append) mode does that already... append if exists, else create new
fopen($myFile, "a");
$method = (file_exists($myFile)) ? 'a' : 'w';
$fh = fopen($myFile,$method);
fwrite($fh, $message."\n");
Isn't it $myFile contains absolute/relative path..?
Using the SPL / Standard PHP Library:
# addfile.php
$file = new \SplFileObject( __DIR__.'/foo.txt', 'a' );
var_dump( file_exists( $file->getFilename() ) );
$ php addfile.php
bool(true)

Categories