douwu8251 2017-02-26 04:52
浏览 54
已采纳

上传到服务器后,Php生成了错误的格式url文件路径

Below is the code for me to generate a unique file Url of each image uploaded and store it to database.

    //create unique image file name based on micro time and date
    $now = DateTime::createFromFormat('U.u', microtime(true));
    $id = $now->format('YmdHisu');

    // Path to move uploaded files
    $target_path = "uploads";
    //the file name
    $path = "$target_path/$id.jpeg";

    // getting server ip address
    $server_ip = gethostbyname(gethostname());

    // final file url that is being uploaded
    $file_upload_url = 'http://'. $server_ip . '/' . 'abc' . '/' .$path;

What I expect for this code should generate the url in json_encode as below:

http://111.111.11.1/abc/uploads/20170226041004809200.jpeg

But now the problem is it generate the Url but in json_encode result is like below

http:\/\/111.111.11.1\/abc\/uploads\/20170226041004809200.jpeg

What I tried so far,

$file_upload_url = 'http:'. $server_ip  . 'abc'  .$path;

But it not what I want,it become this

http:111.111.11.1.1abcuploads\/20170226041515563600.jpeg

Can somebody have any idea how to solve this?Or have a better suggestion?

  • 写回答

2条回答 默认 最新

  • dongpai2468 2017-02-26 04:56
    关注

    Use str_replace to replace the \

    <?php
       $file_upload_url = "http:\/\/111.111.11.1\/abc\/uploads\/20170226041004809200.jpeg";
       $upload_path = str_replace('\\','',$file_upload_url);
       echo $upload_path;
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件