您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關如何在php項目中利用curl實現一個圖片上傳功能,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
//上傳頁面代碼
$url = "http://192.168.1.100/upload.php?lang=cn"; #可以get傳相應參數 $file = $path.'/'. $Icon; //要上傳的文件 $fields['f'] = '@'.$file; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url ); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1 ); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields ); curl_exec( $ch ); if ($error = curl_error($ch) ) { die($error); } curl_close($ch);
//接收圖片資源
date_default_timezone_set('Asia/Shanghai'); //設置時區 $ip = ''; if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')){ $ip = getenv('HTTP_CLIENT_IP'); }elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')){ $ip = getenv('HTTP_X_FORWARDED_FOR'); }elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')){ $ip = getenv('REMOTE_ADDR'); }elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')){ $ip = $_SERVER['REMOTE_ADDR']; } preg_match("/[\d\.]{7,15}/", $ip, $ipmatches); $ip = $ipmatches[0] ? $ipmatches[0] : 'unknown'; //驗證請求IP,設置白名單 $date = date("[Y-m-d H:i:s]"); $allowip = array('192.168.1.100'); if(!in_array($ip,$allowip)){ $ipVal = "{$date}{$ip}\n"; file_put_contents('upload_ip.log', $ipVal, FILE_APPEN); die(-1); } //接收get傳參 $lang = isset($_REQUEST['lang']) ? $_REQUEST['lang'] : 'CN'; $uploaddir = "/data/cdncache/res/gifts/{$lang}/"; $uploadfile = $uploaddir . $_FILES['f']['name']; if (move_uploaded_file($_FILES['f']['tmp_name'], $uploadfile)) #保存圖片成功 { $type = $_FILES['f']['type']; $tmp_name = $_FILES['f']['tmp_name']; $error = $_FILES['f']['error']; $size = $_FILES['f']['size']; $log_txt = ' name:' . $_FILES['f']['name'] . ' type:' . $type . ' tmp_name:' . $tmp_name . ' error:' . $error . ' size:' . $size . " hello " . ' lang:' . $_REQUEST['lang']; $value = "{$date}{$log_txt}\n"; @file_put_contents('upload_ok.log', $value, FILE_APPEND); exit(); } else { $type = $_FILES['f']['type']; $tmp_name = $_FILES['f']['tmp_name']; $error = $_FILES['f']['error']; $size = $_FILES['f']['size']; $log_txt = ' name:' . $_FILES['f']['name'] . ' type:' . $type . ' tmp_name:' . $tmp_name . ' error:' . $error . ' size:' . $size . " hello " . ' lang:' . $_REQUEST['lang']; $value = "{$date}{$log_txt}\n"; @file_put_contents('upload_ng.log', $value, FILE_APPEND); exit(); }
以上就是如何在php項目中利用curl實現一個圖片上傳功能,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。