Jump to content

Archived

This topic is now archived and is closed to further replies.

Woodburn2006

re-naming uploaded files

Recommended Posts

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:
[list]
[*]artistname1
[*]artistname2
[*]artistname3
[*]artistname4
[*]etc etc
[/list]

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

[code]<?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");
  }
}
?>[/code]

Share this post


Link to post
Share on other sites

Hopefully this works, the idea is there anyway ;)

[code]
<?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++;
}
?>

[/code]

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

[code]<?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++;
}
?>[/code]

Share this post


Link to post
Share on other sites

×

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.