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

4
  • 2
    You want solution in C# or PHP ? Commented May 22, 2015 at 7:24
  • i want solution PHP (better), cause C# is restricted in unity Commented May 22, 2015 at 7:31
  • so, if i understand you correctly, you want the file name (or path and name) and nothing else as a result of the upload? Commented May 22, 2015 at 7:39
  • yeah, i need to upload the file using the URL like using $_GET command in php. not by clicking browse button in http i don't need the http part.. Commented May 22, 2015 at 7:42

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

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' ??
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

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.