Php url exist or not

How to check if a file exists from a URL in PHP

Today we’ll explain to you how to check if a file exists from a URL in PHP. Using php built-in file_exists() function, we can check whether a file or directory exists on the server or not but will not use it to check if a file exists on a remote server or not.

Sometimes we need to check the given image URL or another file URL that exists or not so this will help you.

Different ways to check file exists on remote server or not

1. Using fopen() function

Here, we will create a custom function to check if a file exists on a remote server or not using fopen() function in PHP.

2. Using get_headers() function

Also, you can check if the URL is working or not by using the below custom function.

3. Using cURL function

You can do the same task using cURL in PHP as shown in the below code:

That’s it for today.
Thank you for reading. Happy Coding.

You may also like.

How to convert a date format in PHP - Clue Mediator

How to convert a date format in PHP

How to convert Camel Case to Snake Case in PHP - Clue Mediator

How to convert Camel Case to Snake Case in PHP

Delete all files from a folder in PHP - Clue Mediator

Delete all files from a folder in PHP

Resize an image using the GD library in PHP - Clue Mediator

Resize an image using the GD library in PHP

How to use session in PHP - Clue Mediator

How to use session in PHP

Load dynamic content in Bootstrap Modal with AJAX, PHP and MySQL - Clue Mediator

Load dynamic content in Bootstrap Modal with AJAX, PHP and MySQL

Leave a Reply Cancel reply

Search your query

Recent Posts

  • Change a Password for MySQL on Linux using Command Line July 27, 2023
  • Executing MySQL Queries Directly From the Command Line July 26, 2023
  • Listing tables and their structure with the MySQL Command Line July 25, 2023
  • How to Import an .sql File into a Remote Server from a Local Machine July 24, 2023
  • How to Copy a File from/to a Remote Server using Command Line July 23, 2023

Tags

Join us

Top Posts

Explore the article

We are not simply proficient at writing blog post, we’re excellent at explaining the way of learning which response to developers.

For any inquiries, contact us at [email protected] .

  • We provide the best solution to your problem.
  • We give you an example of each article.
  • Provide an example source code for you to download.
  • We offer live demos where you can play with them.
  • Quick answers to your questions via email or comment.
Читайте также:  Java localdate to millis

Clue Mediator © 2023. All Rights Reserved.

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.

Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.

Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.

Источник

How to check a url exist or not in php

I m using codes like to check if a url exists but it is not working for sites which are using ssl conection, i mean https://www.example.com type sites Solution: I don’t know if you can use with https. But as an alternative (if you have Curl enabled) you can use the following function: If you just need the HTTP status code you can modify the function like this: If you don’t have Curl you could try the following function: Note that for HTTPS support you should have SSL support enabled.

How to check a url exist or not in php

I want to check the url http://example.com/file.txt exist or not in php. How can I do it?

if(! @ file_get_contents('http://www.domain.com/file.txt'))

This is the easiest way to do it. If you are unfamiliar with the @ , that will instruct the function to return false if it would have otherwise thrown an error

The would use the PHP curl extension:

$ch = curl_init(); // set up curl curl_setopt( $ch, CURLOPT_URL, $url ); // the url to request if ( false===( $response = curl_exec( $ch ) ) ) < // fetch remote contents $error = curl_error( $ch ); // doesn't exist >curl_close( $ch ); // close the resource 
function urlExists($url=NULL) < if($url == NULL) return false; $ch = curl_init($url); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $data = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if($httpcode>=200 && $httpcode <300)< return true; >else < return false; >> 
$filename="http://example.com/file.txt"; if (file_exists($filename)) < echo "The file $filename exists"; >else

Php — Check if url exists using Laravel, You are right! But well there are 2 possibilities, i guess that he wants to know if the domain exists (is registered). In that case get_headers() fits totally in. In the case you want to know if a file on the server exists you have to choose another way. Since he said ‘it works’ it might be the right solution 😉 –

Fast function to check if url exists PHP

I used @file_get_contents and get_headers, both are slow even if the URL is below 1ko, tried to use cURL but it isn’t supported by the server. Is there any fast fucntion to use instead ?

Читайте также:  Java parser class example

using the method suggested by @arkascha you could do something like this:

$url='http://stackoverflow.com'; $options=array( 'http'=>array( 'method' => 'HEAD', 'User-Agent' => $_SERVER['HTTP_USER_AGENT'] ) ); stream_context_get_default( $options ); $headers=get_headers( $url, 1 ); echo $headers[0]; 

It seems fairly quick and you could further parse the response to find if the status is 200 or otherwise.

Frank Koehl did a neat little function to do just that and return the url’s http status.

http status code function

/** * @author Frank Koehl * @src http://frankkoehl.com/2009/09/http-status-code-curl-php/ */ function get_status($url) < // must set $url first. Duh. $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); // do your curl thing here $data = curl_exec($ch); $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return $http_status; >

Php — Check whether image exists on remote URL, I need a reliable way with PHP to check whether the images actually exist at the remote url. I tried various approaches with different PHP libraries, curl, etc., but none of them works well, some of them are downright slow. Given the fact that I need to generate (and check!) about 60 URLS for each book in my …

