【问题标题】:PHP remote file download with custom name set使用自定义名称集的 PHP 远程文件下载
【发布时间】:2014-04-21 12:24:33
【问题描述】:

我这里有这个脚本,它可以将远程文件发送到我的服务器

问题是,如果我想重命名文件,我必须手动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>

谁能帮忙,我会很高兴的。

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

提前致谢

【问题讨论】:

    标签: php file file-upload upload


    【解决方案1】:

    新文件的路径是file_put_contents的第一个参数,直接传入"uploads/{$new_name}"

    在哪里$new_name= "desired_file_name.". $ext";

    【讨论】:

    • 我仍然需要编辑 php 文件,我的意思是一个输入框,我可以在其中指定名称
    • 你可以用 Javascript 的FileAPI 做类似的事情 - 但它有点矫枉过正并且不兼容浏览器。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-10
      • 1970-01-01
      • 1970-01-01
      • 2018-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-24
      相关资源
      最近更新 更多