在PHP

时间:2017-12-07 10:37:30

标签: php

当我将图像名称存储在表格中时,要像URL一样存储名称。

例如,我已经准备好了像我要在图片名称之前添加的http://192.168.137.1/NI/uploads/网址。

我想要这个结果:http://192.168.137.1/NI/uploads/imagename.png

PHP代码:

//We will upload files to this folder
//So one thing don't forget, also create a folder named uploads inside your 
project folder i.e. MyApi folder
 define('UPLOAD_PATH', 'uploads/');

 //connecting to database 
 $conn = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME) or die('Unable to 
 connect');

  //An array to display the response
  $response = array();

   //if the call is an api call 
   if(isset($_GET['apicall'])){

    //switching the api call 
    switch($_GET['apicall']){

    //if it is an upload call we will upload the image
    case 'uploadpic':

    //first confirming that we have the image and tags in the request 
    parameter
   if(isset($_FILES['pic']['name']) && isset($_POST['tags'])){

  //uploading file and storing it to database as well 
 try{
 move_uploaded_file($_FILES['pic']['tmp_name'], UPLOAD_PATH . $_FILES['pic']
['name']);


 $stmt = $conn->prepare("INSERT INTO t_table (image,tags)
VALUES (?,?)");

 $stmt->bind_param("ss", $_FILES['pic']['name'],$_POST['tags']);
 if($stmt->execute()){
 $response['error'] = false;
 $response['message'] = 'File uploaded successfully';
 }else{
 throw new Exception("Could not upload file");
 }
 }catch(Exception $e){
 $response['error'] = true;
 $response['message'] = 'Could not upload file';
}

 }else{
 $response['error'] = true;
 $response['message'] = "Required params not available";
 }

2 个答案:

答案 0 :(得分:1)

试试这个

公共功能网址($ patheFile,$ imageName)     {         return sprintf(             “%S://%s%S”,             isset($ _ SERVER ['HTTPS'])&& $ _SERVER ['HTTPS']!='关'? 'https':'http',             $ _ SERVER [ 'SERVER_NAME'],             $ _ SERVER [ 'REQUEST_URI']         );     } echo url('uplode',$ _FILES ['pic'] ['name'])

答案 1 :(得分:1)

不要存储URL,只需在UI上显示项目的base url时附加。一旦您在另一个IP上部署应用程序或将IP更改为域名,这将无法正常工作。

存储名称并在迭代前端数据时保持附加基本URL。