hokletrain Posted October 12, 2017 Share Posted October 12, 2017 (edited) 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 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 October 12, 2017 by hokletrain Quote Link to comment https://forums.phpfreaks.com/topic/305343-php-matching-array-elements-and-copying-files/ Share on other sites More sharing options...
phpmillion Posted October 12, 2017 Share Posted October 12, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/305343-php-matching-array-elements-and-copying-files/#findComment-1552661 Share on other sites More sharing options...
taquitosensei Posted October 13, 2017 Share Posted October 13, 2017 (edited) 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 October 13, 2017 by taquitosensei Quote Link to comment https://forums.phpfreaks.com/topic/305343-php-matching-array-elements-and-copying-files/#findComment-1552678 Share on other sites More sharing options...
hokletrain Posted October 17, 2017 Author Share Posted October 17, 2017 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() Quote Link to comment https://forums.phpfreaks.com/topic/305343-php-matching-array-elements-and-copying-files/#findComment-1552754 Share on other sites More sharing options...
hokletrain Posted October 17, 2017 Author Share Posted October 17, 2017 (edited) 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 October 17, 2017 by hokletrain Quote Link to comment https://forums.phpfreaks.com/topic/305343-php-matching-array-elements-and-copying-files/#findComment-1552755 Share on other sites More sharing options...
taquitosensei Posted October 17, 2017 Share Posted October 17, 2017 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; } Quote Link to comment https://forums.phpfreaks.com/topic/305343-php-matching-array-elements-and-copying-files/#findComment-1552766 Share on other sites More sharing options...
Barand Posted October 17, 2017 Share Posted October 17, 2017 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/305343-php-matching-array-elements-and-copying-files/#findComment-1552767 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.