You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP从文件夹获取图片:本地主机图片列表代码技术问询

Hey there! Let's refine that PHP script to reliably pull images from your folder and return them in a usable format. Based on the snippet you shared, here's a complete, tested version that fixes gaps and adds useful features:

PHP Script to Fetch & Return Images from a Folder
<?php
// Allow cross-origin requests (restrict the origin to your domain in production!)
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
// Tell clients we're sending JSON data
header('Content-Type: application/json');

// Target folder storing your images (relative to this script)
$target_dir = "uploadToper/";
// Base URL to build full image links
$base_url = "http://" . $_SERVER['SERVER_NAME'] . "/apimerit/";

$image_collection = array();

// First, check if the target folder exists to avoid errors
if (is_dir($target_dir)) {
    // Open the directory for reading
    if ($directory_handle = opendir($target_dir)) {
        // Loop through every item in the folder
        while (($file = readdir($directory_handle)) !== false) {
            // Skip the default "." and ".." system entries
            if ($file !== "." && $file !== "..") {
                // Get the file extension to filter only images
                $file_extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
                // List of allowed image formats (add/remove as needed)
                $allowed_formats = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
                
                // Only add valid image files to our collection
                if (in_array($file_extension, $allowed_formats)) {
                    $full_image_url = $base_url . $target_dir . $file;
                    $image_collection[] = array(
                        'title' => pathinfo($file, PATHINFO_FILENAME), // Use filename (no extension) as title
                        'image_url' => $full_image_url,
                        'filename' => $file
                    );
                }
            }
        }
        // Close the directory handle when done
        closedir($directory_handle);
    }
}

// Output the collection as JSON
echo json_encode($image_collection);
?>

Key Fixes & Explanations

  • JSON Content Type Header: Added Content-Type: application/json so frontend apps or APIs calling this script can properly parse the response.
  • Error Prevention: We first check if the uploadToper/ folder exists to avoid fatal errors if the directory is missing.
  • Image Filtering: Only files with common image extensions are included—you can tweak the $allowed_formats array to add formats like svg or remove ones you don't need.
  • Clean Data: We skip the system-generated . and .. entries that clutter directory listings, and include useful fields like the full image URL and filename.

Quick Usage Notes

  1. Ensure the uploadToper/ folder is in the same directory as this PHP script (adjust $target_dir if it's stored elsewhere, e.g., ../uploads/).
  2. If your site uses HTTPS, update the $base_url to start with https:// instead of http:// (you can also hardcode it for reliability, e.g., $base_url = "https://yourdomain.com/apimerit/";).
  3. When you send a GET request to this script, it will return a JSON array of all valid images in the target folder.

内容的提问来源于stack exchange,提问作者Sagar Sasuke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:13:51