Move .jpg files Between Folder According to A List

I have a folder (picts) of .jpgs.  I want to move some of them to a new folder called "current"   according to a list,  skulist.txt of the picture names. Is this possible with php? I can put the list into a MySql table if needed.
dplsrAsked:
Who is Participating?
 
Ray PaseurCommented:
Have a look at these links... This method will overwrite any matching file names in the output directory.  If you want to prevent that, back up your files or make a check for file_exists() on the output file before calling copy().

Script: http://www.laprbass.com/RAY_temp_dpslr.php
Input: http://www.laprbass.com/RAY_temp_dpslr_input/
Output: http://www.laprbass.com/RAY_temp_dpslr_outgo/

Word of warning... Servers cache file information.  If you are testing this script and it looks like the results are goofy, get a non-browser based program like an FTP client to go look at the true contents of the directories.

<?php // RAY_temp_dpslr.php
error_reporting(E_ALL);

// See http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_27674409.html

// SET THE INPUT AND OUTPUT DIRECTORIES
$in = 'RAY_temp_dpslr_input';
$ot = 'RAY_temp_dpslr_outgo';

// SIMULATE READING THE LIST OF FILE NAMES WITH file_get_contents('skulist.txt');
$skulist = <<<SKULIST
41350-708.jpg
41560-782.jpg
4179-962.jpg
44122-820.jpg
44123-820.jpg
44124-820.jpg
44237-782.jpg
44238-02.jpg
44238-962.jpg
SKULIST;

// MAKE AN ARRAY FROM THE LIST
$arr = explode(PHP_EOL, $skulist);

// ACTIVATE THIS TO SEE THE ARRAY
// var_dump($arr);

// ITERATE OVER THE ARRAY
foreach ($arr as $f)
{
    // REMOVE UNWANTED EOL CHARACTERS
    $f = trim($f);

    // REMOVE CACHED FILE INFORMATION
    clearstatcache();

    // CREATE FILE PATHS
    $i = $in . DIRECTORY_SEPARATOR . $f;
    $o = $ot . DIRECTORY_SEPARATOR . $f;

    // CAN WE READ THE FILE?
    if (is_file($i))
    {
        if ($x = copy($i, $o))
        {
            echo PHP_EOL . "<br/>COPIED $i TO $o";
        }
        else
        {
            echo PHP_EOL . "<br/>DID NOT COPY $i TO $o";
        }

        // IF COPY SUCCESSFUL - OPTIONALLY REMOVE THE OLD VERSION
        // if ($x) unlink($i);
    }
    else
    {
        echo PHP_EOL . "<br/>NOT READABLE: $i";
    }
}

Open in new window

0
 
X-treemCommented:
yes, read list (txt or mysql), check if exists in folder,  (fileexists), copy to new folder, (optionally delete)
0
 
Ray PaseurCommented:
Please post the contents of the skulist.txt file.  No MySQL is needed, I expect.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
dplsrAuthor Commented:
It is just a list of file names.
I found this code that will move files . I don't know how to work the list into the code.
<?php
$uploads_dir = '/uploads';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        $name = $_FILES["pictures"]["name"][$key];
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
    }
}
?>
0
 
Ray PaseurCommented:
The code you found uses the $_FILES superglobal array.  This is used in file uploads.  Are you uploading the files, or are they already on your server?
0
 
dplsrAuthor Commented:
They are on the server. I just need to move the ones on the list to a new folder.
0
 
dplsrAuthor Commented:
Please note: the list I attached is a sample. the complete list has 2000+- names. There are 4000+- files in the picts folder.
0
 
Ray PaseurCommented:
the list I attached is a sample
Eh?  I just need to see a few lines that can show me the file paths for the images and how the names are carried in the skulist.txt file, thanks.
0
 
dplsrAuthor Commented:
the path is  /picts/xxxxx.jpg. They are listed in a column on the skulist.txt
0
 
Ray PaseurCommented:
They are listed in a column on the skulist.txt
Please work with me here.  I would like you to post an example of a few lines from the skulist.txt file.  It is not worth my time to try to guess what the file looks like when you have it in front of you, so please copy a few lines from the file and post those lines here.  Thank you.
0
 
dplsrAuthor Commented:
I attached the file a while ago, but maybe it did not make the trip. Here are a few.
41350-708.jpg
41560-782.jpg
4179-962.jpg
44122-820.jpg
44123-820.jpg
44124-820.jpg
44237-782.jpg
44238-02.jpg
44238-962.jpg
0
 
dplsrAuthor Commented:
Sorry this is all new to me. Do I run this from a web page?
0
 
