PHP wamp using relative path failed to write to text document - 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

Related

download file to my server with php with a url without file name

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

PHP unlink removes file, but file still exists

I have a very simple function:
unlink($oldPicture);
if (is_readable($oldPicture)) {
echo 'The file is readable';
} else {
echo 'The file is not readable';
}
}
The file shows not readable after execution and disappears from the file directory. However, it's still available when accessed from the browser despite not being cached (opened the file on separate browsers for testing). Is there something I am missing here? Is the file being cached by the server? That is the only explanation I could come up with.
Try something like:
if (is_file($oldPicture)) {
chmod($oldPicture, 0777);
if (unlink($oldPicture)) {
echo 'File deleted';
} else {
echo 'Can\'t remove file';
}
} else {
echo 'File does not exist';
}
Make sure you have full path for $oldPicture
Example:
$oldPicture = dirname(__FILE__) . '/oldpicture.png';

Why can't this header location thing works

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()

PHP unlinking not working with variable

Going out of my mind with php unlinking
Here is my delete file script
$pictures = $_POST['data'];
//print_r ($pictures);
$imageone = $pictures[0];
$filename = "file:///Users/LUJO/Documents/CODE/REVLIVEGIT/wp-content/uploads/dropzone/" . $imageone;
echo $filename;
if (is_file($filename)) {
chmod($filename, 0777);
if (unlink($filename)) {
echo 'File deleted';
} else {
echo 'Cannot remove that file';
}
} else {
echo 'File does not exist';
}
The above does not work, error response is file does not exist
however if i change the filename path to this (the echo data from the echo above)
$filename = "file:///Users/LUJO/Documents/CODE/REVLIVEGIT/wp-content/uploads/dropzone/1420291529-whitetphoto.jpeg "
works fine and deletes the image.
Why can i not use the $imageone variable?
Do a print_r($pictures) to see if $pictures[0] is indeed the filename you're looking for.
Also note that if $pictures[0] is "//windows/*" you'll loose your windows if the user running PHP has administrative rights... so just using $pictures=$_POST["data"] is very VERY unsafe!

Unable to read file in directory with file_get_contents - PHP

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.

Categories