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';
}
Related
I am getting data from a database and I want to check whether the data contains an image or not. I tried with following code and I want to know if is this a good solution for this.
$headers = get_headers('https://cdn.discordapp.com/attachments/999921405858680857/1011168600410820679/1656572127-blog-2.JPG', 1);
if (strpos($headers['Content-Type'], 'image/') !== false) {
echo "Work";
} else {
echo "Not Image";
}
No. When verifying files, you should never rely on headers. Headers can easily be modified by whomever is providing you with the file. A much more reliable option would be to use the PHP fileinfo extension.
<?php
// Fileinfo extension loaded
$finfoAvailable = false;
// Download the file
$imgUrl = 'https://cdn.discordapp.com/attachments/999921405858680857/1011168600410820679/1656572127-blog-2.JPG';
// Use basename() function to return the base name of file
$file_name = basename($imgUrl);
// Get the file with file_get_contents()
// Save the file with file_put_contents()
if (file_put_contents($file_name, file_get_contents($imgUrl)))
{
// Checks if fileinfo PHP extension is loaded.
if (!extension_loaded('fileinfo'))
{
// dl() is disabled in the PHP-FPM since php7 so we check if it's available first
if(function_exists('dl')
{
// Check if OS is Windows
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
{
if (!dl('fileinfo.dll'))
{
echo "Unable to load fileinfo.dll extension";
}
else
{
$finfoAvailable = true;
}
}
// OS is something else than Windows (like linux)
else
{
if (!dl('fileinfo.so'))
{
echo "Unable to load fileinfo.so extension";
}
else
{
$finfoAvailable = true;
}
}
}
else
{
echo "Unable to load PHP extensions as dl() function is not available";
}
}
else
{
$finfoAvailable = true;
}
// If fileinfo is available
if($finfoAvailable)
{
$finfo = finfo_open( FILEINFO_MIME_TYPE );
$mtype = finfo_file( $finfo, $file_name );
if(strpos($mtype, 'image/') === 0)
{
echo "File is an image";
}
else
{
echo "File is not an image";
}
}
else
{
echo "PHP extension fileinfo in not available";
}
}
else
{
echo "File downloading failed.";
}
?>
How to check whether file exist or not in given URL? I can't verify that the file is found or not in a given URL:
$url = "https://content.gomasterkey.com/images/watermark.aspx?imageurl=/uf/1002/property/43644/581330_Image.JPG&width=640&group=1575&module=1&watermarktype=default&position=TopRight";
if(file_exists($url)){
echo "exist";
}else{
echo "not exist";
}
if(file_get_contents($url)){
echo "exist";
}else{
echo "not exist";
}
You can use fopen() to check either file exists or not.
$url = "https://content.gomasterkey.com/images/watermark.aspx?imageurl=/uf/1002/property/43644/581330_Image.JPG&width=640&group=1575&module=1&watermarktype=default&position=TopRight";
// Open for reading only.
if(#fopen($url, 'r') ){
echo 'File Found';
}else{
echo 'file not found';
}
Usinge fopen() function you can check the remote file exists or not.
// Remote file url
$remoteFile = 'https://content.gomasterkey.com/images/watermark.aspx?imageurl=/uf/1002/property/43644/581330_Image.JPG&width=640&group=1575&module=1&watermarktype=default&position=TopRight';
// Open file
$handle = #fopen($remoteFile, 'r');
// Check if file exists
if(!$handle){
echo 'File not found';
}else{
echo 'File exists';
}
You should use get_headers, because its less overhead for just checking if a file exists
$headers=get_headers($url);
if(stripos($headers[0],"200 OK")){
echo "file exists";
} else {
echo "file doesn't 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';
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!
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.