Jump to content

Recommended Posts

Ok fellas....this one has me stumped. 

 

 

I am trying to copy a whole bunch of pictures from one directory into a new directory.  However, the new directory will store the images based upon the user's id.  Initially I stored all the images in one directory.  HUGE mistake..  That single directory is over 5GB and takes and eternity to open and edit the files for 1 single user.  So I need to separate them out.

 

Anyways, here is what I am trying to do in theory.

 

All the information about the user and the file they uploaded is stored in my database.  But how do I create a script that is somewhat dynamic?  One that will refresh the copy file script for each result.

 

Here is the general idea....or a conversation starter code i am working with so far.

 

<?php

include 'db_connect.php';


$userId=2;

          // Make the directory that will store the users images with the desired folder structure
          $structure = './images/' .$userId. '';

          // create the directory and set permissions
          if (!mkdir($structure, 0777, true)) {
              die('Failed to create folder');
              chmod($structure, 0777);
          }

//Query the database for the images
$query_image = "SELECT *
	FROM images
                WHERE angler=$userId
	ORDER BY submit_id DESC";

$image_result = mysql_query($query_image) or die(mysql_error());

// get count of how many rows in case we need that info
$rowCount = mysql_num_rows($image_result);

//get the results into an array by setting each result as a variable
while($row = mysql_fetch_assoc($image_result))
   $galleryresults[]= $row;


          //now move files
          $file = './submitted_pics/{$galleryresults[0]['image']}';
          $newfile = './images/' .$userId. '/{$galleryresults[0]['image']}';

          if (!copy($file, $newfile)) {
              echo "failed to copy $file...\n";
          }

?>

Any update? Particularly on whether or not you've given it a run and seen what it generates?

 

Only issue I see is, you're using mysql_fetch_assoc and then turning it into an array manually? Why not use mysql_fetch_array?

 

Meh, I've been spoiled on framework queries, so I may be way off here.

          //now move files
          $file = './submitted_pics/'.$galleryresults[0]['image']; //fixed
          $newfile = './images/' .$userId. '/'.$galleryresults[0]['image']}; //fixed
//Use rename to "move" instead of copy
          if (!rename($file, $newfile)) {

Any update? Particularly on whether or not you've given it a run and seen what it generates?

 

Only issue I see is, you're using mysql_fetch_assoc and then turning it into an array manually? Why not use mysql_fetch_array?

 

Meh, I've been spoiled on framework queries, so I may be way off here.

 

Yeah it works but it only copies one file over.  It returns the first result from the database and copies that file.  I need to copy like 13,000 photos....not just one..

 

For me to do that manually it takes about 30 seconds per photo.  30 sec * 13,000 pics = 108 hours of copying photos.....LOL!

You should be able to move them while within the while loop. You can give this a try. You may want to add a 'LIMIT 5' or some low number to the query string until you verify it works properly.

while($row = mysql_fetch_assoc($image_result)) {
//move files
$file = "./submitted_pics/{$row['image']}";
$newfile = "./images/$userId/{$row['image']}";

if( !file_exists($newfile) ) {  // in case script is executed multiple times.
	if (!copy($file, $newfile)) {
		echo "failed to copy $file...\n";
	}
} else {
                 echo "File $file already exists in target directory . . .";
}

It works like a champ!

 

Thanks!

 

I didn't think I could use it in a while loop.... Guess I learn things everyday!

 

<?php

include 'db_connect.php';

$userId=2;

    // Specify the directory that will store the users images with the desired folder structure
    $structure = './images/' .$userId. '';
    
    //First check to see if the directory already exists
    if (file_exists($structure)) { 
        echo "The directory exists"; 

    } else { 
          // create the directory and set permissions
          if (!mkdir($structure, 0777, true)) {
              die('Failed to create folder');
              chmod($structure, 0777);
              echo "The Directory was created";
          }
    }  

//Query the database for the images
$query_image = "SELECT *
	FROM submit
                WHERE user=$userId
	ORDER BY submit_id ASC LIMIT 5";

$image_result = mysql_query($query_image) or die(mysql_error());

// get count of how many rows in case we need that info
$rowCount = mysql_num_rows($image_result);


while($row = mysql_fetch_assoc($image_result)) {

//move files
$file = "./submitted_pics/{$row['image']}";
$newfile = "./images/$userId/{$row['image']}";

if( !file_exists($newfile) ) {  // in case script is executed multiple times.

if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
} else {
                 echo "File $file already exists in target directory . . .";
}
}

echo "Your copy was a success!  $rowCount pictures were copied.";

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.