dongwupu5991 2015-02-07 20:02
浏览 230
已采纳

文件名中的特殊字符导致问题

$imageURL = 'http://www.cnsnews.com/sites/default/files/images/OBAMA-AP%20PHOTO_8.jpg';

$imageInfo = pathinfo($imageURL);
$imageName = clean(basename($imageURL,'.'.$imageInfo['extension']));
$fileName =  $imageName.'.'.$imageInfo['extension'];// file name

$content = file_get_contents($imageURL);

file_put_contents(UPLOAD_DIR.$fileName, $content);   
$product_file = UPLOAD_DIR.$fileName;


function clean($string) {
   $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
   $string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.

   return preg_replace('/-+/', '-', $string); // Replaces multiple hyphens with single one.
}

.. although almost all urls I've tried work fine except this one, probably because of the special chars in OBAMA-AP%20PHOTO_8.jpg? even though I'm cleaning it?

The image doesn't get downloaded.

  • 写回答

2条回答 默认 最新

  • duan5801 2015-02-07 20:13
    关注

    (editing my previous answer, it wasn't very helpful)

    triage the problem -- does the file_get_contents($imageURL) return data? Print the length of the returned string to test, echo strlen($contents);

    Can you create a file that contains a '%' in the filename?

    I can download the imageURL as typed, so you're most likely right, it's the % in the save-to filename that's going to be the problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测