python php/html file upload issue

S.Selvam Siva s.selvamsiva at gmail.com
Sat Mar 21 15:47:36 CET 2009


 Hi all,
I want to upload a file from python to php/html form using urllib2,and my
code is below

PYTHON CODE:
import urllib
import urllib2,sys,traceback
url='http://localhost/index2.php'
values={}
f=open('addons.xcu','r')
values['datafile']=f.read() #is this correct ?
values['Submit']='True'
data = urllib.urlencode(values)
req = urllib2.Request(url,data)
response = urllib2.urlopen(req,data)
the_page = response.read()
print the_page

PHP/HTML CODE:
html>

<body>
    <?php
    if(isset($_POST["Submit"]))
    {
       echo "upload-file-name:".$_FILES['datafile']['name']."<br/>";

    } else {
              ?>
        <form enctype="multipart/form-data" action="<?php echo
$_SERVER["PHP_SELF"]; ?>" method="post">
        Please enter a file name :<input type="file" name="datafile"
size="100">
         <input type="submit" value="submit" name="Submit"/>
        </form>
        <?php
    }
    ?>
</body>
</html>

But the *$_FILES['datafile']['name']* in response is always empty...I can
not guess what went wrong with my code ,
I will be happy, if you can figure out the problem.


-- 
Yours,
S.Selvam
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090321/9899161b/attachment.html>


More information about the Python-list mailing list