Rumah  >  Artikel  >  pembangunan bahagian belakang  >  php实现文件上传和下载的方法(代码)

php实现文件上传和下载的方法(代码)

不言
不言asal
2018-08-21 10:44:395265semak imbas

本篇文章给大家带来的内容是关于php实现文件上传和下载的方法(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

1.传入参数作为function里数组的key

function test2($name){
    $ar = (object) array(     
    $name => 1,     
    "image_id"=>1234
    );    
    echo $ar->$name;
}
//获取host_id的value
test2(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); 
            }

相关推荐:

php实现文件下载和多文件上传

php之文件上传和下载

Atas ialah kandungan terperinci php实现文件上传和下载的方法(代码). Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn