douyi9597 2016-01-29 23:05
浏览 114

PHP cURL脚本在文件名末尾添加“空格”

I'm calling a PHP script via AJAX that saves a remote image locally (via Mapbox API). Everything works great, except for the result includes a 'space' (filename.jpg[space]) at the end of the string.

Javascript:

var mapboxUrl = 'https://api.mapbox.com/v4/motioncrafter.96d11990/' + longitude + ',' + latitude + ',11/1280x720@2x.jpg80?access_token=pk.eyJ1IjoibW90aW9uY3JhZnRlciIsImEiOiJjaWg2dHJvM2YwNndudjJrdGI2amUzdnlyIn0.oN201-rjUC4DHhz7QhGJRA';
      $.ajax({
        type: "POST",
        url: "../../php/map.php",
        data: {
          map: mapboxUrl,
            },
        success: function(result) {
          $('.video_map').val(result);
        }
      });

PHP:

<?php
$mapboxUrl = $_POST["map"];
$path = "../images/user-maps/";
$imageFile = $path . "map_wide_" . rand(10000, 99999) . time() . ".jpg";
$ch = curl_init($mapboxUrl);
$fp = fopen($imageFile, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

echo $imageFile;
?> 

Any idea why this space is showing up?

  • 写回答

1条回答

  • dongzhan5246 2016-01-30 00:34
    关注

    Get rid of this line:

    ?>
    

    I suspect you have a space after that. Everything outside <?php ... ?> gets sent to the client, so if it's actually ?><space>, the space will be appended to the image filename.

    There's no need to have ?> at the end of a script, the end of the file automatically tells PHP to stop processing the code.

    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路