本篇文章给大家带来的内容是关于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实现文件上传和下载的方法(代码)的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号