Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Move .jpg files Between Folder According to A List

Posted on 2012-04-13
23
Medium Priority
?
344 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 111

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 111

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 111

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 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

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 111

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 111

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

719 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