get image files from directory on server for further processing

我的梦境 提交于 2019-12-25 18:23:18

问题


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:

  1. $mug fetches the uploaded file and puts it to its destination place.
  2. You create a new function that implements the processing you wish to use here and in $mug.
  3. 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 to readdir($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 when readdir() is given rubbish, it might return NULL.

  • Incorrect path for fopen()

    Given $folderimage = "images" and $entry = "photo.jpg", then the fopen() line will try to open the image at photo.jpg rather than ../uploads/images/photo.jpg. You likely wanted to use something like $dir . $entry, but read on as you shouldn't be using fopen() 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 from fopen() to it.

  • opendir()/readdir() for directory iteration is ancient

    There 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!