gpt4 book ai didi

php - mkdir() 跳过 foreach 函数中的第一个文件

转载 作者:可可西里 更新时间:2023-10-31 22:42:05 27 4
gpt4 key购买 nike

我有一个脚本可以读取 URL-Image-List 并将图像保存在基于 URL 的文件夹中。

This is how it should look like

这是我的脚本:

// Open the URL and save each line in a array
$file = fopen("http://www.edem.de/php/imglist.ashx","r");
$fileArray = array();
while (($line = fgetss($file)) !== false) {
$fileArray[] = $line;
}

// Sort the array
array_multisort(array_values($fileArray), SORT_NUMERIC , array_keys($fileArray), SORT_NUMERIC , $fileArray);

// Start the iteration for saving each image
foreach ($fileArray as $url) {

// Explode URL for folder and image name
$urlpath = parse_url($url, PHP_URL_PATH);
$dataend = explode("/", $urlpath);
$ending = $dataend[5];
$folder = $dataend[3];

// delete "_" and "whitespace"
$folder = strtr($folder, "_", " ");
$folder = preg_replace('/\s+/', '', $folder);
$ending = strtr($ending, "_", " ");
$ending = preg_replace('/\s+/', '', $ending);

// Check if folder exist. If yes -> go on with path. If no -> create folder
if (!file_exists("Bilder/$folder/")) {
$imgpath = mkdir("Bilder/$folder/", 0777, true);
//I THINK THE MISTAKE IS HERE
}
else {
$imgpath = ('Bilder/' .$folder. '/');
}

// save the path and the image name in a variable
$savepath = $imgpath . $ending;

// Test echo
echo ("The image <strong>$ending</strong> is saved in the folder <strong>$savepath</strong>. The file should be in the folder <strong>$folder</strong><br><br>");

//Connect and save images
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec ($ch);
curl_close ($ch);

$fp = fopen(''.$savepath.'','w');
fwrite($fp, $rawdata);
fclose($fp);
}

每个文件夹应该包含一些图像。但是:当必须创建文件夹时,他创建了这个文件夹,但将第一个图像保存在根目录中而不是这个文件夹中。

示例回显 Here is the created echo of this script文件夹已创建,但第一张图像未保存在此新文件夹中。以下图像也属于此文件夹,已正确保存。

我没有收到任何错误。

最佳答案

在第一种情况下,您将其存储为 truefalse。更改以下内容:

// Check if folder exist. If yes -> go on with path. If no -> create folder
if (!file_exists("Bilder/$folder/")) {
$imgpath = mkdir("Bilder/$folder/", 0777, true);
//I THINK THE MISTAKE IS HERE
}
else {
$imgpath = ('Bilder/' .$folder. '/');
}

收件人:

// Check if folder exist. If yes -> go on with path. If no -> create folder
if (!file_exists("Bilder/$folder/")) {
mkdir("Bilder/$folder/", 0777, true);
}
$imgpath = ('Bilder/' .$folder. '/');

关于php - mkdir() 跳过 foreach 函数中的第一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31207023/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com