File upload to remote server using php curl

Posted by midhun pottmmal On Saturday, 22 December 2012 7 comments


           Upload a file to remote server from html form; you have used the same option as for FORM POST METHOD but with some changes.

Use $_FILE PHP VARIABLE TO READ THE UPLOAD FILE AND PASS IT TO POSTFILED.



       Here to php file 1.curl.php and second is 2.upload.php

     
                                               

 1. curl.php

        
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
// Upload a file to remote server.
// Upload file field on form having multipart/form-data.
// written by scriptime.blogspot.in
set_time_limit(0);
$url = 'http://localhost/curl/upload.php'; // change to your form action url.
$field_name = 'file'; // please edit it according to your form file field name.
if (isset($_FILES['file']))
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("$field_name"=>"@".$_FILES['file']['tmp_name']));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
}
else
{
print "<form enctype=\"multipart/form-data\" "
. "action=\"$PHP_SELF\" method=\"post\" >\n";
print '<p>
<input type="file" name="file">
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>';
print "</form>";
}
?>

2.upload.php



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<?php
if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br>";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br>";
  echo "Type: " . $_FILES["file"]["type"] . "<br>";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }
?>


7 comments:

CharanRaj Bammidi said...

Good Post..i use this script in one of my coding...
Thankyou 4 sharing

midhun pottmmal said...

thx charan

Pixel Thennarasu said...

its work for me.but when i run the code show message top of page Notice: Undefined variable: PHP_SELF in D:\xampp\htdocs\set\curl.php on line 19 how to remove?

Liker Boy said...

Wow, Excellent post. This article is really very interesting and effective. I think its must be helpful for us. Thanks for sharing your informative.
social exchange sites
earn money online
social bookmarking sites list
directory submission site list
article submission sites
blog commenting sites
forum posting sites
press release sites list
outsourcing
off page seo
seo tutorial
free seo tools
freelancing
freelancing sites
seo

El Taufan said...

Thanks for Sharing That... Sucses for You

findaunionprinter

findaunionprinter

getoifile

getdriversforpc

offlineinstallerfilehippo

theprinterdriver

esoftpedia

filehorse

thesoftpedia

caranddriver

smadav

Tahir Bahi said...
This comment has been removed by the author.
Tahir Bahi said...

Hello my friend! I want to say that this post is awesome, great written and include almost all
important infos.I really love this post I will visit again to read your post in a very short time and I hope you will make more posts like this.

T H A N K S
F O R
S H A R I N G

WinAVI Video Converter 11.5 Keygen
Trend Micro Antivirus
Movie Converter 7 Keygen
Neuratron Audio Score Ultimate Keygen

Post a Comment

Fashion