本篇文章給大家帶來的內容是關於php實現檔案上傳和下載的方法(代碼),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所協助。
1.傳入參數作為function裡數組的key
function test2($name){ $ar = (object) array( $name => 1, "image_id"=>1234 ); echo $ar->$name;}//擷取host_id的valuetest2(host_id);
2.檔案下載
dest_path為’/tmp/images/raw_image.jpg’
image_src 為圖片的http路徑
//如果檔案存在,已經下載過,刪除該檔案 if (file_exists($dest_path)) { unlink($dest_path); } //下載對應的檔案 $f_output = fopen($dest_path, 'a'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $image_src); curl_setopt($ch, CURLOPT_FILE, $f_output); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt($ch, CURLOPT_LOW_SPEED_LIMIT, 2); curl_setopt($ch, CURLOPT_LOW_SPEED_TIME, 60); $result = curl_exec($ch); curl_close($ch); fclose($f_output); if ($result) { break; }
3.檔案上傳
url為上傳路徑
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_VERBOSE, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => $bs64)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $res = curl_exec($ch); curl_close($ch); }