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.";
}
}
Related
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";
}
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.
When accessing a file in PHP, it's possible to escape from the directory using ".." which can lead to a security risk. Is there any way to validate that a file is in a specified directory? There doesn't seem to be a built-in function for it.
This is not a secure way to check if the file exists in the expected location.. you should do the following.
$base = '/expected/path/';
$filename = realpath($filename);
if ($filename === false || strncmp($filename, $base, strlen($base)) !== 0) {
echo 'Missing file or not in the expected location';
}
else {
echo 'The file exists and is in the expected location';
}
There is a very good example on php.net
http://php.net/manual/en/function.file-exists.php
<?php
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
How can i test if a directory already exist and if not create one in PHP?
Try this:
$filename = "/tmp";
if (!file_exists($filename))
echo $filename, " does not exist";
elseif (!is_dir($filename))
echo $filename, " is not a directory";
else
echo "Directory ", $filename, " already exists";
file_exists checks if the path/file exists and is_dir checks whether the given filename is a directory.
Edit:
to create the directory afterwards, call
mkdir($filename);
To expand on the answer above based on the questioner's comments:
$filename = "/tmp";
if (!is_dir($filename)) {
mkdir($filename);
}
You need to use mkdir() to actually make the directory.
Try this:
$dir = "/path/to/dir";
if(is_dir($dir) == false)
mkdir($dir);
If you want the complete path the be created (if not present), set recusive parameter to true.
See documentation of mkdir for more information.
Use This:
if(file_exists("Directory path") && is_dir("Directory path"))
{
//Your Code;
}