【问题标题】:PHP File Upload using url parameters使用 url 参数的 PHP 文件上传
【发布时间】:2011-02-20 23:09:06
【问题描述】:

有没有办法使用 php 和参数中的文件名(而不是使用提交表单)将文件上传到服务器,如下所示:

myserver/upload.php?file=c:\example.txt

我使用的是本地服务器,所以我没有文件大小限制或上传功能的问题,我有一个使用表单上传文件的代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<body>
  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="fileForm" enctype="multipart/form-data">
    File to upload:
    <table>
      <tr><td><input name="upfile" type="file"></td></tr>
      <tr><td><input type="submit" name="submitBtn" value="Upload"></td></tr>
    </table>  
  </form>
<?php    
if (isset($_POST['submitBtn'])){

    // Define the upload location
    $target_path = "c:\\";

    // Create the file name with path
    $target_path = $target_path . basename( $_FILES['upfile']['name']); 

    // Try to move the file from the temporay directory to the defined.
    if(move_uploaded_file($_FILES['upfile']['tmp_name'], $target_path)) {
        echo "The file ".  basename( $_FILES['upfile']['name']). 
             " has been uploaded";
    } else{
        echo "There was an error uploading the file, please try again!";
    }
}
?>
</body>  

感谢您的帮助

【问题讨论】:

  • @prasanna 您应该将您的评论移至答案。得到我的支持:)
  • 如果客户端和服务器在同一台机器上,这是可能的。否则,它不是。

标签: php file url upload


【解决方案1】:
Image Upload Via url:
//check
$url = "http://theonlytutorials.com/wp-content/uploads/2014/05/blog-logo1.png";
$name = basename($url);
try {
    $files = file_get_contents($url);
    if ($files) {
        $stored_name = time() . $name;
        file_put_contents("assets/images/product/$stored_name", $files);
    }
}catch (Exception $e){
}

【讨论】:

    【解决方案2】:

    如果您在本地执行此操作,是否需要使用“上传”机制?否则,如果您要向外部发送内容,您可以使用 cURL 上传文件,然后在 CLI 上运行 PHP 脚本,快速谷歌:http://dtbaker.com.au/random-bits/uploading-a-file-using-curl-in-php.html

    【讨论】:

      【解决方案3】:

      如果您想使用 url 发送文件名,如下所示:

      www.website.com/upload.php?hilton_plaza_party.jpg
      

      有一个脚本可以做到这一点。这里:http://valums.com/ajax-upload/

      【讨论】:

        猜你喜欢
        • 2014-01-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-21
        • 2018-04-18
        • 1970-01-01
        相关资源
        最近更新 更多