Check if url is exist or not

I’ve website where visitor would submit links so i would like of the posted url is exist or not so i’ve been using the following function

else< return true; >> $ur = "http://www.google.com"; // example if(url_exist($url))< echo "yea valid"; >else < echo "not valid"; >?> 

but my website become so slow and something not even loading at the step of checking if valid or not valid url so i wonder if there any idea else can do the same without loading too much on my hosting server as the above function !! ~ any help

$headers = @get_headers($url); if(strpos($headers[0],'200')===false)return false; 

So if a url returns anything other than code 200, you will know that url is wrong.

$url = 'http://google.com/'; if( get_headers($url) ) echo "$url exists"; 

Using example from link in the accepted answer:

$url = 'http://www.example.com'; $handle = @fopen($url,'r'); if ($handle !== false) < echo 'Exists'; >else

Reproducing here in case external resource becomes unavailable in the future.

You can try the below code, hope it will work.

$url = 'http://www.example.com/file.jpg'; $file_header = @get_headers($url); if($file_header[0] == 'HTTP/1.1 404 Not Found') < $is_exists = false; >else

How to check if page url has www in it or not with PHP?, Anyway, to check for www when site is not using sub domains, that’s peace of cake. But to check if url has www and your site is using sub domains, that could be tough. The simplest solution to deal with that problem would be to disable www sub domain, if you can.

How to check if a https site exists in php

 $array = get_headers($url); $string = $array[0]; if(strpos($string,"200")) < echo 'url exists'; >else < echo 'url does not exist'; >//this code does not works for ssl connection ?> 

to check if a url exists but it is not working for sites which are using ssl conection, i mean https://www.example.com type sites

I don’t know if you can use get_headers with https.

But as an alternative (if you have Curl enabled) you can use the following function:

If you just need the HTTP status code you can modify the function like this:

If you don’t have Curl you could try the following function:

 else < $port = isset($url_info['port']) ? $url_info['port'] : 80; @$fp=fsockopen($url_info['host'], $port, $errno, $errstr, 10); >if($fp) < stream_set_timeout($fp, 10); $head = "HEAD ".@$url_info['path']."?".@$url_info['query']; $head .= " HTTP/1.0\r\nHost: ".@$url_info['host']."\r\n\r\n"; fputs($fp, $head); while(!feof($fp)) < if($header=trim(fgets($fp, 1024))) < $sc_pos = strpos( $header, ':' ); if( $sc_pos === false ) < $headers['status'] = $header; >else < $label = substr( $header, 0, $sc_pos ); $value = substr( $header, $sc_pos+1 ); $headers[strtolower($label)] = trim($value); >> > return $headers; > else < return false; >> ?> 

Note that for HTTPS support you should have SSL support enabled. (uncomment extension=php_openssl.dll in php.ini).

Читайте также:  Php массив всех букв

If you can’t edit your php.ini and don’t have SSL support it will be difficult to get the (encrypted) headers.

You can check your wrappers (openssl and httpd) with:

$w = stream_get_wrappers(); echo 'openssl: ', extension_loaded ('openssl') ? 'yes':'no', "
\n"; echo 'http wrapper: ', in_array('http', $w) ? 'yes':'no', "
\n"; echo 'https wrapper: ', in_array('https', $w) ? 'yes':'no', "
\n"; echo 'wrappers:
', var_dump($w), "
";

You can check this question on SO for a similar problem.

Http headers - check if url exists php, Server can respond with different status codes as described in RFC 2616 For you task all codes 2xx and 3xx mean success.. Performance note: get_headers by default uses GET method but if you not interested in page content it's better and faster to use HEAD method.

Источник

Simple PHP Code To Check If URL Parameter Is Exist Or Not

In web development, many times it is needed to take the value of URL parameter to process. Below is given an example of URL with value in parameter:

https://domain.come/index.php?city=kolkata

In the above URL “city’ is the parameter and “kolkata” is a value of the parameter city. A parameter in URL always followed by a question mark (?) and the value is written after an equal sign “=” next to the parameter.

Now suppose you want to check if the parameter in URL exists or not in PHP. Yes, often in web development project it may need to check if the parameter exists or not in URL. For example, the OpenWeatherMap API provide weather details for specific location and the location like city and country provided as URL parameter.

Now in this post, I am going to show you the simple PHP code to check if the parameter in URL exist or not.

The PHP code to check if parameter exist

We have taken the above URL with a parameter to give you the example. Here is the URL again – https://domain.come/index.php?city=kolkata.

Now below is the given PHP code which will check if the parameter “city” exists:

The above code is so simple which is using an if condition. $_GET[‘city’] will get the value of “city” parameter and it is checked by PHP if condition.

Now suppose, you want to get the value of “city” parameter inside a PHP variable and want to display it on the webpage. Below is the code which will do that:

The above code will display the value of the parameter city on the web page.

I think you have now understood this tutorial. It is really good if you have got a clear idea about how URL parameter works with PHP. Web developers who work with PHP often have to work with URL parameter and it’s value.

If you still have any confusion then let me know, I will try to solve your problem.

Источник

Оцените статью