I have a file present in a directory content that contains a file to read from. On my MAMP installation, content/ is present in the webroot folder (/Applications/MAMP/htdocs/testApp/content). I am unable to read the file (test.txt) from the folder. The function I am using is:
function getDataFromLibrary($tgt_url) {
//header('Content-Type: text/html; charset=UTF-8');
echo "inside get data from lib.";
if(file_exists($tgt_url)) {
echo "File does exist.";
if(is_readable($tgt_url)) {
echo "file is readable.";
} else {
echo "file is not readable.";
}
} else {
echo "File does not exist.";
}
echo "tgt url = ".$tgt_url."the end.";
$file_contents = file_get_contents(urlencode($tgt_url));
if($file_contents === false) {
echo "could not read file contents.";
} else {
echo "file contents read.";
}
echo "Target url=".var_dump($tgt_url)."again the end.";
echo "File contents = ".$file_contents;
//$file_contents_replaced = str_replace($file_contents, "\"", "\\\"");
//echo $file_contents_replaced;
//var_dump($file_contents);
//return $tgt_url;
}
and the output I get when $tgt_url = content/test.txt is:
inside get data from lib.File does exist.file is readable.tgt url = content/test.txtthe end.could not read file contents.string(33) "content/test.txt" Target url=again the end.File contents =
The file does exist and is also readable. Where am I going wrong?
Any help is most welcome.
Edit:
I turned on the following errors in php.ini:
display_errors = On
and in the script, I included the lines:
error_reporting(E_ALL);
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
The output is:
inside get data from lib.File does exist.file is readable.tgt url = content/test.txtthe end.
Warning: file_get_contents(content%2Ftest.txt): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/testApp/index.php on line 323
could not read file contents.string(33) "content/test.txt" Target url=again the end.File contents =
As you check at the beginning everything $tgt_url you shouldn't after all checks change what you checked. Now you checked if file exists etc for $tgt_url and finally you wanted to get file urlencode($tgt_url) what will be not the same.
After called urlencode(), the $tgt_url will become to "content%2Ftest.txt", not "content/test.txt" again.
You can called urlencode() at the front of the getDataFromLibrary(), it will show: File does not exist.
Don't use urlencode() inside file_get_contents() functions.
Related
I can download a csv file via a fixed url. The url just doesn't have a file name.
When I open the url in a browser, the file export.csv is generated and after about 10 sec the download starts.
Now I want to download this export csv file directly to my own server,
I have the code below, but it doesn't work in my case.
Can you help me? my thanks are big
<?php
$url =
'https://www.example.com/product-export/link123456789';
$file_name = basename($url);
if (file_put_contents($file_name, file_get_contents($url)))
{
echo "File downloaded successfully";
}
else
{
echo "File downloading failed.";
}
?>
As the generated output filename is always export.csv, we may use the following to specify the filename:
$file_name = dirname(__FILE__) . "/export.csv";
(make sure the directory is write-permitted so that the php can save the file "export.csv")
Hence, the code will be:
<?php
$url = 'http://www.createchhk.com/SOanswers/export1a.csv';
//$file_name = basename($url);
$file_name = dirname(__FILE__) . "/export.csv";
if (file_put_contents($file_name, file_get_contents($url))) {
echo "File downloaded successfully";
} else {
echo "File downloading failed.";
}
?>
For your cronjob, it will be something like :
1 1 * * * php /path_to_the_php/test.php
kindly help me.
my env as follow : window 7 x64,wamp x64 v2.5.
apache web root directory : D:/wampProject/.
I want to write to the target text file in D:/wampProject/chapter02/orders.txt
syntaxTest.php in D:/wampProject/,as below.
<?php
header("Content-type: text/html; charset=utf-8");
$document_root= $_SERVER['DOCUMENT_ROOT'];
//#1 $filename = "$document_root"."chapter02/orders.txt"; //works
//#2 $filename="D:/wampProject/"."chapter02/orders.txt"; //works
//#3 $filename="$document_root../orders.txt";
//#3 output: filename D:/wampProject/../orders.txt
//showed contents added in the page after refresh,
//but text file didn't get written refer pics below .
//Got the wrong dir? seems like page result shows 'cache' written content
//added by absolute path in all time by the script.
//#4 $filename="D:/wampProject/"."../orders.txt";
//filenameD:/wampProject/../orders.txt as failed as #3.
if (is_writable($filename)) {
echo 'The file is writable';
} else {
echo 'The file is not writable';
} ;
echo '<br/>';
if (is_readable($filename)) {
echo 'The file is readable';
} else {
echo 'The file is not readable';
};
$fp=fopen($filename,'ab');
$outputstring="address string"."\n";
echo $outputstring.'<br />';
flock($fp,LOCK_EX);
fwrite($fp,$outputstring,strlen($outputstring));
flock($fp,LOCK_UN);
fclose($fp);
echo "fclosed".'<br />';
$filecon=file_get_contents($filename);
echo $filecon."<br />";
?>
page result
orders.txt after using 4 filename paths above
function actionExit()
{
$fake = file_get_contents("fake.txt");
$header='Location: '. $fake;
file_put_contents ("data.txt",$header); //It shows Location: http://google.com
header($header);
exit();
}
The file data.txt contains Location: http://google.com
However, it doesn't show anything just a blank screen.
I think that you can't write your file due to missing permissions, so that file_put_contents() fails.
You can check it with is_writable()
$filename = 'test.txt';
if (is_writable($filename)) {
echo 'The file is writeable';
} else {
echo 'The file isn\'t writeable';
}
If the file isn't writeable, you can set permissions with chmod()
In my personal experience, you cannot delete something that's in use, I think unlink() will not work if the target file is in use, how do you handle that?
<?php unlink ("notes.txt"); // how to handle if file in use? ?>
unlink returns a boolean that you can use to detect if deletion was successful or not:
<?php
$file = fopen('notes.txt','w');
fwrite($file,'abc123');
$resul = unlink("notes.txt"); // ◄■■■ ATTEMPT TO DELETE OPEN FILE.
if ( $resul )
echo "File deleted";
else echo "File NOT deleted (file in use or protected)";
fclose($file);
?>
You might see a warning message on screen, so turn off warnings and let your code (the if($resul)) handle the problem.
Edit :
It's possible to detect whether the file is in use or it is protected by using the function is_writable, next code shows how :
<?php
$file = fopen("notes.txt","w"); // ◄■■■ OPEN FILE.
fwrite($file,"abc123");
$resul = unlink("notes.txt"); // ◄■■■ ATTEMPT TO DELETE FILE.
if ( $resul ) // ◄■■■ IF FILE WAS DELETED...
echo "File deleted";
elseif ( is_writable( "notes.txt" ) ) // ◄■■■ IF FILE IS WRITABLE...
echo "File NOT deleted (file in use)";
else echo "File NOT deleted (file protected)";
fclose($file);
?>
To test previous code, open the properties of the file and set it to readonly and hidden, then run the code.
I am trying to delete a file from folder in php here is my model function
function deleteFiles()
{
$file = "http://localhost/copycopy/img/uploaded/long.jpeg";
if(is_file($file))
{
#unlink($file); // delete file
echo $file.'file deleted';
}
else
{
echo "no file";
}
}
but I always see "no file" and file is never deleted, file is in folder,because the url in $file actually displays the file in browser
help me
instead of using web url
$file = "http://localhost/copycopy/img/uploaded/long.jpeg";
use local path to file:
$file = $pathToYourWebSite . "/copycopy/img/uploaded/long.jpeg";
be sure to set $pathToYourWebSite to real location of your website;