使用php脚本从Server下载并重命名文件 [英] Download and rename file from Server using php script

查看:132
本文介绍了使用php脚本从Server下载并重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在存储所有我的文件,如8asd98asd9as7d98asd9.file,并从mysql-database中获取真实文件名:id(INT)AI PK,pathOnServer TEXT NOT NULL,realFilename VARCHAR(200)NOT NULL。 p>

我需要一个脚本,让我可以访问www.website.com/getFile?id=2这样的文件,它下载文件并将其重命名为realFilename。



如何做到这一点?

解决方案

代码会做一个窍门你寻找

 <?php 

if(!is_numeric _GET ['id'])){//我们验证参数
echo'Wrong parameter';
exit();
}

$ oDb = new mysqli(ip,user,pass,databasename); //连接数据库
if($ oDb-> connect_errno){//检查错误
echo'Error';
exit();
}

$ oResult = $ oDb-> query('SELECT`pathOnServer`,`realFilename`
FROM --tablename--
WHERE id =' 。(int)$ _ GET ['id']。'
LIMIT 1'); // make query
if($ oResult-> num_rows!= 1){
echo'No file found';
exit();
}

$ oQuery = $ oResult-> fetch_object();
header('Content-disposition:attachment; filename ='。$ oQuery-> realFilename); // put special headers
readfile($ oQuery-> pathOnServer。$ oQuery-> id); //把文件
?>

您还可以使用filesize添加标题。
希望会帮助你。


I am storing all my files like "8asd98asd9as7d98asd9.file" and rerieve the "real" filename from a mysql-database: id (INT) AI PK, pathOnServer TEXT NOT NULL, realFilename VARCHAR(200) NOT NULL.

I need a script which allows me to access the files like "www.website.com/getFile?id=2" which downloads the file and renames it to "realFilename".

How can this be done?

解决方案

I do believe this code will do a trick you look for

<?php

   if(!is_numeric($_GET['id'])) { // We validate parameter
       echo 'Wrong parameter';
       exit();
   }

   $oDb = new mysqli("ip","user","pass","databasename"); // Connecting to database
   if($oDb->connect_errno) { // Check for an error
      echo 'Error';
      exit();
   }

   $oResult = $oDb->query('SELECT `pathOnServer`, `realFilename`
                           FROM --tablename--
                           WHERE id = '.(int)$_GET['id'].'
                           LIMIT 1'); // make query
   if($oResult->num_rows != 1) {
       echo 'No file found';
       exit();
   }

   $oQuery = $oResult->fetch_object();
   header('Content-disposition: attachment; filename='.$oQuery->realFilename); // put special headers
   readfile($oQuery->pathOnServer.$oQuery->id); // put the file
?>

You could also add the header with filesize. Hope it will help you.

这篇关于使用php脚本从Server下载并重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