嘿伙计们,我想知道如何在将文件上传到服务器文件夹之前专门重命名文件,以避免上传时文件名相同,并用它重写同名文件。我倾向于使用 school_id 作为图像的文件名。这是php代码
include_once('DBconnect.php');
session_start();
$school_id = $_SESSION['school_id'];
$name = $_FILES['file_path'] ['name'];
$size = $_FILES['file_path'] ['size'];
$type = $_FILES['file_path'] ['type'];
$extension = substr($name, strpos($name, '.')+1);
$max_size =3145728;
$temp_file = $_FILES['file_path'] ['tmp_name'];
if(isset($name)){
if(!empty($name)){
if(($extension == 'jpg' || $extension == 'jpeg') && ($type == 'image/jpeg' || $type == 'image/jpg') && $size <= $max_size ){
if($school_id == 0){
$location = 'uploads_images/super_admin/';
move_uploaded_file($temp_file,$location.$name);
$insert_path = "INSERT INTO image_upload (school_id,path_name)
VALUES('$school_id','$name')";
$result2 = mysql_query($insert_path);
if(@!$result2){
die('error header'.mysql_error());
}
echo "Success in uploading";
}
else{
$location = 'uploads_images/schools/';
move_uploaded_file($temp_file,$location.$name);
$insert_path = "INSERT INTO image_upload (school_id,path_name)
VALUES('$school_id','$name')";
$result = mysql_query($insert_path);
if(@!$result){
die('error header'.mysql_error());
}
echo "Success in uploading";
}
}
else{
echo "File type is too big or Incorrect file type";
}
}
else{
echo "Please Choose A JPEG File";
}
}
?>