-2

我想在我的服务器上检查我的图像,我有另一个子域来保存图像,但它们是通过 HTTPS 提供的

它只能在本地工作,但不能通过 HTTPS 远程工作,因为它总是打印“存在目录”。

$foto = "https://subdomain.example.com/images/" . $var . "/" . $var2. "/flayer";

if (file_exists($foto . ".jpg")) {
    echo "HELLO WORL";
}

if (glob($foto . ".*")) {
    if (file_exists($foto . ".jpg")) {
        $ruta = "https://subdomain.example.com/images/". $var . "/" . $var2 . "/flayer.jpg";
    }else{ 
        echo "no exists";
    }
}else{
    echo "no exists on directory";
}
4

2 回答 2

0

glob()是一个目录服务。如果它是您域的子目录,那么您的文件系统应该存储在同一台服务器上。在您的索引文件夹中,定义或存储一个变量到此路径 - 将其视为环境变量。

define('PUBLIC_ROOT', dirname(__FILE__));

您现在可以在您的子域中循环浏览您的目录。它将是一个名为您的子域的文件夹。

foreach(glob(PUBLIC_ROOT . '/subdomain/images/*.jpg', GLOB_BRACE) as $image)
    // Do something with each JPEG image

如果由于某种原因,您的子域托管在其他地方 - 奇怪 - 或者它是一个外部站点,您需要使用 HTTP。发送cURL请求。您可以在标题中指定任何人。

$ch = curl_init('https://sub.example.com/images/foo.jpg');

curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec  ($ch);

if(curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200)
    // It didn't exist

curl_close($ch);

您不能跨域(通过 HTTP 协议)使用目录服务(DS 协议)。

继续前进:如果您需要该服务器上保存的所有图像的集合,则考虑构建一个 API,该 API 返回子域上所有文件路径或动态 URI 的 JSON 编码数组,然后从另一个域请求它.

于 2019-01-08T22:18:22.000 回答
0

我用 2 个选项进行测试

   function remote_file_exists($url, $ext) {
    $ch = curl_init($url . $ext);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if ($httpCode == 200) {
        return true;
    }
}

    function remote($url, $ext) {
    if (@exif_imagetype($url . $ext)) {
        return true;
    } else {
        return false;
    }
}

工作,但太慢了,有什么办法做得更快?

于 2019-01-09T04:32:58.027 回答