PHP

How to extract file name from given URL ?

CK CK 3788 
Created at  
443 0 0
That's not too difficult. First, you want to chop anything off after the last You can use the command strrpos() to find the position, and then substr to chop it off. However, / is the escape chatacter, so you need to escape that one (/). Finally you need to add +1 the the position since you don't want to include the in the result, so:

CODE
$string = substr($string,strrpos($string,'/')+1);


Then, if you want to get the file extension, do the same thing for the . Because there can be more . in a file name you also search for the latest occurence.

CODE
$string = substr($string,0,strrpos($string,'.'));


And there you have it..
Tags PHP copy file download file extract_fileext extract_filename Facebook X
Comments 0
Similar posts
  1. How to download a file from a certain URL to server ?
    419
  2. File downloading via HTTP
    445
  3. How to block a connection from the certain site ?
    350
  4. XMLRPC example
    760
  5. How to upload attachments such as image by metaWeblog API ?
    391
  6. How to post blog content by metaweblog API ?
    486
  7. ISO 8601 format time format string conversion
    534
  8. HTML redirection in PHP
    398
  9. Get the actual filesize for a size above 2GB in Windows
    427
  10. Posting to a Wordpress Blog Via XMLRPC Using Blogger API
    763
  11. How to convert W3C Date Time Format ?
    453
  12. How to put timeout when opening URL by fopen ?
    440
  13. How to limit by Timeout when opening URL ?
    457
  14. foreach loop
    442
  15. Can PHP switch in strings ?
    391
  16. PHP Switch Example
    427
  17. How to send email in PHP ?
    889
  18. How to pass variables via url
    468
  19. How to declare array in PHP like C/C++
    670
  20. PHP date and time formatting
    428
  21. How to redirect browser in PHP ?
    484
  22. How to get current filename as variable ?
    768
  23. email validation, simple
    412
  24. How to ban / block IP addresses
    509
  25. for loop - simple
    449
  26. form action - detect form post
    428
  27. How to make random number ?
    517
  28. How to force cookies, disable php sessid in URL ?
    555
  29. How to check if a file exists ?
    786
  30. How to include a file with error reporting ?
    428
  1. Email Validation Snipet in PHP
    378
  2. PHP socket programming to get content with post method
    409
  3. Get Remote IP Address in PHP
    494
  4. How to remove HTML tags in text string?
    405
  5. Directory listing in PHP
    511
  6. Seconds to String in PHP
    435
  7. How to delete file on certain path ?
    388
  8. ASCII Artwork Generator from image file
    469
  9. How to limit by Timeout when opening URL ?
    457
  10. How to put timeout when opening URL by fopen ?
    440
  11. How to convert W3C Date Time Format ?
    453
  12. Get the actual filesize for a size above 2GB in Windows
    427
  13. HTML redirection in PHP
    398
  14. ISO 8601 format time format string conversion
    534
  15. How to download a file from a certain URL to server ?
    419
  16. File downloading via HTTP
    445
  17. XMLRPC example
    760
  18. How to upload attachments such as image by metaWeblog API ?
    391
  19. How to post blog content by metaweblog API ?
    486
  20. Posting to a Wordpress Blog Via XMLRPC Using Blogger API
    763
  21. foreach loop
    442
  22. Can PHP switch in strings ?
    391
  23. PHP Switch Example
    427
  24. How to send email in PHP ?
    889
  25. How to pass variables via url
    468
  26. How to declare array in PHP like C/C++
    670
  27. PHP date and time formatting
    428
  28. How to redirect browser in PHP ?
    484
  29. How to get current filename as variable ?
    768
  30. email validation, simple
    412
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    27
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    24
  3. How do I create animated background for Google Meet?
    19,730
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    217
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    86
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    153
  7. Array of arguments passed to script in PHP
    85
  8. Invalid Auction Storms in Ad Tech
    93
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    198
  10. The Two Clocks of Ad Server Architecture
    63
  11. Frequency Cap Architecture Overview
    509
  12. Collecting Proof-of-Play from 100,000 Screens
    141
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    280
  14. What is RedisVL?
    105
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    224
  16. TPU & GPU Cluster Management Tools
    234
  17. The Concept of Scaffolding in AI Agents
    222
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    576
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    253
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    328
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    197
  22. Machine Learning: Training vs. Ranking
    213
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    217
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    189
  25. Designing a Data Curation System Around a Vector Database
    215
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    229
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    649
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    185
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    207
  30. Understanding Data Lineage
    252