php – mkdir()在foreach函数中跳过第一个文件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – mkdir()在foreach函数中跳过第一个文件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2318字,纯文字阅读大概需要4分钟。
内容图文
![php – mkdir()在foreach函数中跳过第一个文件](/upload/InfoBanner/zyjiaocheng/819/08bffe7cd72948df9066a2534852a1ba.jpg)
我有一个脚本,它读取URL-Image-List并将图像保存在基于URL的文件夹中.
这是我的脚本:
// 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);
}
每个文件夹应包含一些图像.
但是:当必须创建文件夹时,他会创建此文件夹,但将第一个图像保存在根目录中,而不是保存在此文件夹中.
示例回声
该文件夹已创建,但第一个图像未保存在此新文件夹中.
以下图像也属于此文件夹,可以正确保存.
我没有错.
解决方法:
在第一种情况下,您将其存储为true或false.更改以下内容:
// 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函数中跳过第一个文件全部内容,希望文章能够帮你解决php – mkdir()在foreach函数中跳过第一个文件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。