简体   繁体   English

php 如何在通过 curl 上传之前重命名文件

[英]php how can i rename file before upload via curl

i need to rename the image file before upload how can i do that i can use the rename but it moves the file to a different dir file_get_contents to change the name is not working for me i need to rename this part /C-laragon-www-api-backend-screenshots-1-jpg.jpg this is the path on my system我需要在上传之前重命名图像文件我该怎么做我可以使用重命名但它将文件移动到不同的目录 file_get_contents 更改名称对我不起作用我需要重命名这部分/C-laragon-www-api-backend-screenshots-1-jpg.jpg这是我系统上的路径

here is my function这是我的功能

/**
 * @param $image
 * @return mixed|exception
 */
    function imgUpload($image)
    {
        try {
            $API_KEY = 'xxxxx';
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, 'https://api.imgbb.com/1/upload?key=' . $API_KEY);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POST, 1);
            $extension = pathinfo($image, PATHINFO_EXTENSION);
            $file_name = uniqid() . '.' . $extension;
            $data = [
                'image' => base64_encode(file_get_contents($image, $file_name))
            ];
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            $result = curl_exec($ch);
    
            curl_close($ch);
        } catch (Exception $e) {
            $e->getMessage();
        }
        return json_decode($result, true);
        
    }

the output输出

 ["data"]=>
  array(12) {
    ["id"]=>
    string(7) "xxxxx"
    ["title"]=>
    string(49) "C-laragon-www-api-backend-screenshots-1-jpg"
    ["url_viewer"]=>
    string(22) "https://ibb.co/xxxx"
    ["url"]=>
    string(78) "https://i.ibb.co/xxxx/C-laragon-www-api-backend-screenshots-1-jpg.jpg"
    ["display_url"]=>
    string(78) "https://i.ibb.co/xxxx/C-laragon-www-api-backend-screenshots-1-jpg.jpg"
    ["size"]=>
    int(435765)
    ["time"]=>
    string(10) "xxxxxx"
    ["expiration"]=>
    string(1) "0"
    ["image"]=>
    array(5) {

Does this work for you:这对你有用吗:

    function imgUpload($image)
    {
        try {
            $file = new \CURLFile("@{$image}"); //<-- Path of the image with @ in front
            $data = array('name' => 'New name of image', 'file' => $file);

            $API_KEY = 'xxxxx';
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, 'https://api.imgbb.com/1/upload?key=' . $API_KEY);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POST, 1);
            $extension = pathinfo($image, PATHINFO_EXTENSION);
            // $file_name = uniqid() . '.' . $extension;

            curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

            $result = curl_exec($ch);
    
            curl_close($ch);
        } catch (Exception $e) {
            $e->getMessage();
        }
        return json_decode($result, true);
        
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM