How can I removed the name of the file I'm uploading and leave the extention of the file alone? For example, if I'm uploading a file called "picture1.jpg" and the username in my cookie is "Person1" how can I make it upload the file as "Person1-[random number here].jpg" instead of "Person1-[random number here]-picture1.jpg"?

Here's the code I'm using:

<?php
require('rand.php'); //file used to generate a random number

$site_name = $_SERVER['HTTP_HOST'];
$url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
$url_this =  "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];

$upload_dir = "upload_files/";
$upload_url = $url_dir."/upload_files/";
$message ="";

if (!is_dir("upload_files")) {
	die ("upload_files directory doesn't exist");
}

if ($_FILES['userfile']) {
	$message = do_upload($upload_dir, $upload_url);
}
else {
	$message = "Invalid File Specified.";
}

print $message;

function do_upload($upload_dir, $upload_url) {

$temp_name = $_FILES['userfile']['tmp_name'];
$file_name = $_COOKIE['Username']."-".str_rand(7, '123456789')."-".$_FILES['userfile']['name'];
$file_type = $_FILES['userfile']['type']; 
$file_size = $_FILES['userfile']['size']; 
$result    = $_FILES['userfile']['error'];
$file_url  = $upload_url.$file_name;
$file_path = $upload_dir.$file_name;

//File Name Check
if ( $file_name =="") { 
	$message = "Invalid File Name Specified";
	return $message;
}
//File Size Check
else if ( $file_size > 500000) {
    $message = "The file size is over 500K.";
    return $message;
}
//File Type Check
else if ( $file_type == "text/plain" ) {
    $message = "Sorry, You cannot upload any script file" ;
    return $message;
}
echo "uploaded";
    $result  =  move_uploaded_file($temp_name, $file_path);
    $message = ($result)?"File url <a href=$file_url>$file_url</a>" :
    	      "Somthing is wrong with uploading a file.";

return $message;

}
?>

<form name="upload" id="upload" ENCTYPE="multipart/form-data" method="post">
  Upload Image<input type="file" id="userfile" name="userfile">
  <input type="submit" name="upload" value="Upload">
</form> 

Thanks in advance.

    $dot_pos = strrpos($temp_name, '.');
    $first_part = substr($temp_name, 0, $dot_pos) . '-' . $rnd_num;
    $last_part = substr($temp_name, $dot_pos);
    $name = $first_part . $last_part;

      I'm confused. Where do I add that code or what do I replace with it?

        Sorry, I got the "$first_part" line wrong. It should have been:

        $first_part = $_COOKIE['Username'] . '-' . $rnd_num; 
        

        So in your code you would replace this line:

        $file_name = $_COOKIE['Username']."-".str_rand(7, '123456789')."-".$_FILES['userfile']['name'];

        with this:

        $dot_pos = strrpos($_FILES['userfile']['name'], '.'); 
        $first_part = $_COOKIE['Username'] . '-' . str_rand(7, '123456789');  
        $last_part = substr($temp_name, $dot_pos); $file_name = $first_part . $last_part;

          That didn't work. When I replace that piece of code, it wouldn't upload the file at all. Thanks anyway.

            When that code is executed, the file is already uploaded. All you're doing is renaming it before moving it to permanent storage.

              Write a Reply...