您好,欢迎访问一九零五行业门户网

我用file_put_contents上传图片,但是只能保存在当前目录,怎么把该图片移动到想要的目录呢?

$id = $_post['id'];$name = $_post['name'];$address = $_post['address'];$phonenumber = $_post['phonenumber'];$image = $_post['image'];//得到图片$imagebase64 = base64_decode($image);$host = $_server['http_host'];//获取主机ip$post = $_server['http_port'];//获取主机端口$imagename = $id..png;//生成的图片名称和用户id一样$file_put = file_put_contents($imagename, $imagebase64);

我是把图片转换为base64后上传的,但是用file_put_contents函数只能放在该脚本的目录,请问怎么把上传的图片移动到想要的目录下呢?
回复内容: $id = $_post['id'];$name = $_post['name'];$address = $_post['address'];$phonenumber = $_post['phonenumber'];$image = $_post['image'];//得到图片$imagebase64 = base64_decode($image);$host = $_server['http_host'];//获取主机ip$post = $_server['http_port'];//获取主机端口$imagename = $id..png;//生成的图片名称和用户id一样$file_put = file_put_contents($imagename, $imagebase64);

我是把图片转换为base64后上传的,但是用file_put_contents函数只能放在该脚本的目录,请问怎么把上传的图片移动到想要的目录下呢?
file_put_contents的第一个参数filename是可以带路径的,所以你这里只要修改$imagename变量为你要移动的目录以及文件名即可,类似如下代码:
file_put_contents(../static/images/1.png, $imagebase64);
熟悉一下手册先https://secure.php.net/manual...
保存的路径可以拼接的,所以,拼接成你想要上传的路径,也可以是绝对路径哦
其它类似信息

推荐信息