How to upload files to usb drive using PHP

if (isset($_POST['uploadfile'])) {
  
$filepath = "D:/interact/interact_storage/storage/$username/storage/";
$filename = $_FILES["file"]["name"];

if (!is_dir($filepath)) {
   mkdir($filepath, 0777, true);
};

move_uploaded_file($_FILES["file"]["tmp_name"], $filepath . $filename);
echo "File Uploaded.";
}

I keep getting permission denied/cannot open stream

Where? On is_dir, mkdir or move_uploaded_file?

move_uploaded_file

Are you sure the value of $_FILES["file"]["tmp_name"] is correct?

I am certain because I can change the filepath, and everything works.

I honestly don’t know what might be wrong here. Maybe @benanamen or @mabismad know?

OP, are you using IIS for a server?

yes I am using iis

First, I would recommend NOT using IIS.

As to your op, you need to enable write permissions for the IUSR account.

A recommendation like that needs some explanation, don’t you think?

Explaining the cons of IIS would take this thread way off topic.

I enabled those permission for my usb drive through properties, which I know well how to do but that is not working; do I need to enable these permissions in iis manager somewhere?

This might help:

Warning : mkdir(): Permission denied in C:\inetpub\wwwroot\interact\index.php on line 471

Warning : move_uploaded_file(D:/interact/interact_storage/storage/jonathan/storage/background.jpg): Failed to open stream: Permission denied in C:\inetpub\wwwroot\interact\index.php on line 474

Warning : move_uploaded_file(): Unable to move “C:\Windows\Temp\phpF59D.tmp” to “D:/interact/interact_storage/storage/jonathan/storage/background.jpg” in C:\inetpub\wwwroot\interact\index.php on line 474

I would suggest that the first of those is causing the other two - it won’t allow you to create the folder, so the next two operations which involve that folder fail.

I have no idea how to fix it in IIS. Maybe posting in the “Server config” area might be an idea? Although it’s PHP that is revealing the problem, it seems that it’s a user permissions thing that is the actual problem.

Thanks everyone! I found that the usb drive had additional permissions that are not seen everywhere, and just used a different drive.