Ray PaseurCommented:
Yes.  It is a PHP script.  I installed it on my server and ran it to test.
0
 
dplsrAuthor Commented:
OK I rename the file with the pictures:RAY_temp_dpslr_outgo
and the file where I am moving them to: RAY_temp_dpslr_input

Note I am using diferent file names, I changed them in the code.

I open the page and get:
NOT READABLE: RAY_temp_dpslr_input/11003.jpg
NOT READABLE: RAY_temp_dpslr_input/11003QC.jpg
NOT READABLE: RAY_temp_dpslr_input/11007.jpg
NOT READABLE: RAY_temp_dpslr_input/11007A.jpg
NOT READABLE: RAY_temp_dpslr_input/11011G2152132L.jpg
NOT READABLE: RAY_temp_dpslr_input/11017.jpg
NOT READABLE: RAY_temp_dpslr_input/11018.jpg
NOT READABLE: RAY_temp_dpslr_input/11019.jpg
NOT READABLE: RAY_temp_dpslr_input/11020AT-15.jpg
NOT READABLE: RAY_temp_dpslr_input/11023AT-15.jpg
NOT READABLE: RAY_temp_dpslr_input/11028.jpg
0
 
Ray PaseurCommented:
The directory named "...input" is the directory that you want to move the files from
The directory named "...outgo" is the directory that you want to move the files to
0
 
dplsrAuthor Commented:
Ihave the file to be moved in the file RAY_temp_dpslr_input, still get error

NOT READABLE: RAY_temp_dpslr_input/11003.jpg
NOT READABLE: RAY_temp_dpslr_input/11003QC.jpg
NOT READABLE: RAY_temp_dpslr_input/11007.jpg
NOT READABLE: RAY_temp_dpslr_input/11007A.jpg
NOT READABLE: RAY_temp_dpslr_input/11011G2152132L.jpg
NOT READABLE: RAY_temp_dpslr_input/11017.jpg
NOT READABLE: RAY_temp_dpslr_input/11018.jpg
NOT READABLE: RAY_temp_dpslr_input/11019.jpg
NOT READABLE: RAY_temp_dpslr_input/11020AT-15.jpg
NOT READABLE: RAY_temp_dpslr_input/11023AT-15.jpg
NOT READABLE: RAY_temp_dpslr_input/11028.jpg
0
 
dplsrAuthor Commented:
I tried it with the list. Same error NOT READABLE: RAY_temp_dpslr_input/
0
 
dplsrAuthor Commented:
The problem seems to be it iis not reading the list or file at all.      // CAN WE READ THE FILE? is not printing anything.
0
 
Ray PaseurCommented:
It may be a file permissions problem.  Sorry, but debugging Windows file permissions is out of my reach from the question and answer dialog at EE.  I was able to test the script on my server and it worked correctly.

You might want to post a question about how to diagnose file permission issues in Windows.  I just don't know how those things work.
0
 
dplsrAuthor Commented:
It is not on Windows. It is on a Linix/Apache
0
 
dplsrAuthor Commented:
Making progress! no getting:

DID NOT COPY RAY_temp_dpslr_input/11003.jpg TO RAY_temp_dpslr_outgo/11003.jpg
DID NOT COPY RAY_temp_dpslr_input/11003QC.jpg TO RAY_temp_dpslr_outgo/11003QC.jpg
DID NOT COPY RAY_temp_dpslr_input/11007.jpg TO RAY_temp_dpslr_outgo/11007.jpg
DID NOT COPY RAY_temp_dpslr_input/11007A.jpg TO RAY_temp_dpslr_outgo/11007A.jpg
DID NOT COPY RAY_temp_dpslr_input/11011G2152132L.jpg TO RAY_temp_dpslr_outgo/11011G2152132L.jpg
DID NOT COPY RAY_temp_dpslr_input/11017.jpg TO RAY_temp_dpslr_outgo/11017.jpg
DID NOT COPY RAY_temp_dpslr_input/11018.jpg TO RAY_temp_dpslr_outgo/11018.jpg
DID NOT COPY RAY_temp_dpslr_input/11019.jpg TO RAY_temp_dpslr_outgo/11019.jpg
DID NOT COPY RAY_temp_dpslr_input/11020AT-15.jpg TO RAY_temp_dpslr_outgo/11020AT-15.jpg
DID NOT COPY RAY_temp_dpslr_input/11023AT-15.jpg TO RAY_temp_dpslr_outgo/11023AT-15.jpg
DID NOT COPY RAY_temp_dpslr_input/11028.jpg TO RAY_temp_dpslr_outgo/11028.jpg
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.