Rename Uploaded File in PHP
What I want to do is add a number to the beginning of the file name so I don't have a duplicate file name in the folder.
So I choose my file "example.pdf" and upload, I got 开发者_如何转开发the the part of the code that looks like this:
move_uploaded_file($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
In this line of code above can I add a variable to change the file name to "1-example.pdf"?
Is that possible?
You can specify the new name as part of the second parameter of move_uploaded_file
:
move_uploaded_file($HTTP_POST_FILES['ufile']['tmp_name'][0], "$path1/example.pdf");
Hope this helps!
The move_uploaded_file
method's second parameter is not only the path, but the filename to place the file. Try something like this:
move_uploaded_file($_FILES['ufile']['tmp_name'][0], $path1.'/[PUT NUMBER HERE]-'.$_FILES['ufile']['name'][0]);
What I did is that I created a random number which goes until to a million so the program will generate a number to rename a picture like this .though I did not use exactly your codes in your question .
#Rename images
$rand = rand(0,1000000);
$rename = $rand.$name;
$move = move_uploaded_file($temp_name,$target.$rename);
精彩评论