%0D wget with php

时间:2017-03-06 02:13:52

标签: php bash

我正在开展一个个人项目,允许我从网上获取文件。

我遇到了问题,所有文件名最后都有%0D,列在列表中的最后一个。

这是我的代码:

index.php:

<!DOCTYPE html>
<html>
    <form action="action.php" method="post" enctype="multipart/form-data">
        <textarea name="directlink" class="form-control" rows="3"></textarea>
        <button type="submit" name="send" class="btn btn-default">Send</button>
    </form>
</html>

action.php:

<?php

if (isset($_POST['directlink'])) {
    $file = str_replace(' ', '', $_POST['directlink']);
    $files = explode("\n", $file);
    foreach ($files as $value) {
    $final[] = base64_encode($value);
    }
$file = implode(" ", $final);
shell_exec('sudo -u user download/wget.sh ' . 'option' . ' ' . $file);
}

?>

Wget.sh:

#!/bin/bash

/download/wget_cmd.sh "$@" 2>&1

Wget_cmd.sh:

#!/bin/bash

for i in ${@:2}
do
    file=$(echo -n "${i}" | base64 --decode)
    wget "$file" -P /download/ 2>&1
done

1 个答案:

答案 0 :(得分:1)

您按\n分解textarea原始数据。如果用户使用Windows,您最终会得到\r。您可能希望改为preg_split

$files = preg_split('/[\r\n]+/', $file);

...然后可能修剪每个文件:

foreach ($files as $value) {
    $final[] = base64_encode(trim($value));
}

P.S。 PHP可以很好地执行HTTP,并不需要外部命令。