if(isset($_FILES['attachment'])){
$errors= array();
$file_name = $_FILES['attachment']['name'];
$file_size =$_FILES['attachment']['size'];
$file_tmp =$_FILES['attachment']['tmp_name'];
$file_type=$_FILES['attachment']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['attachment']['name'])));
$extensions= array("jpeg","jpg","png");
if(in_array($file_ext,$extensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}
}
$sql = $wpdb->prepare( "INSERT INTO ".$tablename." (post_id, name, email, price, author, post_title, purchase_type, closing_date, concessions_amount, insp_from, insp_to, seller_charge, emd, home_warranty, seller_occupancy, attachment ) VALUES ( %d, %s, %s, %d, %s, %s, %s, %s, %d, %s, %s, %d, %d, %s, %s, %s )", $id, $name, $email, $price, $author, $post_title, $purchase_type, $closing_date, $concessions_amount, $insp_from, $insp_to, $seller_charge, $emd, $home_warranty, $seller_occupancy, $file_name );
if(empty($errors)==true){
function tp_wordpress_uploads_directory_path() {
$upload_dir = wp_upload_dir();
return $upload_dir['basedir'] . "/offers/";
}
$upload_check = move_uploaded_file($file_tmp,tp_wordpress_uploads_directory_path().$file_name);
}
現在系統正在替換現有的文件,是否有人可以幫助添加一個隨機數,以便文件名不匹配,可以通過將當前時間和日期添加到?我知道如何使用蘭德;可以工作,但無法在代碼中實現它。
PHP有一個built-in函數,專門用于生成隨機文件名:
https://www.php.net/manual/en/function.tempnam.php
如果您確實想在文件名中附加一個隨機數,則可以使用:
但是,不能保證它不會生成相同的數字兩次或多次,從而導致文件再次被覆蓋。