Jump to content

re-naming uploaded files


Woodburn2006

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

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.