0

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.

sam-w
  • 7,478
  • 1
  • 47
  • 77
Jjames
  • 183
  • 1
  • 3
  • 14

3 Answers3

0

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.
Levente Pánczél
  • 1,895
  • 2
  • 14
  • 16
0

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));
}
?>
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
0

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");
    }
    
Community
  • 1
  • 1
salathe
  • 51,324
  • 12
  • 104
  • 132