I use file_put_contents to upload an image, but it can only be saved in the current directory. How can I move the image to the desired directory?

WBOY
Release: 2016-10-11 14:23:23
Original
1590 people have browsed it

<code>$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);

</code>
Copy after login
Copy after login

I uploaded the image after converting it to Base64, but using the file_put_contents function can only be placed in the directory of the script. How can I move the uploaded image to the desired directory?

Reply content:

<code>$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);

</code>
Copy after login
Copy after login

I uploaded the image after converting it to Base64, but using the file_put_contents function can only be placed in the directory of the script. How can I move the uploaded image to the desired directory?

The first parameter filename of file_put_contents can take a path, so you only need to modify the $imageName variable to the directory and file name you want to move, similar to the following code:

<code>file_put_contents("../static/images/1.png", $imageBase64);</code>
Copy after login

Get familiar with the manual first https://secure.php.net/manual...
The saved path can be spliced, so you can splice it into the path you want to upload, or it can be an absolute path

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!