![](/img/trans.png)
[英]How do I execute this mysql query with PHP and then randomize the results?
[英]How do I randomize the name of an uploaded image
if( isset($_POST["Headline"]) && isset( $_POST["Summary"]) && isset( $_POST["Description"])&& isset( $_POST["submit"]) ) {
$Headline = $_POST['Headline'];
$Summary = $_POST['Summary'];
$Description= $_POST['Description'];
$image_file = $_FILES["txt_file"]["name"];
$type = $_FILES["txt_file"]["type"]; //file name "txt_file"
$size = $_FILES["txt_file"]["size"];
$temp = $_FILES["txt_file"]["tmp_name"];
$path="Images/".$image_file; //set upload folder path
if(empty($Headline )) {
echo '<script language="javascript">';
echo 'alert("Please Enter a Headline")';
echo '</script>';
}
else if(empty($Summary)) {
echo '<script language="javascript">';
echo 'alert("Please Enter a Summary")';
echo '</script>';
}
else if(empty($Description)) {
echo '<script language="javascript">';
echo 'alert("Please Enter a Description")';
echo '</script>';
}
else if(empty($image_file)) {
echo '<script language="javascript">';
echo 'alert("Please upload an image")';
echo '</script>';
}
else if ($type=="image/jpg" || $type=='image/jpeg' || $type=='image/png' || $type=='image/gif') {
if(!file_exists($path)) //check file not exist in your upload folder path
{
move_uploaded_file($temp, "Images/" .$image_file);
in here is where the image gets sent to the folder but how do i make it so it is randomized
$query = $con-> prepare("
INSERT INTO News ( Headline, Summary,Description, Image,Date)
VALUES ( :Headline, :Summary,:Description, '".$_FILES['txt_file']['name']."' , NOW() )
");
$success = $query-> execute ([
'Headline' => $Headline,
'Description' => $Description,
'Summary' => $Summary,
]);
}
我希望圖像的名稱每次都能理想地不同,因此當用戶上傳具有相同名稱的圖像時,不會覆蓋已經存在的圖像
GUID生成器呢? 對於MS SQL,將select cast(newId() as nvarchar(128))
在PHP上,您可以使用此功能。
• mt_rand
<?php
function GUID()
{
if (function_exists('com_create_guid') === true)
{
return trim(com_create_guid(), '{}');
}
return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
}
echo 'Image name: ' . GUID();
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.