Hello, I have a working script allowing me to upload a file to my server, and it changes the filename to a random number upon upload. My script is:

//This function separates the extension from the rest of the file name and returns it 
 function findexts ($filename) 
 { 
 $filename = strtolower($filename) ; 
 $exts = split("[/\\.]", $filename) ; 
 $n = count($exts)-1; 
 $exts = $exts[$n]; 
 return $exts; 
 } 
 //This applies the function to our file  
$ext = findexts ($_FILES['Photo']['name']) ; //This line assigns a random number to a variable. You could also use a timestamp here if you prefer. $ran = rand () ; //This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended. $ran2 = $ran."."; //This assigns the subdirectory you want to save into $target_path = "../uploads/photos/"; //This combines the directory, the random file name, and the extension $target_path = $target_path . $ran2.$ext; if(!move_uploaded_file($_FILES['Photo']['tmp_name'], $target_path)) { die("There was an error uploading the Photo, please try again!"); } $insertSQL = sprintf("INSERT INTO photos (Caption, DatePosted, Gallery_id, Photo) VALUES (%s, %s, %s, '".$ran2.$ext."')", GetSQLValueString($_POST['Caption'], "text"), GetSQLValueString($_POST['DatePosted'], "date"), GetSQLValueString($_POST['gid'], "int"), GetSQLValueString($_FILES['Photo']['name'], "text"));

My problem is when I upload an image, for example:
Image.jpg becomes 10120202.jpg

But I want it to be Image1012020.jpg

How can I alter this code to place the filename infront of my random numbers?

Thanks

    Thank You!

    I took your advice and tweaked it a bit more and worked perfect:

    $ran2 = substr($_FILES['Photo']['name'], 0, -4).$ran."."; 

    I was uploading an image for example Image.jpg and it was making it Image.jpg102339953.jpg so I used substr to remove the image extension.

      Write a Reply...