Jump to content

Rename on upload


JayLewis

Recommended Posts

This is the function i use to upload images to my server in a certain directory...

how would i change the file name on upload?

 

thanks.

 

<? 
$dir=opendir("image/upload"); 

$i=0; 
while($imgfile=readdir($dir)) 
{ 
     if ($imgfile != "." && $imgfile!="..") 
         { 
        $imgarray[$i]=$imgfile; 
        $i++; 
        } 
} 

closedir($dir); 

$rand=rand(0,count($imgarray)-1); 

    if($rand >= 0) 
    { 
    echo '<img src="image/upload/'.$imgarray[$rand].'" width="125" height="125">'; 
//replace path, width & height sizes to fit your needs 
    } 
?>

Link to comment
Share on other sites

My Bad :( i got the wrong bit of code.. sorry :)

 

<?php 
$target = "image/upload/"; 
$target = $target . basename( $_FILES['uploaded']['name']) ; 
$ok=1; 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} 
else {
echo "Sorry, there was a problem uploading your file.";
}

?>

Link to comment
Share on other sites

<?php 
$target = "image/upload/"; 
$target = $target . "newnameappend-". basename( $_FILES['uploaded']['name']) ; // This is where you designate a new name.
$ok=1; 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} 
else {
echo "Sorry, there was a problem uploading your file.";
}

?>

Link to comment
Share on other sites

ahh there ya go :)

 

So here when you're using move_uploaded_file() that's where you're actually renaming the temporary file to (in your case) the filename in $target.  You do not by any means have to use the name from $_FILES['uploaded']['name'].

Link to comment
Share on other sites

<?php 
$target = "image/upload/"; 
$target = $target . "newnameappend-001.jpg" ; // This is where you designate a new name. That and .jpg should match the uploaded extension.
$ok=1; 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} 
else {
echo "Sorry, there was a problem uploading your file.";
}

?>

Link to comment
Share on other sites

You would need some way to increment to the next one, either a text file that stores the last value and you read it than increment it one than write it again or database or something.

<?php
$incrementVal = $oldVal++; // add one to old val note old val is not set in this script, you need to decide that.
$target = "image/upload/";
$target = $target . "newnameappend-".$incrementVal.".jpg" ; // This is where you designate a new name. That and .jpg should match the uploaded extension.
$ok=1;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else {
echo "Sorry, there was a problem uploading your file.";
}

?>

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.