Get file name of a URL in PHP

php get file name from path
php get current file name
php get file path
php get file extension
php pathinfo
php get uploaded file name
get image name from url php
php get url

I want to get the file name from a URL. But the problem is it's not ending with an extension.

For example, http://something.com/1245/65/. On clicking this URL, we will get a PDF file. How do I store the file name of that file in a variable?


<?php
  header('Content-Type: text/plain');

  $curl = curl_init('http://localhost/fakefile.php');

  curl_setopt($curl, CURLOPT_HEADER, true);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD');

  if (($response = curl_exec($curl)) !== false)
  {
    if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200')
    {
      var_dump($response);

      $reDispo = '/^Content-Disposition: .*?filename=(?<f>[^\s]+|\x22[^\x22]+\x22)\x3B?.*$/m';
      if (preg_match($reDispo, $response, $mDispo))
      {
        $filename = trim($mDispo['f'],' ";');

        echo "Filename Found: $filename";
      }
    }
  }

  curl_close($curl);

That would parse the Content-Disposition line for the filename=foo.bar information (assuming it does render the file directly out using this method.)

Get only filename from url in php without any variable values which , Use parse_url() as Pekka said: <? php $url = 'http://www.example.com/search.php?arg1=arg2'; $parts = parse_url($url); $str = $parts['scheme']. '://'. Method 2: Using PHP Curl: The cURL stands for ‘Client for URLs’, originally with URL spelled in uppercase to make it obvious that it deals with URLs. It is pronounced as ‘see URL’. The cURL project has two products libcurl and curl. Steps to download file: Initialize a file URL to the variable; Create cURL session


If you issue a GET request for http://something.com/1245/65/ and it returns you a PDF file -- that is, something with Content-Type = application/pdf -- you have absolutely no way of knowing the actual name of the .pdf file that was sent to you.

In the web application that I work on, I generate and combine PDF files and stream them directly to the browser in response to just such a GET request - no actual file even exists so there definitely is not filename.

If you are in control of the server you could invent a way to communicate the name to the requester, or if it's your PHP code that wants to know (which runs on the server) you could provide some other server-side hook to find the filename.

PHP basename() Function, echo basename($path) ."<br/>"; //Show filename, but cut off file extension for ".​php" files echo basename($path,".php"); ?> The output of the code above will be:. What I am trying to figure out is how can I get the filename.ext from a url string (not necessarily the active URL, maybe a user input url). Currently I am looking to get the file name and extension of user provided URLs containing images.


Curiously enough, trying the same code you have above was making my webserver hang too. I replaced this code:

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD');

With this:

curl_setopt($ch, CURLOPT_NOBODY, true);

How to get file name from a path in PHP?, Also we can use the function pathinfo() which creates an array with the parts of the path we want to use. Syntax: $filename = pathinfo(path); when we want to access the file name we will use $filename['basename']. Answer: Use the PHP $_SERVER Superglobal Variable You can use the $_SERVER built-in variable to get the current page URL in PHP. The $_SERVER is a superglobal variable, which means it is always available in all scopes. Also if you want full URL of the page you'll need to check the scheme name (or protocol), whether it is http or https.


Say if you have a url

$url = 'www/htdocs/lib/inc.php'

 $pathifo = pathinfo($url)// Return Array 

when you say $pathinfo['PATHINFO_FILENAME'] //It would output inc

Download file from URL using PHP, Use basename() function to return the base name of file. $file_name = basename ( $url );. // Use file_get_contents() function to get the file. // from url and use  Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.


Get filename from URL with extension – James' Desk, Get the filename from the URL or path using Javascript: Consider the URL: var url = "http://www.example.com/my_page.html" Method 1:  PHP Forms PHP Form Handling PHP Form Validation PHP Form Required PHP Form URL/E-mail PHP Form Complete PHP Advanced PHP Date and Time PHP Include PHP File Handling PHP File Open/Read PHP File Create/Write PHP File Upload PHP Cookies PHP Sessions PHP Filters PHP Filters Advanced PHP JSON PHP OOP


How to get folder name out of a URL? - PHP, Can somebody tell me how to get the name of the folder name below? http://www​.domain.com/folder1/folder2/THIS_ONE/file.php I would like to  In fact, the manual section "Handling file uploads" uses basename() in an example, but this will NOT extract the file name from a Windows path such as C:\My Documents\My Name\filename.ext. After much frustrated coding, here is how I handled it (might not be the best, but it works):


How to Get the Current Page URL of a Web Page Using PHP, The $_SERVER['REQUEST_URI'] returns the directory pathway and file name of the page you are on. Actual PHP Output of this Page. http://www. Get the directory name, filename and extension with pathinfo() PHP’s pathinfo() function returns an associative array containing the basename, dirname, extension and (from PHP 5.2.0) the filename without the extension.