从服务器上的目录中获取图像文件以进行进一步处 [英] get image files from directory on server for further processing
问题描述
我在服务器上有包含图像文件的文件夹。我正在尝试获取这些文件,然后进一步上传,但我认为我使用了错误的功能。
I have folders on the server that contain image files. I'm trying to get those files and then upload them further, but I think I'm using the wrong function.
我的代码:
$dir = "../uploads/".$folderimage."/";
if ($handle = opendir($dir)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "$entry\n";
$handle = fopen($entry,"wb");
$mug->images_upload(array( "AlbumID" => "#####", "File" => $handle));
}
}
closedir($handle);
}
不知道我在做什么,我需要做的就是传递文件到类函数 $ mug-> images-> upload
。它适用于 $ _ POST
请求,但我需要将已经上传的文件移动到该文件夹。
Not sure what I am doing, all I need to do is pass the file to the class function $mug->images->upload
. It works from a $_POST
request but I need to move the files already uploaded to the folder.
推荐答案
您发布的代码存在许多明显的问题。
There are a number of apparent issues with the code that you have posted.
$dir = "../uploads/".$folderimage."/";
if ($handle = opendir($dir)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "$entry\n";
$handle = fopen($entry,"wb");
$mug->images_upload(array( "AlbumID" => "#####", "File" => $handle));
}
}
closedir($handle);
}
-
冲突变量。
您已经使用
$ handle
vairable来存储目录句柄,但以后才会覆盖它使用循环内的文件资源。一旦你在循环中覆盖它,下一次调用readdir($ handle)
就没有任何意义,你是在文件资源上调用该函数。这很容易导致无限循环,因为当readdir()
被给予垃圾时,它可能返回NULL
。You have used the
$handle
vairable to store the directory handle, only to later overwrite it with a file resource inside the loop. As soon as you overwrite it inside the loop, the next call toreaddir($handle)
does not make any sense are you are calling the function on a file resource. This could very easily lead to an infinite loop since whenreaddir()
is given rubbish, it might returnNULL
.的路径不正确fopen()
Incorrect path for
fopen()
鉴于
$ folderimage =images
和$ entry =photo.jpg
,然后fopen()
行将尝试在photo.jpg
而不是打开图像> ../上传/图像/ photo.jpg
。你可能想要使用像$ dir这样的东西。 $ entry
,但请继续阅读,因为你根本不应该使用fopen()
。Given
$folderimage = "images"
and$entry = "photo.jpg"
, then thefopen()
line will try to open the image atphoto.jpg
rather than../uploads/images/photo.jpg
. You likely wanted to use something like$dir . $entry
, but read on as you shouldn't be usingfopen()
at all.phpSmug库的使用不正确
文件
参数必须是字符串含的路径,正在上载的本地文件(来源)。您尝试将文件资源从fopen()
传递给它。The
File
argument must be a string containing "the path to the local file that is being uploaded" (source). You instead try to pass a file resource fromfopen()
to it.opendir()/ readdir()
目录迭代是古老的opendir()/readdir()
for directory iteration is ancient有更好的方法来遍历PHP中的目录内容。如劳伦斯的答案中所述,
glob()
可能有用。There are better ways to traverse directory contents in PHP. As mentioned in Lawrence's answer,
glob()
might be useful.我还提倡使用 filesystem iterator SPL 。
$dir = "../uploads/$folderimage/"; foreach (new FilesystemIterator($dir) as $fileinfo) { $image_pathname = $fileinfo->getPathname(); $mug->images_upload("AlbumID=#####", "File=$image_pathname"); }
这篇关于从服务器上的目录中获取图像文件以进行进一步处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!