Jump to content

Recommended Posts

Hey guys , I'm quite new to PHP and have been working on a piece of code for a while now and just can't seem to find a solution. I'm trying to make a script which copy's images from a folder to another folder on the web server. The folder names on the web server will be the serial number in the image name. So basically i want to get the serial number from the image name, create folder with 'serial number' as the name if folder exists don't make a new folder. And then copy the image to that folder? I can find loads on filtering arrays but nothing on working with the array elements. Is there a way to read the element and get INT that's length is 7? Any help or tips would be greatly appreciated  :happy-04:

 

The middle part of the filename "5165266"  is the serial number which changes a lot. Each serial number represents a camera unit that automatically takes pictures. 

 

Output:

 

Array ( [0] => Thumbs.db [1] => YDOC_CAMERA DEMO_5165266_170317_120008.004.jpg [2] => YDOC_CAMERA DEMO_5165266_170317_130006.005.jpg [3] => YDOC_CAMERA DEMO_5165266_170317_140002.006.jpg )

 

 

Reading Directory image names into array:

$dir = "/Documents/PICTURECOLLECTOR/";
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        $images = array();

        while (($file = readdir($dh)) !== false) {
            if (!is_dir($dir.$file)) {
                $images[] = $file;
            }
        }
        closedir($dh);
    }
}

Copying images code:

$srcPath = '/Documents/PICTURECOLLECTOR/';
$destPath = '/website/pictures/';  

$srcDir = opendir($srcPath);
while($readFile = readdir($srcDir))
{
   if($readFile != '.' && $readFile != '..')
   {   
       if (!file_exists($destPath . $readFile)) 
       {
         if(copy($srcPath . $readFile, $destPath . $readFile))
            {
               echo "Copy file";
        }
           else
         {
            echo "Canot Copy file";
           }
       }
    }
}
closedir($srcDir); 

P.S Sorry if the title is misleading forgot to change it... 

Edited by hokletrain

Can you provide an example of how everything should look? I mean, something like original filename before copy, the name of new folder and the name of new file. I understand what you want to do, but I have troubles understanding what final result you expect.

You're overthinking it a bit. Not tested and there may be a few typos. 

$src = '/Documents/PICTURECOLLECTOR/'; 
$dest = '/website/pictures/'; 

$files = array_diff(scan_dir($src), array('Thumbs.db')); 
foreach($files as $file) { 
  if(is_file($file)) { 
    $file_exp=explode("_",$file); 
    $serial=$file_exp[2];
    if(!file_exists($dest.$serial."/".$file)) {
      if(is_dir($dest.$serial)) {
        if(copy($src.$file,$dest.$serial."/".$file)) {
          "Copied file ".$src.$file." successfully"; 
        } else {
          "Unable to copy file ".$src.$file." to ".$dest.$serial."/".$file; 
        }
      }
    }
  }
}
Edited by taquitosensei

 

You're overthinking it a bit. Not tested and there may be a few typos. 

$src = '/Documents/PICTURECOLLECTOR/'; 
$dest = '/website/pictures/'; 

$files = array_diff(scan_dir($src), array('Thumbs.db')); 
foreach($files as $file) { 
  if(is_file($file)) { 
    $file_exp=explode("_",$file); 
    $serial=$file_exp[2];
    if(!file_exists($dest.$serial."/".$file)) {
      if(is_dir($dest.$serial)) {
        if(copy($src.$file,$dest.$serial."/".$file)) {
          "Copied file ".$src.$file." successfully"; 
        } else {
          "Unable to copy file ".$src.$file." to ".$dest.$serial."/".$file; 
        }
      }
    }
  }
}

 

I think i am too, i tried this piece I'm getting an error :

 

 

 

 

Fatal error: Uncaught Error: Call to undefined function scan_dir()

The only issue with using an array location is that the the name changes so there can be multiple underlined  parts before it reaches the serial number e.g Test_camera_DEMO_782300. I have been working on a different way which essentially works with one file at time operated by a CRON job ? In the front end when  a sensor unit is made a folder is also created  with the serial number.  I'm getting the latest image chucking it into an array, then trying to match the serial number contained in the image file name with the folder name? If matched copy image to folder?

 

My Current Code Which doesn't work properly: 

<?php 

$imagesDir = '/Pictures/irrigation/';
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
array_multisort(array_map('filemtime', $images), SORT_NUMERIC, SORT_DESC, $images);
$latestimage = $images[0]; 

print($latestimage);

$path = 'images'; // '.' for current
foreach (new DirectoryIterator($path) as $file) {
    if ($file->isDot()) continue;

    if ($file->isDir()) {
        $folder = $file->getFilename();
    }
}


$pos = strpos($folder, $latestimage);

$result = substr($folder, $pos);

print($result);


$file = basename($latestimage);
$dest = "images/$result/$file";


if (copy($latestimage, $dest)){
    
    echo '<strong>copied!!</strong>';
} else {
    echo "<strong>didn't copy <strong>";
}

  
?>

Edited by hokletrain

get rid of the underscore in scan_dir I warned you their might be some typos lol.  To figure out the serial 

$file_exp=explode("_",$file); 
foreach($file_exp as $f) { 
  if(strlen($f)==7 && strpos($f, ".") === false) $serial=$f; 
}

I'd look for the first numeric 7-digit element. (There could potentially be a 7-char text element)

foreach ($filea_exp as $f) {
    if (ctype_digit($f) && strlen($f)==7 ) {
        $serial = $f;
        break;
    }
}
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.