curl-and-php
Uploading file
Date: Sat, 6 Jul 2002 14:46:25 +0200
Hi all,
I know this has already been discussed but I stil have problems to make it
work: I would need to automatically upload a local file from a client PC so
I was thinking using cURL to simulate filling a form to pass the file name
to this form.
So I started testing something like the code below:
- cURL to call the HTML form:
<?
$APP="http://host/bin/curl1.php";
$file="d:\\tv.jpg";
if (isset($file))
{
$ch = curl_init($APP);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file'=>"@$file"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$postResult = curl_exec($ch);
curl_close($ch);
print "$postResult";
print "$file";
}
else
{
print "<form enctype=\"multipart/form-data\ action=\"curl1.php\"
method=\"post\" >\n";
print "</form>";
}
?>
- HTML form:
<form action="<?echo $PHP_SELF?>" enctype="multipart/form-data" method=POST>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<input type=file name=file size=29>
<input type="submit" value=" Send File ">
</form>
<?
if($file) {
$formats = array('jpg','gif','png');
$dest = "images/$file_name";
if(in_array(strtolower(substr($file_name,-3)),$formats)) {
if(!copy($file,$dest)) {
echo "Unable to create $dest - check permissions<br>\n";
exit;
}
}
}
else {
print "no file";
}
$fd = opendir("images");
while($entry = readdir($fd)) {
if(eregi("\.(jpg|gif|png)$",$entry)) {
echo "<a href=\"images/$entry\">";
echo "<img src=\"images/$entry\" align=middle border=0 height=80
width=100>";
echo "</a> $entry<br>\n";
}
}
closedir($fd);
?>
It seems to be working an a test machine (NT4, Apache 1.3.19, PHP 4.0.6,
cURL 7.8) but I moved things to another server and it fails to POST the file
name (NT4, Apache 2.0.36, PHP 4.2.1, cURL 7.9)
Any idea?
Could anyone provide me with a working piece of code to do sthg like this?
(Sorry I'm a newbie and I would need this to be able to make a demo site
next week- would be able to spend more time soon to work on this)
Any help much appreciated,
Thanks
-------------------------------------------------------
This sf.net email is sponsored by: Jabber - The world's fastest growing
real-time communications platform! Don't just IM. Build it in!
http://www.jabber.com/osdn/xim
Received on 2002-07-25