2017-12-07 6 views
0

画像名をテーブルに保存するとき、URLのような名前を保存したい。PHPの画像名の前にURLを追加

たとえば、画像名の前に追加したいhttp://192.168.137.1/NI/uploads/のような準備完了URLがあります。

私はこの結果をしたい: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

これは使えないと思います –

+0

それはどういう意味ですか? –

答えて

1

URLを保存しないでください、UI上でそれを見せながら、単にプロジェクトのbase urlを追加します。これは、別のIPにアプリケーションをデプロイしたり、IPをドメイン名に変更すると機能しません。

名前を格納し、フロントエンドのデータを繰り返し処理している間にベースURLを追加したままにします。

1

この

パブリック関数のURLを試してみてください($ patheFile、$ imagenameの) { リターンのsprintf( 「%sの: 'HTTPS'!= 'オフ'? 'https': 'http'、: isset($ _ SERVER ['HTTPS'])& & $ _SERVER ['HTTPS']!= 'オフ'$ _SERVER ['SERVER_NAME']、 $ _SERVER ['REQUEST_URI'] ); } エコーURL( 'uplode'、$ _FILES [ 'PIC'] [ '名前'])

+0

私のコードに挿入できますか? –

+0

あなたのコードでこれを使用するか、この関数を関数呼び出しとして使用することができます $ image = sprintf( "%s://%s%s"、isset($ _ SERVER ['HTTPS'])&& $ _ SERVER ['HTTPS' ] = 'オフ'? 'https': 'http'、$ _ SERVER ['SERVER_NAME']、 '/ uploads /'。$ _FILES ['pic'] ['name']) – todary

関連する問題