问题
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.
My code:
$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);
}
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.
回答1:
It's a bit tricky to emulate a file upload POST request for your $mug object. You would be better off if you could refactor the code in $mug as follows:
- $mug fetches the uploaded file and puts it to its destination place.
- You create a new function that implements the processing you wish to use here and in $mug.
- Call this function from here, and from $mug with the appropriate filename.
回答2:
If $mug->images_upload
is expecting the file path then pass $dir.$entry
to it
<?php
$dir = "../uploads/".$folderimage."/";
if ($handle = opendir($dir)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo $entry.PHP_EOL;
$mug->images_upload(array( "AlbumID"=>"#####", "File"=>$dir.$entry));
}
}
closedir($handle);
}
//Or simpler way but slightly slower
foreach (glob($dir."*.{png,jpg,gif}", GLOB_BRACE) as $file) {
echo $file.PHP_EOL;
$mug->images_upload(array( "AlbumID"=>"#####", "File" =>$dir.$file));
}
?>
回答3:
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);
}
Clashing variables.
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
.Incorrect path for
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.Incorrect usage of the phpSmug library
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()
for directory iteration is ancientThere are better ways to traverse directory contents in PHP. As mentioned in Lawrence's answer,
glob()
might be useful.I would also advocate using the filesystem iterator from the SPL.
$dir = "../uploads/$folderimage/"; foreach (new FilesystemIterator($dir) as $fileinfo) { $image_pathname = $fileinfo->getPathname(); $mug->images_upload("AlbumID=#####", "File=$image_pathname"); }
来源:https://stackoverflow.com/questions/13434909/get-image-files-from-directory-on-server-for-further-processing