使用url参数上传PHP文件

时间:2011-02-20 23:09:06

标签: php file url upload

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

  

MYSERVER / upload.php的文件= 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>  

感谢您的帮助

3 个答案:

答案 0 :(得分:2)

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){
}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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

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