使用自定义名称集进行PHP远程文件下载

时间:2014-04-21 12:24:33

标签: php file file-upload upload

我这里有这个脚本,它让我的远程文件到我的服务器

问题是,如果我想重命名文件,我必须手动ftp到服务器并重命名该文件。无论如何在下载之前命名文件?

<?php

if($_POST){
//get the url
$url = $_POST['url'];

//add time to the current filename
$name = basename($url);
list($txt, $ext) = explode(".", $name);
$name = $txt.time();
$name = $name.".".$ext;

//here is the actual code to get the file from the url and save it to the uploads folder
//get the file from the url using file_get_contents and put it into the folder using file_put_contents
$upload = file_put_contents("uploads/$name",file_get_contents($url));
//check success
if($upload)  echo "Success: <a href='uploads/".$name."' target='_blank'>Check Uploaded</a>"; else "please check your folder permission";
}
?>

<html>
<head><title>File Upload from URL</title></head>
<body>
<h3>File Upload from URL</h3>
    <form action="" method="post">
        Your URL: <input type="text" name="url" />
    </form>
</body>
</html>

任何人都可以帮忙,我会很开心。

我必须手动重命名文件,这就是问题,无论如何我可以在上传前指定名称吗?

提前致谢

2 个答案:

答案 0 :(得分:0)

新文件的路径是file_put_contents的第一个参数,只是通过"uploads/{$new_name}"

其中$new_name= "desired_file_name.". $ext";

答案 1 :(得分:0)

使用php重命名方法... Check

<?php
    rename("file.txt", "new_file.txt");
?>