Hongmu Notes
Home Language Notes The header jumps to a file link. If the file link is 404, then modify the file suffix and try again.
Language Notes PHP

The header jumps to a file link. If the file link is 404, then modify the file suffix and try again.

The header jumps to a file link. If the file link is 404, then modify the file suffix and try again.

The following is the compiled and optimized code:

 隐藏内容:登录后可查看
$file_url = $_GET['src'];
$file_extensions = array('png', 'jpeg', 'jpg');

foreach ($file_extensions as $extension) {
  $r = pathinfo($file_url, PATHINFO_EXTENSION);
  $new_file_url = str_replace($r, $extension, $file_url);

  if (@get_headers($new_file_url)[0] === 'HTTP/1.1 200 OK') {
    header("Location: $new_file_url");
    exit;
  }
}

header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found", true, 404);
echo "File not found.";

The optimized code has been enhanced with the following improvements:

  1. The unnecessary `false!=` check has been removed; you can now directly use the result returned by `@get_headers()`.
  2. Move the file access code inside the loop; this way, once a valid file is found, you can simply jump to the access code.
  3. Move the "File not found." output statement outside the loop; this prevents multiple "File not found." messages from being printed, making the code more concise.
微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP ob function record

PHP ob function record Language Notes PHP

Usage of the following three functions ob_get_contents(); ob_end_clean(); ob_start(); You can use these functions to buffer local files and execute local script code. Use ob_start() to save the output code into the buffer, and the page will not be displayed; then use ob_get_contents to get the data in the buffer. o…
👁 141
PHP preg

PHP preg Language Notes PHP

The preg_match_all function is used to perform a global regular expression match. preg_match_all() Syntax int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG…
👁 169
Detailed explanation of PHP ternary operator and if

Detailed explanation of PHP ternary operator and if Language Notes PHP

Ternary operator condition ? Result 1 : Result 2 Explanation: The position in front of the question mark is the condition for judgment. If the condition is met, the result is 1, and if it is not met, the result is 2. This article compares and explains the ternary operator and if...else... in detail. I hope it will be helpful to everyone. Today when I was revising my paper online, I encountered a statement that I couldn’t understand: $if_summary = $row['IF_SUMMARY']=…
👁 189
PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232

Recommended reading

(Adaptive Mobile Version) HTML5 Responsive Law Firm Website – PBootCMS Template; Law Firm Website Source Code Download – 0423

(Adaptive Mobile Version) HTML5 Responsive Law Firm Website – PBootCMS Template; Law Firm Website Source Code Download – 0423 Practical Collection pbootcms Template

This is an adaptive, mobile-friendly HTML5 responsive PbootCMS website template designed for law firms and attorneys to showcase their legal services and successful case studies. It helps legal service providers build a professional online presence and attract potential clients. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn | Related Articles...
👁 46
(Adaptive mobile version) Group company website template Generous corporate website source code download 1162

(Adaptive mobile version) Group company website template Generous corporate website source code download 1162 Practical Collection pbootcms Template

A PbootCMS website template for group companies and atmospheric enterprises, supporting PC and WAP. The design style is high-end and the functional modules are comprehensive, which is suitable for large enterprise groups to display their brand strength and business segments. Template display Installation instructions Website backend: /admin.php Account: admin Password: admin Unzip password: www.4s5.cn Related articles PbootCMS complete guide to secure operation and maintenance...
👁 52
Change article line breaks in EMLog to <p> tags.

Change article line breaks in EMLog to <p> tags. Program Notes emlog

The new version of EMLOG is still unfamiliar to me – the editor still has a ton of issues! However, since I'm not very familiar with JavaScript, I've simply modified the line breaks using PHP! Find your article page template (e.g., `echo_log.php`) and modify the variable used for outputting your articles: `&lt;? $log_content = str_replace(&quot;&lt;br/&gt;", &quot;&lt;/p&amp;gt;...`
👁 191
Resource Tutorial Download Website Template 0757

Resource Tutorial Download Website Template 0757 Practical Collection Yiyou template

This EyouCMS template is ideal for resource tutorial download websites, featuring a design style tailored for resource sharing – enabling you to display tutorial resources, download services, category navigation, and user reviews. It helps resource platforms attract users online and provide seamless download services. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (EyouC...
👁 48
(Adaptive Mobile Version) LED Lighting – pbootCMS Website Template; Download LED Green Lighting Website Source Code – 0424

(Adaptive Mobile Version) LED Lighting – pbootCMS Website Template; Download LED Green Lighting Website Source Code – 0424 Practical Collection pbootcms Template

An adaptive mobile-friendly LED lighting and green lighting PbootCMS website template. Featuring a modern, energy-efficient design, this template is ideal for LED lighting manufacturers and lighting engineering companies to showcase their products. It helps lighting enterprises present their brands online and attract both engineering and international trade clients. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 53
(Adaptive mobile version) Green photovoltaic energy website template download 1163

(Adaptive mobile version) Green photovoltaic energy website template download 1163 Practical Collection pbootcms Template

A green photovoltaic energy PbootCMS website template that supports PC and WAP. The design style is fresh and energy-saving, suitable for displaying photovoltaic products, power station cases and green energy solutions. It helps new energy companies display their brands online and attract investment and cooperation. Template display Installation instructions Website backend: /admin.php Account: admin Password: admin Unzip password: www.4s5.cn Related articles...
👁 76