In this code :
$path = "C:\NucServ\www\vv\static\arrays\news.php";
$fp = fopen($path, "w");
if(fwrite($fp=fopen($path,"w"),$text))
{
echo "ok";
}
fclose($fp);
I have this error message:
failed to open stream: Invalid argument
What is wrong in my code?
Your backslashes is converted into special chars by PHP. For instance, ...arrays\news.php gets turned into
...arrays
ews.php
You should escape them like this:
$path = "C:\\NucServ\\www\\vv\\static\\arrays\\news.php";
Or use singles, like this:
$path = 'C:\NucServ\www\vv\static\arrays\news.php';
Also, your if is messed up. You shouldn't fopen the file again. Just use your $fp which you already have.
path error:
$path = 'C:/NucServ/www/vv/static/arrays/news.php';
file lock:
user file_get_contents replace fopen
Related
I want to copy the content from (Bazamod.txt) to (compile.txt) but I get this error:
Warning: fopen(LicenteSi/Test/compile.txt): failed to open stream: No
such file or directory in
/storage/ssd3/361/16261361/public_html/createL.php on line 137
Warning: fwrite() expects parameter 1 to be resource, boolean given in
/storage/ssd3/361/16261361/public_html/createL.php on line 141
Function create_compile_mod($Licence_Name, $Path){
$FilePath = "$Path/compile.txt";
$myFile = fopen($FilePath, "r+");
copy("bazamod/Bazamod.txt", $FilePath);
fwrite($myFile, $FilePath);
}
Thank you!
If you what you need to do is just to copy the contents of Bazamod.txt to compile.txt, by providing a path to compile.txt as argument, then the following function will do the trick:
<?php
function create_compile_mod($Path)
{
$fileContents = file_get_contents("bazamod/Bazamod.txt");
$fileHandle = fopen($Path . "/compile.txt", "r+");
fputs($fileHandle, $fileContents);
fclose($fileHandle);
}
?>
I have not included your $Licence_Name argument as it does not seem to be used, but you can adapt the above code to fit your needs.
Keep in mind that the above code will copy the entire contents of Bazamod.txt and replace the existing contents of compile.txt. If you would just like to append new text, use the "a" access mode instead of the specified "r+", and the text will automatically be added at the bottom of the document.
If you need to add at a specific line, you could go for:
<?php
function create_compile_mod($Path, $lineIndex)
{
$oldContents = file_get_contents("bazamod/Bazamod.txt");
$compileArray = file($Path . "compile.txt");
array_splice($compileArray, $lineIndex, 0, $oldContents);
$newContent = implode(PHP_EOL, $compileArray);
$compileFh = fopen($Path . "compile.txt", "r+");
fputs($compileFh, $newContent);
}
?>
Specify your $lineIndex to be the line number at which you want you content to be put (starting from line 0), and call your function like create_compile_mod("./", 4).
Hello I am trying to read a file which contain a url where I want to redirect I am using this
$file = 'test.txt';
$myfile = fopen($file, "r") or die("Unable to open file!");
$link = fread($myfile,filesize($link));
fclose($myfile);
header("Location: $link");
The browser show me an error "The Page isnĀ“t redirect correctly"
The file test.txt contains
http://www.google.es
See this statement here,
$link = fread($myfile,filesize($link));
^^^^^^^^^^^^^^^
filesize() function expects the file path as it's argument, but you're passing an undefined variable $link.
So the above statement should be like this:
$link = fread($myfile,filesize($file));
Also, use exit(); after header(...); because header(...); itself is not sufficient to redirect the user to a different page.
header("Location: $link");
exit();
You're using the wrong variable for
filesize($link)
which should be $file
filesize($file)
(Edit):
Debugging procedure (and with error reporting set to catch and display).
Having commented out the header and echoing the $link variable
$file = "test.txt";
$myfile = fopen($file, "r") or die("Unable to open file!");
echo $link = fread($myfile,filesize($link));
fclose($myfile);
// header("Location: $link");
...you would have been presented with the following notice/warning:
Notice: Undefined variable: link in /path/to/file.php on line x
Warning: fread(): Length parameter must be greater than 0 in /path/to/file.php on line x
Since fread() couldn't figure out the filesize, it failed to to "read" it.
And of course as Rajdeep stated in his (much better) answer, to add exit; after header. Yours would have (most likely) worked, but it is indeed better practice.
I am writing a PHP script so that I can do a find and replace in a large CSV file. I wrote this script:
// FIND AND REPLACE
$sourcePath = 'custom.csv';
$tempPath = $sourcePath . 'temp';
$source = fopen($sourcePath, 'r');
$target = fopen($tempPath, 'w');
while(!feof($source)) {
$line = preg_replace ("village", "village/",fgets($source));
fwrite($target, $line);
}
fclose($source);
fclose($target);
unlink($sourcePath);
rename($tempPath, $sourcePath);
But I am getting these errors,
Warning: feof() expects parameter 1 to be resource, boolean given
Warning: fgets() expects parameter 1 to be resource, boolean given
Warning: preg_replace(): Delimiter must not be alphanumeric or backslash
$source = fopen($sourcePath, 'r'); isn't returning what you think it is.
It's likely returning false, which typically happens when PHP can't find the file at the path you provided. If you're certain the file exists, you should confirm that the user executing the script has the proper permissions to read the file.
You're second issue regarding preg_replace() is being caused by not using delimiters. They are needed in the first argument.
$line = preg_replace ("/village/", "village/",fgets($source));
However, regular expressions aren't needed with this simple of a replacement. You should instead use str_replace() and the script should run faster.
Your code should look like this:
<?php
$sourcePath = 'custom.csv';
$tempPath = $sourcePath . 'temp';
$source = fopen($sourcePath, 'r');
$target = fopen($tempPath, 'w');
if($source){
while(!feof($source)) {
$line = str_replace("Village\\", "Village",fgets($source));
fwrite($target, $line);
}
} else {
echo "$sourcePath not found, or using the wrong permissions.";
}
fclose($source);
fclose($target);
unlink($sourcePath);
rename($tempPath, $sourcePath);
?>
You are not checking if fopen is actually returning a file pointer resource or a false result. It is likely returning false and throwing the warning that a boolean is provided.
Also, you could use:
$line = str_replace("village", "village/", fgets($source));
I have a problem with copying files to local server. I've got only the last file, plashka_3.png.
Warning: copy(http://rus-yurist.ru/images/plashka_1.png )
[function.copy]: failed to open stream: HTTP request failed!
Warning: copy(http://rus-yurist.ru/images/plashka_2.png )
[function.copy]: failed to open stream: HTTP request failed!
$txt="http://rus-yurist.ru/images/plashka_1.png
http://rus-yurist.ru/images/plashka_2.png
http://rus-yurist.ru/images/plashka_3.png";
$a1=explode("\n",$txt);
$a1=array_unique($a1);
foreach($a1 as $url) {
$ch = curl_init($url);
$path_parts = pathinfo($url);
$path = 'docs/'.$path_parts['basename'];
copy($url, $path);
// file_put_contents($path, file_get_contents($url)); /* not works too, only last file
}
I always use file_put_contents
file_put_contents("Tmpfile.zip", fopen("http://someurl/file.zip", 'r'));
I should trim my $url variable! Thanks to jeroen!
I'm trying:
-to connect to the server
-to list all the files
-to read the files and echo the content.
The connection work well:
$conn_id = ssh2_connect('xxx.com', 22);
$login_result = ssh2_auth_password($conn_id, 'xxxxxx', 'xxxxxxxxx');
$sftp = ssh2_sftp($conn_id);
$dir = "ssh2.sftp://$sftp/home";
$ftp_contents = scanFilesystem($dir);
Then I loop over the directory and get all the filenames, work like a charm:
foreach($ftp_contents as $key => $currentFilename){
echo($currentFilename);
}
Then I try to get the content of the file.... And it doesn't work.
$stream = fopen("ssh2.sftp://$sftp/home/".$currentFilename, 'r');
echo($stream);
There is the output error:
Warning: fopen(ssh2.sftp://Resource id #4/home/myfile.xml) [function.fopen]: failed to open stream: operation failed in /home/xxxxxx/public_html/xxx.php on line 38
The line 38 is the end of the foreach.
I try with:
fopen("ssh2.sftp://$sftp/home/".$currentFilename, 'r');
fopen("ssh2.sftp://$sftp/".$currentFilename, 'r');
fopen("/home/".$currentFilename, 'r');
fopen("home/".$currentFilename, 'r');
Nothing work, always the same type of error, can some one help me please?
Thanks.
update: I try with : $stream = file_get_contents("ssh2.sftp://$sftp/home/$data"); does'nt work either ...
Still got the error :
Warning: file_get_contents() [function.file-get-contents]: Unable to open ssh2.sftp://Resource id #3/xxx.xml on remote host in /home/xxxxxx/public_html/xxxx.php on line 40
I don't have a clue ... can some one help me?
I had the same problem. Resolved by converting the $sftp resource to an integer before using it as a url, which is a string.
Should work like that:
$sftp = (int)$sftp;
$stream = fopen("ssh2.sftp://$sftp/home/".$currentFilename, 'r');