Jump to content


Photo

re-naming uploaded files


  • Please log in to reply
3 replies to this topic

#1 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 29 August 2006 - 03:39 PM

i am doing an upload page for a picture gallery. the user will be uploading more than one image. what i want to do is, is for every picture that is uploaded i want to rename with the artists name and have a sequental number following it. for example:
  • artistname1
  • artistname2
  • artistname3
  • artistname4
  • etc etc

how would i do this when i use the following code to upload:

<?php
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, "images/$name");
   }
}
?>


#2 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 29 August 2006 - 03:46 PM


Hopefully this works, the idea is there anyway ;)

<?php
$i = 1;   // Counter

foreach ($_FILES["pictures"]["error"] as $key => $error) {
   list($artistname, $extension) = explode(".", $_FILES['name'][$key]);   // seperate name from extension

   if ($error == UPLOAD_ERR_OK) {
       $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
       $name = $_FILES["pictures"]["name"][$key];
       move_uploaded_file($tmp_name, "images/{$artistname}{$i}.{$extension}");
   }
   $i++;
}
?>


Though this wouldnt take into account of the artist has uploaded in past.  That would involve seeing how many files they already have uploaded and starting $i at that point and then continuing the increment.

#3 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 29 August 2006 - 03:51 PM

cool thanks, what would i use to get the list of files that were previously use and then get the highest number?

im assuming that i would fetch the highest labelled number then split the artist name from the number and extension so that i would have a figure to set as $i?

#4 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 29 August 2006 - 04:03 PM

i used this code like was said to use, but the outputted image has no extension, it renames the files fine but there is no extension on the image.

<?php
$comp = $_POST["comp"];
$col = $_POST["col"];
$i = 1;   // Counter

foreach ($_FILES["pictures"]["error"] as $key => $error) {
   list($artistname, $extension) = explode(".", $_FILES['name'][$key]);   // seperate name from extension

   if ($error == UPLOAD_ERR_OK) {
       $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
       $name = $_FILES["pictures"]["name"][$key];
       move_uploaded_file($tmp_name, "tests/{$comp}{$i}.{$extension}");
	   echo "done<br>";
   }
   $i++;
}
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users