2
<?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>";

?>

I'm a beginner in php

I have a php file like this to upload a file into a database, but i don't need the html part. i have to give the file path directly to the php using the url is there a way to do it ?? .

My exact problem is i need my C# program to communicate with the server to upload and download a file, I need to send it only using the URL so how can i send the actual path in url without need to type browse for the path in html

Sougata Bose
  • 31,517
  • 8
  • 49
  • 87

2 Answers2

1

save these contents into any php file and than whenever you hit this url with f= link to the file that it will save that file into your specidifed folder where i'm just using the current folder where this php file resides.

there are not security checkes so it can grab any file , make sure to add final checks to perform authentication and avoid injection to the script.

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

hope this will help you

Regards

imran qasim
  • 1,050
  • 13
  • 18
  • Actually don't need to save the file in location if you notice i have saved the file in a database in my code, but you got my point correctly with the URL part !! but how to make my code to recognize the file, as im already using the $ _FILES thing how to post the file to the FILES 'userfile' ?? – culkinnivas sekar May 22 '15 at 08:08
0

You can not upload file using HTTP-GET! You have to send the file in http body with your request. So in most scenarios the HTTP POST Method ist used for.

C# Solution on Stackoverflow:

Sending Files using HTTP POST in c#

PHP Solution with cURL on Stackoverflow:

how to upload file using curl with php

take
  • 2,202
  • 2
  • 19
  • 36
  • I think the PHP curl thing is good, but i still can't understand how to use that in my context . can you tell me how can i use that in my code, I'm very bad at PHP – culkinnivas sekar May 22 '15 at 07:52