Changing the prefix of tmp_name

When I upload a file, the tmp_name is “php[random letters]”. Is there a way to change the “php” prefix to something else?

Thanks!

Yes, you can use the substr() function to return the string without the leading three characters:

$newStr =  'ruby'.substr('php is great', 3);
// gives: ruby is great

Thanks. I know how to replace it after it’s given the tmp_name, but I’m more interested in a way to change the tmp_name that’s initially assigned.

I don’t believe you can change the generated temporary name (from the PHP side of things). In the C code for generating the temporary file name, ‘php’ is a hardcoded prefix:

fd = php_open_temporary_fd_ex(PG(upload_tmp_dir), "php", &temp_filename, 1 TSRMLS_CC);

(Source code here.)

The definition of the php_open_temporary_fd_ex function shows the second argument (‘php’) is the prefix:

PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, char **opened_path_p, zend_bool open_basedir_check TSRMLS_DC)

Thus if you’d like to change it, you’ll need to modify the C source code for PHP and build PHP from source. It’s not a difficult thing to do, just a little tedious (and for what seems like very little gain)…

2 Likes

Or… just move it with move_uploaded_file, like you would do normally?

I use a PHP script called easy PHP upload to do my uploading functions I can change name or do what I want using the below listed code after including the upload class to my file.

<?php

/**
 * @author SiNUX
 * @copyright 2013
 */
include_once("upload_class.php");

//Image Upload

if($_FILES['stdImage']['name'])
    {

        $max_size = 1000*1000; // the max. size for uploading
        $my_upload = new file_upload;
        $my_upload->upload_dir = "image/"; // "files" is the folder for the uploaded files (you have to create this folder)
        $my_upload->extensions = array(".png",".jpeg",".jpg"); // specify the allowed extensions here
        // $my_upload->extensions = "de"; // use this to switch the messages into an other language (translate first!!!)
        $my_upload->max_length_filename = 1000; // change this value to fit your field length in your database (standard 100)
        $my_upload->rename_file = true;
        $my_upload->the_temp_file = $_FILES['stdImage']['tmp_name'];
        $my_upload->the_file = $_FILES['stdImage']['name'];
        $my_upload->http_error = $_FILES['stdImage']['error'];
            //$my_upload->replace = (isset($_POST['replace'])) ? $_POST['replace'] : "n"; // because only a checked checkboxes is true
            //$my_upload->do_filename_check = (isset($_POST['check'])) ? $_POST['check'] : "n"; // use this boolean to check for a valid filename
            //$new_name = (isset($_POST['name'])) ? $_POST['name'] : "";
        if ($my_upload->upload()) { // new name is an additional filename information, use this to rename the uploaded file
            $full_path = $my_upload->upload_dir.$my_upload->file_copy;
            $imagename = $my_upload->get_uploaded_file_info($full_path);
        }
    
    }
    else
    {
        $imagename = "" ;
    }

I agree that’s a bit of an effort just to change the default. Thanks for looking that up, though! Good to know at least.