简体   繁体   中英

Rename file while move_uploaded_file

Anyway, I got this code:

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

Which just uploads my file, but I also got $newname, which gives it a new name, problem is, how do I actually rename it to $newname before/while it gets uploaded?

You would rename it while you're moving it, so change:

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

to:

move_uploaded_file($_FILES["file"]["tmp_name"], "uploaded/" . $newname);

The second argument to move_uploaded_file is the destination path, so simply use the $newname variable as a part of this.

move_uploaded_file($_FILES["file"]["tmp_name"], "uploaded/" . $newname);

Incidentally, you should also use is_uploaded_file to ensure that the source file is really an upload. (Likewise, you should ensure that $newfile is a safe filename for the operating system in use.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM