0

Basically I am scanning whole system using python directory listing technique and sending all files to PHP script from where I want to store them in a folder named as project on my computer. I saw available solution of my problem but these solutions are not working for me.

How to upload & Save Files with Desired name

my python script:

url = 'http://localhost:9090/project/php/test.php'
driveStr = subprocess.check_output("fsutil fsinfo drives")
driveStr = driveStr.decode("utf-8")
driveStr = driveStr.strip().lstrip('Drives: ')
drives = driveStr.split()
print(drives)
mylist = []
for drive in drives:
    if os.path.isdir(drive):
        p = os.walk(drive)
        for i, dirs, files in p:

            for filename in files:
                if not filename.endswith ( ".txt" ) :
                    continue
                print ( filename )
                post = { 'filename' : filename }
                req = urlencode ( post ).encode ( "utf-8" )
                req = requests.post ( url , post )

I am scanning whole system for text files and sending these files to PHP script on server.

my PHP script:

if(isset($_POST['filename']))
{
    
    $file = $_POST['filename'];
    $dir = "C:\wamp64\www\project";
    move_uploaded_file($file, $dir);
}

It is not storing those file into target directories. But when I saved them in any file on computer those files successfully saved but I am unable to store them in a directory.

programmer
  • 124
  • 2
  • 10
  • You don't seem to be uploading any files, but rather just be posting the filenames. If they are on the same server, you might want to simply [copy()](https://www.php.net/manual/en/function.copy.php) the files instead of trying to upload them. – M. Eriksson Jun 17 '21 at 05:50
  • @MagnusEriksson I have already tried copy() but it didn't solve my issue. – programmer Jun 17 '21 at 05:56
  • So is it on the same server (seeing that you're using localhost)? If yes, then you most likely implemented that code wrong. Show that code instead. Make sure that you send the filename with the absolute path (if you're traversing the files recursively in python) so that PHP knows the exact location of the source file. Or skip python/php all together and just copy the files using the OS? :-) What is this for? – M. Eriksson Jun 17 '21 at 05:59
  • @MagnusEriksson I am using copy function in this way. `$file = $_POST['filename']; $dir = "C:\wamp64\www\project"; copy($file, $dir);` – programmer Jun 17 '21 at 06:02
  • 1
    `$file` needs to contain the absolute path to the file, not just the name of it, or PHP won't know where to look for it. Why can't you just copy/paste the files using Windows file explorer if they are on the same computer? – M. Eriksson Jun 17 '21 at 06:05
  • @MagnusEriksson I can't give absolute path to $file because I am posting all available files on system to PHP every file has different path. Can you please help me how can I do this? – programmer Jun 17 '21 at 06:10
  • At some point you need to actually answer my questions. **Are these scripts on the same computer or not?** If yes, why do it with code instead of just copy/pasting in Windows file explorer? If you do it with code, you _must_ send the full path to the file for PHP to have a clue where to find the file. Just a filename isn't enough. If they are not on the same computer, you need to read up on how to do a proper file upload using Python (since you currently _only pass the filename, not the file_) and then read up on how to properly handle file uploads in PHP. Too many unanswered questions here. – M. Eriksson Jun 17 '21 at 07:31
  • @MagnusEriksson they aren't on same server that why I am not using Windows file explorer – programmer Jun 17 '21 at 07:40
  • 1
    Then you need to do as I suggested, read up on how you do a real file upload in Python (I don't know Python) since you currently only seem to be posting the filename, not the file. and then [how to handle file uploads in PHP](https://www.php.net/manual/en/features.file-upload.post-method.php). Take it one step at the time. If you want the files to be stored in some specific file structure, you need to pass that info along as well (what sub folder(s) the file should be store in etc) – M. Eriksson Jun 17 '21 at 08:04

0 Answers0