How to check whether file exist or not in given URL? - php

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";
}

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

How to check if file handle still points to file

Is there any way in PHP to test if a fileHandle resource still points to the file in the file system that it opened? e.g. in this code:
<?php
$filename = './foo.txt';
$fileHandle = fopen($filename, 'c');
$unlinked = unlink($filename);
if (!$unlinked) {
echo "Failed to unlink file.";
exit(0);
}
file_put_contents($filename, "blah blah");
// The file $filename will exist here, but I want to check that
// the $fileHandle still points to the file on the filesystem name $filename.
?>
At the end of that code, the fileHandle still exists but no longer references the file './foo.txt' on the filesystem. It instead still holds a reference to the original file that has been unlinked, i.e. it has no active entry in the filesystem, and writing data to the fileHandle will not affect the contents of the file called $filename.
Is there a (preferably atomic) operation to determine that $fileHandle is now 'invalid' in the sense that it no longer points to the original file?
It's not an atomic operation but on Linux (and other Unix systems) it's possible to check using stat on the filename, and fstat on the fileHandle, and comparing the inodes returned from each of those functions.
This doesn't work on Windows, which is less than optimal.
<?php
$filename = './foo.txt';
$fileHandle = fopen($filename, 'c');
$locked = flock($fileHandle, LOCK_EX|LOCK_NB, $wouldBlock);
if (!$locked) {
echo "Failed to lock file.";
exit(0);
}
$unlinked = unlink($filename);
if (!$unlinked) {
echo "Failed to unlink file.";
exit(0);
}
// Comment out the next line to have the 'file doesn't exist' result.
file_put_contents($filename, "blah blah ");
$originalInode = null;
$currentInode = null;
$originalStat = fstat($fileHandle);
if(array_key_exists('ino', $originalStat)) {
$originalInode = $originalStat['ino'];
}
$currentStat = #stat($filename);
if($currentStat && array_key_exists('ino', $currentStat)) {
$currentInode = $currentStat['ino'];
}
if ($currentInode == null) {
echo "File doesn't currently exist.";
}
else if ($originalInode == null) {
echo "Something went horribly wrong.";
}
else if ($currentInode != $originalInode) {
echo "File handle no longer points to current file.";
}
else {
echo "inodes apparently match, which should never happen for this test case.";
}
$closed = fclose($fileHandle);
if (!$closed) {
echo "Failed to close file.";
exit(0);
}

How do I know if my image exists on the server? (PHP)

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.

check if url exists in php [duplicate]

This question already has answers here:
How can one check to see if a remote file exists using PHP?
(24 answers)
Closed last month.
if (!(file_exists(http://example.com/images/thumbnail_1286954822.jpg))) {
$filefound = '0';
}
why won't this work?
if (!file_exists('http://example.com/images/thumbnail_1286954822.jpg')) {
$filefound = '0';
}
The function expects a string.
file_exists() does not work properly with HTTP URLs.
file_exists checks whether a file exist in the specified path or not.
Syntax:
file_exists ( string $filename )
Returns TRUE if the file or directory specified by filename exists; FALSE otherwise.
$filename = BASE_DIR."images/a/test.jpg";
if (file_exists($filename)){
echo "File exist.";
}else{
echo "File does not exist.";
}
Another alternative method you can use getimagesize(), it will return 0(zero) if file/directory is not available in the specified path.
if (#getimagesize($filename)) {...}
You can also use PHP get_headers() function.
Example:
function check_file_exists_here($url){
$result=get_headers($url);
return stripos($result[0],"200 OK")?true:false; //check if $result[0] has 200 OK
}
if(check_file_exists_here("http://www.mywebsite.com/file.pdf"))
echo "This file exists";
else
echo "This file does not exist";
Based on your comment to Haim, is this a file on your own server? If so, you need to use the file system path, not url (e.g. file_exists( '/path/to/images/thumbnail.jpg' )).
for me also the file_exists() function is not working properly. So I got this alternative solution. Hope this one help someone
$path = 'http://localhost/admin/public/upload/video_thumbnail/thumbnail_1564385519_0.png';
if (#GetImageSize($path)) {
echo 'File exits';
} else {
echo "File doesn't exits";
}
Check code below
if ($user->image) {
$filename = "images/" . $user->image;
if (file_exists($filename)) {
echo '<br />';
echo "File exist.";
} else {
echo '<br />';
echo "File does not exist.";
}
}

Categories