Jump to content

Re-name image?


Mr Chris

Recommended Posts

Hi Guys,

Following on from the kind  help I was given yesterday I’m trying to put together a script where it uploads a file and saves that file details into a MYSQL Database.

Now it works, but

1) When I added a function to re-name the image if the image name already exists it doesn't re-name it.
2) If I upload two images with the same name I get the error [b] Cannot redeclare get_unique_name()[/b]

Can anyone please help?

[code=php:0]
<form action="script.php" method="post" enctype="multipart/form-data" name="form1">
  <table width="247" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <th width="247">
<input name="file[]" type="file" id="file[]"></th></tr>
    <tr>
      <td><input name="file[]" type="file" id="file[]"></td></tr>
    <tr>
      <td><input name="file[]" type="file" id="file[]"></td></tr>
    <tr>
      <td><div align="center">
        <input type="submit" name="Submit" value="Submit">
      </div></td></tr>
  </table></form>
[/code]

[code=php:0]
<?php

include('**************.ini');
$can_i_connect = db_connect(); // by db_connect function is in my include file
  if(!$can_i_connect)
    {
      echo "Could not connect to database";
    }

foreach ($_FILES['file']['name'] as $k => $filename) {

if ($filename != '')  {

  $tmpfile = $_FILES['file']['tmp_name'][$k];

//Start check name to see if it's a duplicate
function get_unique_name($filename) {
    $parts = explode('.', $filename);
    $ext = array_pop($parts);
    $name = implode('.', $parts);
    $num = 0;
    do {
        $num++;
        $new_name = $name . '-' . $num . '.' . $ext;
    } while(file_exists($new_name));

    return $new_name;
}
//End check name to see if it's a duplicate


//Move code starts here
//if the temp file is there copy it to the server
  if (@is_uploaded_file($tmpfile)) {
    copy($tmpfile, "return_images/" .$new_name);
    echo "<br>success";
    }else {
    echo "<br>Not Uploaded<br>";
    }
//Move code ends here


  }else{
  echo "error";
  }
 

}

foreach ($_FILES['file']['name'] as $value3) {

if(!empty($value3)){
$pathname = "$value3";
mysql_select_db($mysql);
$sql = "INSERT INTO `images` (`imagepath`) VALUES ('$pathname');";

mysql_query($sql) or die('Error, insert query failed');
}
}
echo $pathname;
echo $thumbpath;
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/30334-re-name-image/
Share on other sites

[code]
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}

$ext = findexts ($_FILES['uploaded']['name']) ;

$ran = rand () ;

$ran2 = $ran.".";

$target = "/your/direct/path/$username/";

$target = $target . $ran2.$ext;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
[/code]
Link to comment
https://forums.phpfreaks.com/topic/30334-re-name-image/#findComment-139625
Share on other sites

Thanks Guys,

But i'd really like to use the function I initially used as I want to see why it's not working:

I've moved the re-name out of the for loop and called it later on in the script, but now it's sayng:

[b]Warning: Missing argument 1 for get_unique_name() [/b]

On this line [b]function get_unique_name($new_name) { [/b]

and still not re-naming the image if it's a duplicate in MYSQL.

Any further help much appreciated.

Thanks

Chris

[code=php:0]
<?php

include('***********.ini');
$can_i_connect = db_connect(); // by db_connect function is in my include file
   if(!$can_i_connect)
     {
      echo "Could not connect to database";
     }

foreach ($_FILES['file']['name'] as $k => $filename) {

if ($filename != '')  {

  $tmpfile = $_FILES['file']['tmp_name'][$k];


//Move code starts here
//if the temp file is there copy it to the server
  if (@is_uploaded_file($tmpfile)) {
    copy($tmpfile, "return_images/" .$new_name);
    echo "<br>success";
    }else {
    echo "<br>Not Uploaded<br>";
    }
//Move code ends here


  }else{
  echo "error";
  }
   

}

//Start check name to see if it's a duplicate
function get_unique_name($new_name) {
    $parts = explode('.', $new_name);
    $ext = array_pop($parts);
    $name = implode('.', $parts);
    $num = 0;
    do {
        $num++;
        $new_name = $name . '-' . $num . '.' . $ext;
    } while(file_exists($new_name));

    return $new_name;
}
//End check name to see if it's a duplicate

foreach ($_FILES['file']['name'] as $new_name) {

$x = get_unique_name();
if(!empty($x)){
$pathname = "$new_name";
mysql_select_db($mysql);
$sql = "INSERT INTO `images` (`imagepath`) VALUES ('$pathname');";

mysql_query($sql) or die('Error, insert query failed');
}
}
echo $pathname;
echo $thumbpath;
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/30334-re-name-image/#findComment-139640
Share on other sites

This line [code=php:0]$x = get_unique_name();[/code] should be passing a variable (I'm assuming the old name) to the function, so it should look something like this...

[code=php:0]$x = get_unique_name($_FILES['file']['name']);[/code]

It needs an argument.

Regards
Huggie
Link to comment
https://forums.phpfreaks.com/topic/30334-re-name-image/#findComment-140004
Share on other sites

Archived

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

×
×
  • 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.