如何使上传的文件具有随机名称?
How do I make the uploaded file have a random name?
我想知道如何让我上传的图片有一个随机名称。
使用这个,我可以获得一个随机字符串:
<?php
function generateRandomString($length = 10) {
return substr(str_shuffle(str_repeat($x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length/strlen($x)) )),1,$length);
}
echo generateRandomString(); // OR: generateRandomString(24)
我用这个上传:
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" && $imageFileType != "bmp" ) {
echo "Sorry, only JPG, JPEG, PNG, BMP & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo '<br>The file <img src="http://upimg.comxa.com/uploads/'. basename( $_FILES["fileToUpload"]["name"]). '"> has been uploaded.';
echo '<br>Direct URL: http://upimg.comxa.com/uploads/'. basename( $_FILES["fileToUpload"]["name"]);
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
问题是我不知道怎么做。
我从 w3schools 复制代码,所以我不完全知道它是如何工作的。
谢谢
如果您使用函数更改变量 $target_file,因为这是保存文件的位置。
所以第 2 行不是这个:
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
替换为:
$target_file = $target_dir . generateRandomString();
通过数组变量获取每个字符
<?php
function rndStr(){
$characters = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","Z","Y","Z");
//generate 6 characters from it
$charI = rand(0,25);
$charII = rand(0,25);
$charIII = rand(0,25);
$charIV = rand(0,25);
$charV = rand(0,25);
$charVI = rand(0,25);
//output as string
$rnd_name = $characters[$charI].$characters[$charII].$characters[$charIII].$characters[$charIV].$characters[$charV].$characters[$charVI] ;
return $rnd_name;
}
用法
$target_file = $target_dir . rndStr();
我想知道如何让我上传的图片有一个随机名称。 使用这个,我可以获得一个随机字符串:
<?php
function generateRandomString($length = 10) {
return substr(str_shuffle(str_repeat($x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length/strlen($x)) )),1,$length);
}
echo generateRandomString(); // OR: generateRandomString(24)
我用这个上传:
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" && $imageFileType != "bmp" ) {
echo "Sorry, only JPG, JPEG, PNG, BMP & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo '<br>The file <img src="http://upimg.comxa.com/uploads/'. basename( $_FILES["fileToUpload"]["name"]). '"> has been uploaded.';
echo '<br>Direct URL: http://upimg.comxa.com/uploads/'. basename( $_FILES["fileToUpload"]["name"]);
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
问题是我不知道怎么做。 我从 w3schools 复制代码,所以我不完全知道它是如何工作的。 谢谢
如果您使用函数更改变量 $target_file,因为这是保存文件的位置。
所以第 2 行不是这个:
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
替换为:
$target_file = $target_dir . generateRandomString();
通过数组变量获取每个字符
<?php
function rndStr(){
$characters = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","Z","Y","Z");
//generate 6 characters from it
$charI = rand(0,25);
$charII = rand(0,25);
$charIII = rand(0,25);
$charIV = rand(0,25);
$charV = rand(0,25);
$charVI = rand(0,25);
//output as string
$rnd_name = $characters[$charI].$characters[$charII].$characters[$charIII].$characters[$charIV].$characters[$charV].$characters[$charVI] ;
return $rnd_name;
}
用法
$target_file = $target_dir . rndStr();