如何获取下载文件的名称(PHP)

时间:2015-07-23 17:50:18

标签: php file download get

如何以原始名称下载文件? 网址没有名称。

网址格式:http://someurl/?RANDOMSYMBOLS

URL返回命名文件,但所有已知的PHP方法都会删除到服务器RENAMED文件,其中包含MY名称,而不是原始文件。

我使用file_put_contents("Tmpfile.zip", fopen("http://someurl/?RANDOMSYMBOLS", 'r')),但它将文件删除为"Tmpfile.zip",而不是原始名称(导航到url时常见用户的常用用户获取的文件名)。

我想知道该文件的名称并随之丢弃。 URL格式为"example.com/?filename"时没问题,但不是,它不包含名称。

1 个答案:

答案 0 :(得分:0)

你必须阅读http响应头,http头包含fileName,size ...等 并且必须使用curl因为fopen不显示标题

@receiver(post_delete, sender = Student)
def add_score(instance, **kwargs):
   newChange = Changes(table_name = 'Student',type='delete')
   newChange.save()

@receiver(post_delete, sender = Group)
def add_score(instance, **kwargs):
   newChange = Changes(table_name = 'Group',type='delete')
   newChange.save()

打印$ header以查看带有

的标题列表
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/file.zip");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);

$response = curl_exec($ch);

// Then, after your curl_exec call:
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);

和$ body包含文件数据

相关问题