Solved

Move .jpg files Between Folder According to A List

Posted on 2012-04-13
23
324 Views
Last Modified: 2012-07-16
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.
0
Comment
Question by:dplsr
  • 12
  • 8
23 Comments
 
LVL 8

Expert Comment

by:X-treem
ID: 37842723
yes, read list (txt or mysql), check if exists in folder,  (fileexists), copy to new folder, (optionally delete)
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37842755
Please post the contents of the skulist.txt file.  No MySQL is needed, I expect.
0
 

Author Comment

by:dplsr
ID: 37842971
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37843026
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
 

Author Comment

by:dplsr
ID: 37843064
They are on the server. I just need to move the ones on the list to a new folder.
0
 

Author Comment

by:dplsr
ID: 37843159
Please note: the list I attached is a sample. the complete list has 2000+- names. There are 4000+- files in the picts folder.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37843619
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
 

Author Comment

by:dplsr
ID: 37843847
the path is  /picts/xxxxx.jpg. They are listed in a column on the skulist.txt
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37843925
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
 

Author Comment

by:dplsr
ID: 37843956
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
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37844142
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
 

Author Comment

by:dplsr
ID: 37844195
Sorry this is all new to me. Do I run this from a web page?
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37844227
Yes.  It is a PHP script.  I installed it on my server and ran it to test.
0
 

Author Comment

by:dplsr
ID: 37844327
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37844493
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
 

Author Comment

by:dplsr
ID: 37844644
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
 

Author Comment

by:dplsr
ID: 37846185
I tried it with the list. Same error NOT READABLE: RAY_temp_dpslr_input/
0
 

Author Comment

by:dplsr
ID: 37846193
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37846294
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
 

Author Comment

by:dplsr
ID: 37846303
It is not on Windows. It is on a Linix/Apache
0
 

Author Comment

by:dplsr
ID: 37846363
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question