PHP将图像名称更改为产品名称

时间:2017-12-13 00:09:15

标签: php

我不知道如何在上传到产品标题时更改图像。你可以帮我做吗?我尝试通过更改files_name但它没有工作它只显示名称没有“.png”。所以如果你知道只是告诉我在哪里以及如何改变请帮助我。

您好,我不知道如何在上传到产品标题时更改图片。你可以帮我做吗?我尝试通过更改files_name但它没有工作它只显示名称没有“.png”。所以如果你知道只是告诉我在哪里以及如何改变请帮助我。

<?php 
session_start();
$db = mysqli_connect('localhost', 'root', '', 'kojnes');

$prd_cat = "";
$prd_title = "";
$prd_price = "";
$prd_desc = "";
$prd_img = "";
$update = false;

if (isset($_POST['save'])) {
    $prd_cat = $_POST['prd_cat'];
    $prd_title = $_POST['prd_title'];
    $prd_price = $_POST['prd_price'];
    $prd_desc = $_POST['prd_desc'];
    $prd_img = $_POST['prd_img'];

     if(isset($_FILES['prd_img'])){
  $errors= array();
  $file_name = $_FILES['prd_img']['name'];
  $file_size =$_FILES['prd_img']['size'];
  $file_tmp =$_FILES['prd_img']['tmp_name'];
  $file_type=$_FILES['prd_img']['type'];
  $file_ext=strtolower(end(explode('.',$_FILES['prd_img']['name'])));

  $expensions= array("jpeg","jpg","png");

  if(in_array($file_ext,$expensions)=== false){
     $errors[]="extension not allowed, please choose a JPEG or PNG file.";
  }

  if($file_size > 2097152){
     $errors[]='File size must be excately 2 MB';
  }

  if(empty($errors)==true){
     move_uploaded_file($file_tmp,"C:/xampp/htdocs/images/".$file_name);
     echo "Success";
  }else{
     print_r($errors);
  }
 }
    /////////////////////////////////
    mysqli_query($db, "INSERT INTO products (prd_cat, prd_title, prd_price, 
  prd_desc, prd_img) VALUES ('$prd_cat', '$prd_title', '$prd_price', 
   '$prd_desc', '$file_name')"); 
    $_SESSION['message'] = "Adresas išsaugotas"; 
    header('location: index.php');
}
if (isset($_POST['update'])) {
    $prd_id = $_POST['prd_id'];
    $prd_cat = $_POST['prd_cat'];
    $prd_title = $_POST['prd_title'];
    $prd_price = $_POST['prd_price'];
    $prd_desc = $_POST['prd_desc'];
    $prd_img = $_POST['prd_img'];

if(isset($_FILES['prd_img'])){
    $errors= array();
    $file_name = $_FILES['prd_img']['name'];
    $file_size =$_FILES['prd_img']['size'];
    $file_tmp =$_FILES['prd_img']['tmp_name'];//įkeltas failas laikiname 
žiniatinklio serveryje.
    $file_type=$_FILES['prd_img']['type'];//rodo images.png (priklauso nuo 
failo tipo)
    $file_ext=strtolower(end(explode('.',$_FILES['prd_img']['name'])));

  $expensions= array("jpeg","jpg","png");

  if(in_array($file_ext,$expensions)=== false){
     $errors[]="extension not allowed, please choose a JPEG or PNG file.";
  }

  if($file_size > 2097152){
     $errors[]='File size must be excately 2 MB';
  }

  if(empty($errors)==true){
     move_uploaded_file($file_tmp,"C:/xampp/htdocs/images/".$file_name);
     echo "Success";
  }else{
     print_r($errors);
  }
 }
    mysqli_query($db, "UPDATE products SET prd_cat='$prd_cat', prd_title='$prd_title',prd_price='$prd_price',prd_desc='$prd_desc',prd_img='$file_name'  WHERE prd_id=$prd_id");
    $_SESSION['message'] = "Adresas pakeistas!"; 
    header('location: index.php');
}

if (isset($_GET['del'])) {
$prd_id = $_GET['del'];
mysqli_query($db, "DELETE FROM products WHERE prd_id=$prd_id");
$_SESSION['message'] = "Adresas ištrintas!"; 
header('location: index.php');
}

 $results = mysqli_query($db, "SELECT * FROM products");


if  (isset($_GET['del'])) {
$id = $_GET['del'];
mysqli_query($db, "DELETE FROM user WHERE id=$id");
$_SESSION['message'] = "Adresas ištrintas!"; 
header('location:index.php');
}
$results = mysqli_query($db, "SELECT * FROM user");
?>

1 个答案:

答案 0 :(得分:0)

如果要获取文件扩展名,可能需要使用pathinfo():

$path = $_FILES['image']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);