How to rename uploaded file before saving it into a directory?

两盒软妹~` 提交于 2019-11-26 00:06:18

问题


Below is the code I used in order to upload files into a directory. It works fine. My main question is:

move_uploaded_file() is the one that saves the uploaded file into the directory, and it is also my guess that move_uploaded_file() is the one that sets the name for it.

How could I change the name of my file to a random number?

I have tried to do so below:

$allowedExts = array(\"gif\", \"jpeg\", \"jpg\", \"png\");
$temp = explode(\".\", $_FILES[\"file\"][\"name\"]);
$extension = end($temp);
if ((($_FILES[\"file\"][\"type\"] == \"image/gif\") || ($_FILES[\"file\"][\"type\"] == \"image/jpeg\") || ($_FILES[\"file\"][\"type\"] == \"image/jpg\") || ($_FILES[\"file\"][\"type\"] == \"image/pjpeg\") || ($_FILES[\"file\"][\"type\"] == \"image/x-png\") || ($_FILES[\"file\"][\"type\"] == \"image/png\")) && ($_FILES[\"file\"][\"size\"] < 100000) && in_array($extension, $allowedExts)) {
    if ($_FILES[\"file\"][\"error\"] > 0) {
        echo \"Return Code: \" . $_FILES[\"file\"][\"error\"] . \"<br>\";
    } else {

        $fileName = $temp[0] . \".\" . $temp[1];
        $temp[0] = rand(0, 3000); //Set to random number
        $fileName;

        if (file_exists(\"../img/imageDirectory/\" . $_FILES[\"file\"][\"name\"])) {
            echo $_FILES[\"file\"][\"name\"] . \" already exists. \";
        } else {
            move_uploaded_file($_FILES[\"file\"][\"tmp_name\"], \"../img/imageDirectory/\" . $_FILES[\"file\"][\"name\"]);
            echo \"Stored in: \" . \"../img/imageDirectory/\" . $_FILES[\"file\"][\"name\"];
        }
    }
} else {
    echo \"Invalid file\";
}

I tried changing variables such as the $_FILES[\"file\"][\"name\"] and replacing it with the $fileName; variable so that the new name can be stored.


回答1:


You can simply change the name of the file by changing the name of the file in the second parameter of move_uploaded_file.

Instead of

move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $_FILES["file"]["name"]);

Use

$temp = explode(".", $_FILES["file"]["name"]);
$newfilename = round(microtime(true)) . '.' . end($temp);
move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $newfilename);

Changed to reflect your question, will product a random number based on the current time and append the extension from the originally uploaded file.




回答2:


You can Try this,

$newfilename= date('dmYHis').str_replace(" ", "", basename($_FILES["file"]["name"]));

move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $newfilename);



回答3:


You guess correctly. Read the manual page for move_uploaded_file. Set the second parameter to whereever your want to save the file.

If it doesn't work, there is something wrong with your $fileName. Please post your most recent code.




回答4:


The move_uploaded_file will return false if the file was not successfully moved you can put something into your code to alert you in a log if that happens, that should help you figure out why your having trouble renaming the file




回答5:


/* create new name file */
$filename   = uniqid() . "_" . time(); // 5dab1961e93a7_1571494241
$extension  = pathinfo( $_FILES["file"]["name"], PATHINFO_EXTENSION ); // jpg
$basename   = $filename . '.' . $extension; // 5dab1961e93a7_1571494241.jpg

$source       = $_FILES["file"]["tmp_name"];
$destionation = "../img/imageDirectory/" . $basename;

/* move the file */
move_uploaded_file( $source, $destination );
echo "Stored in: {$destination}";


来源:https://stackoverflow.com/questions/18705639/how-to-rename-uploaded-file-before-saving-it-into-a-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!