gpt4 book ai didi

PHP cURL - 在 URL 中发现非法字符

转载 作者:行者123 更新时间:2023-12-05 02:17:46 24 4
gpt4 key购买 nike

我正在尝试使用数据库中的 URL 强制下载。来自数据库的 URL 是外部 URL。

使用 cURL,我正在尝试检查 URL 是否仍然存在,如下所示:

$id = strip_tags($_GET['dl']);

// grab stuff from db
$resource = $engine->runQuery("SELECT downloadurl, section FROM resources WHERE id=:id");
$resource->execute(array(':id'=>$id));
$row = $resource->fetch(PDO::FETCH_ASSOC);

// define memberonly sections
$memberonly = array(18, 7, 16, 19, 12);

if(in_array($row['section'], $memberonly)) {
if($engine->isLoggedin()) {
$dlurl = $row['downloadurl'];
$filename = basename($dlurl);

// curl lookup
$curl = curl_init($dlurl);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_exec($curl);
$retcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

if($retcode == 200) { // exists
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . $filename . "\"");
readfile($dlurl);
exit;
} elseif($retcode == 404) {
echo 'File does not exist.';
} else {
echo 'Server is busy. Please try again later.';
}
} else {
// head to root
header('Location: /');
}
}

cURL 不断返回 0,所以我使用 var_dump(curl_error($curl)); 来查看问题所在。它输出:

string(31) "Illegal characters found in URL"

这是数据库 URL 的样子:

http://gs2.ww.prod.dl.playstation.net/gs2/ppkgo/prod/CUSA00663_00/12/f_d2245740342e84367b73efbbf753a26de0aecbd69e6a494f1bdf4626cb52d3cc/f/UP0001-CUSA00663_00-AC5GAMEPS4000001-A0105-V0100_1.pkg

我该如何解决?

最佳答案

$filename = basename($dlurl);之后添加这一行$dlurl = urlencode($dlurl);

如下所示:

$dlurl = $row['downloadurl'];

$filename = basename($dlurl);

$dlurl = urlencode($dlurl);

也试试这个来从你的 url 中删除意外的字符:

$dlurl  = str_replace(' ', '', $dlurl ); // remove spaces
$dlurl = str_replace("\t", '', $dlurl ); // remove tabs
$dlurl = str_replace("\n", '', $dlurl ); // remove new lines
$dlurl = str_replace("\r", '', $dlurl ); // remove carriage returns

最终代码如下:

$dlurl = $row['downloadurl'];
$dlurl = str_replace(' ', '', $dlurl ); // remove spaces
$dlurl = str_replace("\t", '', $dlurl ); // remove tabs
$dlurl = str_replace("\n", '', $dlurl ); // remove new lines
$dlurl = str_replace("\r", '', $dlurl ); // remove carriage returns
$filename = basename($dlurl);

如果您这样做,则不需要 urlencode。

关于PHP cURL - 在 URL 中发现非法字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47209524/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com