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 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用