Could not create file Using PHP code - php

On the 000webhost.com server
This is the code
<?php
$my_file = fopen("my_test.txt", 'w');
fclose($my_file );
I thank those who help me in advance

Use the function chmod
http://it2.php.net/function.chmod
$file = "my_test.txt";
$fp = fopen($file, 'w');
fwrite($fp, $content);
fclose($fp);
chmod($file, 0777);
Note* fwrite is optional in your case if you need to enter some data in file

Related

Php: I can add lines to the text file, but I can't delete them

I am working on linux (raspbian). Code works locally, but not on the server.
I can't understand what the problem is in the code. The addLineToFile function works perfectly, while deleteLineFromFile does not.
I call the functions with:
if (isset($_POST['delete'])){
$valTemp = $array[$_POST['delete']];
addLineToFile($directory."linkOffline.txt",$valTemp);
deleteLineFromFile($directory."output.txt",$valTemp);
}
where
$directory = "/home/pi/linkFinder/server/";
then
function addLineToFile($dirFile, $line){
$myfile = fopen($dirFile, "a") or die("Unable to open file!");
fwrite($myfile, "\n". $line);
fclose($myfile);
}
function deleteLineFromFile($dirFile, $line){
$contents = file_get_contents($dirFile);
$contents = str_replace($line."\r\n", '', $contents);
file_put_contents($dirFile, $contents);
}
I tried to assign all the permissions to the txt files, with chmod 777.
I also thought there might be some problem with file_get_contents and file_put_contents. So I rewrote the function like this, but it still doesn't work.
function fileGetContents2($url) { //alternativa a file_get_contents
$file = fopen($url, 'r');
$data = stream_get_contents($file);
fclose($file);
return $data;
}
function deleteLineFromFile($dirFile, $line){
$contents = fileGetContents2($dirFile);
$contents = str_replace($line."\r\n", '', $contents);
$myfile = fopen($dirFile, "w") or die("Unable to open file!");
fwrite($myfile, $contents);
fflush($myfile);
fclose($myfile);
}
The txt files are in a /home/user/... folder and are generated by a python script.
Thanks in advance

Trying to find file extension on hidden download url

I am trying to make a script to take a downloadable file and put it onto my ftp server to then update a database. I cannot figure out what the url extention should be to able to make this work.
<?php
$src = fopen('https://www.atf.gov/firearms/docs/0516-ffl-listxlsx/download', 'r');
$dest1 = fopen('first1k.xlsx', 'w');
$dest2 = fopen('remainder.xlsx', 'w');
echo stream_copy_to_stream($src, $dest1, 19759460) . " bytes copied to first1k.txt\n";
echo stream_copy_to_stream($src, $dest2) . " bytes copied to remainder.txt\n";
?>
This is where I am trying to get the data from https://www.atf.gov/firearms/listing-federal-firearms-licensees-ffls-2016
<?php
$file = "0516.xlsx";
$url = 'https://www.atf.gov/firearms/docs/0516-ffl-listxlsx/download' ;
$handle = fopen("0516.xlsx", "w+");
fwrite($handle, file_get_contents($url));
rewind($handle);
$read = htmlentities(fread($handle, filesize($file)));
?>
fixed it out

Fopen problems in php

In PHP im creating a tool to open txt file with an integer in it, increment the number, save the number to a variable and then save and close the file. this is the code i have for it and it doesnt seem to work when i have it on my test server. Can anyone clue me in as to why this isnt working properly?
//opens pclnumber.txt to $handle, saves number to $number, Increments number in text file, saves and closes file
$handle = fopen("pclnumber.txt", "w+");
$number = fread($handle);
fwrite($handle, $number+1);
fclose($handle);
over all you must set reading permission on your file, and than you can use the follow code:
$filename = "pclnumber.txt";
//read file content
$handle = fopen($filename, "r");
$number = fread($handle, sizeof($filename));
fclose($handle);
//update file content
$handleWrite = fopen($filename, "w+");
fwrite($handleWrite, $number+1);
fclose($handleWrite);
Bye,
Marco

How to write a string at the beginning of txt files massively?

I have 100 txt files on my webserver. I have to insert a string e.g. abcdef on the beginning of all of them using php and save them back again. How is this possible ?
Well, google found this.
function prepend($string, $filename) {
$context = stream_context_create();
$tmpname = tempnam(".");
$fp = fopen($filename, "r", 1, $context);
file_put_contents($tmpname, $string);
file_put_contents($tmpname, $fp, FILE_APPEND);
fclose($fp);
unlink($filename);
rename($tmpname, $filename);
}
So you call prepend($string, $filename) for every file and you're done.
Look at opendir, glob, fopen, and fwrite. For example:
foreach (glob("*.txt") as $file) {
$fh = fopen($file, 'c'); //Open file for writing, place pointer at start of file.
fwrite($fh, 'abcdef');
fclose($fh);
}

Write to file in PHP?

Well actually, I already know how to write a file, but how do I write data to a file where the newest data added is at the top? I know how to do this, but the newest information is at the bottom. By the way, just to clarify, I do need all of the data to be displayed, not just the newest one.
Do you mean append or prepend? This will append:
$fp = fopen('data.txt', 'w');
fwrite($fp, '1');
fwrite($fp, '23');
fclose($fp);
This will prepend:
$handle = fopen('file.txt', "r+");
$read = fread($handle, filesize($file));
$data = $newStuff . $read;
if (!fwrite($handle, $data)) {
echo 'fail';
} else {
echo 'success';
}
If you want to append some text to the file content it's better to use another function:
file_put_contents('data.txt', '123', FILE_APPEND)

Categories