Question

I am having my iPhone app delete a selected image off of my database, after it deletes the image i want, it needs to rename all of the other images left in the folder. So say my folder has 3 images: img1, img2, img3 and so on. Say i delete img1, i go ahead and unlink() it and now i need to rename img2 to img1 and then img3 to img2.

Here is my code for right now

<?php
$dbc = mysql_connect('hostname', 'login', 'password');
if(!$dbc){
die('not connected : ' . mysql_error());
}

$db_selected = mysql_select_db("database", $dbc);
if(!$db_selected){
die("could not connect to DB : " . mysql_error());  
}

$username = $_GET['username'];
$image = $_GET['imagename'];

$check = mysql_query("SELECT image FROM members WHERE username='$username'");
$findMem = mysql_num_rows($check);

if($findMem > 0){

while($row = mysql_fetch_array($check) or die(mysql_error())){
$images = $row["image"];
$images = $images - 1;
unlink("./$image");

mysql_query("UPDATE members SET image='$images'
WHERE username='$username'");
}

$path = "$username/default/";


$files = glob("{$path}/{*.jpg,*.jpeg,*.png}", GLOB_BRACE);


function filetime_callback($a, $b)
{
  if (filemtime($a) === filemtime($b)) return 0;
  return filemtime($a) > filemtime($b) ? -1 : 1; 
} 

// Then sort with usort()
usort($files, "filetime_callback");

}

?>

As you see i have a function set up that will order all of the files by the date created.

I still want the dates created to stay the same, i just need to rename the files inside the folder. There could be 50 images, so its going to have to be some sort of loop.

Thanks for the help :)

Was it helpful?

Solution

See http://php.net/manual/en/function.rename.php

You should probably look at relying on some other mechanism for ordering your files though. I'd suggest that you add an order by field in your database and sort based on that. That way you can have your files sorted independent of the name that you give them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top