Solved

Move .jpg files Between Folder According to A List

Posted on 2012-04-13
23
315 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 108

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
 
LVL 108

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 108

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 108

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 108

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 108

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 108

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 108

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now