开发者

Rename Uploaded File in PHP

开发者 https://www.devze.com 2023-01-21 15:27 出处:网络
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.

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号