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()
Related
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';
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
I have a simple code that checks whether a file exists or not in the server.
<?php
$filename = 'www.testserver/upload/productimg/IN.ZL.6L_sml.jpg';
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
Problem: My local webserver says that the file does not exist even though when I copy paste the url in the browser, I can see the image.
How can I make the condition say that the file does not really exist?
Another option would be, making a request and checking for the response headers:
<?php
$headers = get_headers('http://www.example.com/file.jpg');
if($headers[0]=='HTTP/1.0 200 OK'){
return true; // file exists
}else{
return false; // file doesn't exists
}
?>
Code live example:
http://codepad.viper-7.com/qfnvme
for real path of image you can try:
<?php
$filename = 'www.testserver/upload/productimg/IN.ZL.6L_sml.jpg';
$testImage = #file_get_contents($filename);
if ($testImage != NULL) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
Use
$filename = $_SERVER["DOCUMENT_ROOT"]."/upload/productimg/IN.ZL.6L_sml.jpg";
for testing the file.
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;
Let say I want create file call style.css in /css/ folder.
Example : When I click Save button script will create style.css with content
body {background:#fff;}
a {color:#333; text-decoration:none; }
If server cannot write the file I want show error message Please chmod 777 to /css/ folder
Let me know
$data = "body {background:#fff;}
a {color:#333; text-decoration:none; }";
if (false === file_put_contents('/css/style.css', $data))
echo 'Please chmod 777 to /css/ folder';
You can use the is_writable function to check whether the file is writable or not.
For example:
<?php
$filename = '/path/to/css/style.css';
if (is_writable($filename)) {
echo 'The file is writable';
} else {
echo 'Please chmod 777 to /css/ folder';
}
?>
is the function you may want to use
http://php.net/manual/en/function.fopen.php to open the file
http://php.net/manual/en/function.fwrite.php to write your css
http://php.net/manual/en/function.fclose.php to close the file
or use
http://php.net/manual/en/function.file-put-contents.php just one command to master them all
if you fopen the file and the result of the operation is false then you can't write the file (maybe for permissions, maybe for UID mismatch in safe mode)
file_put_contents (php5 and upper) php calls fopen(), fwrite() and fclose() for you and return false if something id wrong (you should make yourself sure that false is really the boolean value though).
fopen with 'w' flag
<?php
$filename = 'test.txt';
$somecontent = "Add this to the file\n";
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>
http://php.net/manual/en/function.fwrite.php | Example 1