PHP unlink removes file, but file still exists - php

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';

Related

Simple test for file_exist fails

Simplest bit of test code:-
<?php
if (file_exists('https://mywebsite/testarea/test.html')) {
echo 'File exists';
} else {
echo 'Not found';
}
?>
I run this test from my localhost (wamp). Why does this not find the file? I've double checked that it exists in the path specified. Help please.
You can use file_get_contents()
if (file_get_contents('https://mywebsite/testarea/test.html')) {
echo 'File exists';
} else {
echo 'Not found';
}
Or just built this with the help of How to check if a file exists from a url
$ch = curl_init('https://mywebsite/testarea/test.html');
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code == 200) {
echo 'File exists';
} else {
echo 'Not found';
}
file_exists() checks whether a file or directory exists in same system where your script is running.
<?php
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
If you are look for a file in remote place via http than use get_header()
<?php
$url = 'https://mywebsite/testarea/test.html';
$array = get_headers($url);
$string = $array[0];
if(strpos($string,"200")) {
echo 'url exists';
} else {
echo 'url does not exist';
}
?>
You need to use Path to the file or directory . See file_exists manual : https://www.php.net/manual/en/function.file-exists.php
and for file functions wrapper support : https://www.php.net/manual/en/wrappers.php
For your case something like,
<?php
if (file_exists('./testarea/test.html')) {
echo 'File exists';
} else {
echo 'Not found';
}
?>
On windows, use //computername/share/filename or \computername\share\filename to check files on network shares.
You are passing a file's url that is hosted somewhere else on a remote server rather than on your computer that's why it's not able to locate it.
If you have the same file in your localhost folder or anywhere on your computer where wamp has read access, it will pass through the check smoothly.
However, if you want to check if a specific url exists, then you might want to have a look at get_headers function:
$headers = get_headers('https://mywebsite/testarea/test.html');
if($headers && strpos($headers[0], 200)) {
echo 'URL exists';
} else {
echo 'URL does not exist';
}

PHP wamp using relative path failed to write to text document

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

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!

copy images issue in php

I want to copy some images from one folder to another, I do not want to copy them all.
Here is my code but it give me an issue failed to open stream
$image = "a.jpg";
$srcfile='uploads/listings/my_from/'.$image;
$dstfile='uploads/listings/my_to/images/';
copy($srcfile, $dstfile);
What am I missing? Can I do anyhting else so that I can copy selected images to destination without deleting it?
Note: both of these folders are on the same server and same project. Should I do it by curl?
It might pay to do some error checking as well, but you just need to add the full image path to your $dstfile variable:
<?php
$image = "a.jpg";
$srcfile='uploads/listings/my_from/'.$image;
$dstfile='uploads/listings/my_to/images/'.$image;
echo 'Attempting to copy "'.$srcfile.'" to "'.$dstfile.'"<br />';
if(file_exists($srcfile)) {
if(file_exists($dstfile)) {
echo 'Cannot copy file, destination file already exists';
} else {
if(is_writable(dirname($dstfile))) {
if(copy($srcfile,$dstfile)) {
echo 'File successfully copied';
} else {
echo 'File could not be copied';
}
} else {
echo 'Destination is not writable';
}
}
} else {
echo 'Cannot copy file, source file doesnt exist';
}
?>
if you are sure that file a.jpg exists, then this should work
$srcfile='uploads/listings/my_from/'.$image;
$dstfile='uploads/listings/my_to/images/'.$image;
copy($srcfile, $dstfile);
note: destination file name must be specified.
You should check it exists really.
try:
$image = "a.jpg";
$srcfile='uploads/listings/my_from/'. $image;
$dstfile='uploads/listings/my_to/images/' . $image;
if(!file_exists($srcfile) {
throw new \Exception("File does not exist!");
}
copy($srcfile, $dstfile);

Categories