【发布时间】: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 您应该将您的评论移至答案。得到我的支持:)
-
如果客户端和服务器在同一台机器上,这是可能的。否则,它不是。