开发者

How to upload dynamic image instead of url in php

开发者 https://www.devze.com 2022-12-07 18:32 出处:网络
I am working with Php and tyring to upload image with "file_get_contents" ( because creating api for upload image),My following code is working fine but this is static url, So how can i conv

I am working with Php and tyring to upload image with "file_get_contents" ( because creating api for upload image),My following code is working fine but this is static url, So how can i convert this to dynmaic url so i can upload dynamic images instead of static url, Here is my current code which is worki开发者_如何学编程ng fine

$url = 'http://xxxxxxxxxxxxxxxxxxx/images/abc.png';
$filename = substr($url, strrpos($url, '/') + 1);
file_put_contents('uploads/'.$filename, file_get_contents($url));

But how can i upload image dynamically with following code

$data = json_decode(file_get_contents("php://input"), TRUE);
$file_name =$_FILES['file']['name']; 


To do this, first make sure that your HTML form allows for file uploads and submits the file data to your PHP script via the POST method. Then, in your PHP script, you can access the uploaded file data through the $_FILES variable, which is an associative array containing information about the uploaded file, such as its name and content.

You can also use the move_uploaded_file() function to move the uploaded file from its temporary location to a directory of your choice, instead of using the file_put_contents() function. This would allow you to save the file to a different directory than the temporary directory where the file was initially uploaded.

Here is an example of how this solution could be implemented using the move_uploaded_file() function:

// Get the uploaded file name
$file_name = $_FILES['file']['name'];

// Move the uploaded file from its temporary location to the "uploads" directory
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/'.$file_name);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号