如何在没有html的情况下使用url单独上传php中的文件?

时间:2015-05-22 07:23:06

标签: php http

<?php
include_once 'dbconfig.php';
?>
<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr> 
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile"> 
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>

<?php

$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}

$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

mysql_query($query) or die('Error, query failed'); 
include 'library/closedb.php';

echo "<br>File $fileName uploaded<br>";

?>

我是php的初学者

我有一个像这样的php文件将文件上传到数据库,但我不需要html部分。我必须使用url将文件路径直接提供给php是否有办法做到这一点? 。

我的确切问题是我需要我的C#程序与服务器通信才能上传和下载文件,我只需要使用URL发送它,那么如何在url中发送实际路径而无需键入browse for the html中的路径

2 个答案:

答案 0 :(得分:1)

将这些内容保存到任何php文件中,并且每当你点击这个url并使用f = link链接到该文件时它会将该文件保存到你的specidifed文件夹中,我只是使用这个php文件所在的当前文件夹。

没有安全检查,所以它可以抓取任何文件,确保添加最终检查以执行身份验证并避免注入脚本。

$file = $_GET['f']; 
$res = explode('/',parse_url($file)['path']);   
$filename = $res[count($res)-1];    
file_put_contents($filename,file_get_contents($file));

希望这会对你有所帮助

此致

答案 1 :(得分:0)

您无法使用HTTP-GET上传文件!您必须使用您的请求在http正文中发送文件。因此,在大多数情况下,HTTP POST方法都用于。

Stackoverflow上的C#解决方案:

Sending Files using HTTP POST in c#

在Stackoverflow上使用cURL的PHP​​解决方案:

how to upload file using curl with php