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
https://forums.phpfreaks.com/topic/43040-rename-on-upload/
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
https://forums.phpfreaks.com/topic/43040-rename-on-upload/#findComment-209059
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
https://forums.phpfreaks.com/topic/43040-rename-on-upload/#findComment-209062
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
https://forums.phpfreaks.com/topic/43040-rename-on-upload/#findComment-209084
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
https://forums.phpfreaks.com/topic/43040-rename-on-upload/#findComment-209089
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.