php 如何在将上传的文件保存到目录之前重命名它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18705639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to rename uploaded file before saving it into a directory?
提问by George Edward Portillo
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.
move_uploaded_file()是将上传的文件保存到目录中的那个,我猜测也是move_uploaded_file()是为其设置名称的那个。
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.
我尝试更改诸如$_FILES["file"]["name"] 之类的变量并将其替换为$fileName; 变量,以便可以存储新名称。
回答by Ben Fortune
You can simply change the name of the file by changing the name of the file in the second parameter of move_uploaded_file
.
您可以通过更改 的第二个参数中的文件名来简单地更改文件名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.
更改以反映您的问题,将根据当前时间生成一个随机数,并附加原始上传文件的扩展名。
回答by Prabhagaran
You can Try this,
你可以试试这个
$newfilename= date('dmYHis').str_replace(" ", "", basename($_FILES["file"]["name"]));
move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $newfilename);
回答by tmh
You guess correctly. Read the manual page for move_uploaded_file
. Set the second parameter to whereever your want to save the file.
你猜对了。阅读手册页move_uploaded_file
。将第二个参数设置为您想要保存文件的任何位置。
If it doesn't work, there is something wrong with your $fileName
. Please post your most recent code.
如果它不起作用,则您的$fileName
. 请发布您最近的代码。
回答by antelove
/* 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"];
$destination = "../img/imageDirectory/" . $basename;
/* move the file */
move_uploaded_file( $source, $destination );
echo "Stored in: {$destination}";
回答by Yehuda Schwartz
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
move_uploaded_file
如果文件未成功移动,将返回 false 您可以在代码中添加一些内容以在发生这种情况时在日志中提醒您,这应该可以帮助您找出重命名文件时遇到问题的原因
回答by Vin
$temp = explode(".", $_FILES['docat']['name'][$key]);
//Change file name with order_id and microtime as prefix
$newfilename = $_REQUEST['order_id'].'-'.round(microtime(true)) . '-'.$temp[0].'.' . end($temp);