0

I'm trying to write code in php so that when the .php file is opened, it automatically uploads from a specific file address on my windows computer to the localhost server.

This is my attempt but I'm not sure I fully understand how to do this without using an HTML form where the user specifies the file they want to upload.

 <?php
 $target =  'UPLOADED_FILE.csv'; 
 move_uploaded_file('C:\Users\Ken.Feier\Desktop\temp\REPORT.csv', $target);
 ?>

I want the code to take the REPORT.csv file from my personal computer and upload it to our server with the file name UPLOADED_FILE.csv

UPDATE: I see that my problem will not be solved with php. Can anyone recommend any other solution involving Filezilla or any other FTP that can be automated?

Ken
  • 63
  • 13
  • you can't do that without a HTML form, no. PHP runs entirely on the server and never touches the local machine. the C:\ drive you're referencing there is the C: drive of the webserver. Unless you mean this PHP shown above is something which is running on your personal computer and you want to send the file to the webserver, where another script will process it? If so then you would need to make a HTTP request to the appropriate URL of the remote server and send it the file. It's not entirely clear what the situation with your code actually is. – ADyson Dec 05 '17 at 19:25

1 Answers1

1

That's not how it should be done.

You need a page with a html form, which will send the data to server on submit. Note that the file could be stored on your personal.

Form code e.g.

<form method="post" action="destination.php" enctype="multipart/form-data">
  <input type="file" name="filename" />
  <input type="submit" value="upload" />
</form>

Then, on the server, you can use the $_FILES['filename'] which contains your file's infos. Note that when a file is uploaded to the server, it's stored in the tmp folder, which is temporary, so you have to move your file to a persistent directory with move_uploaded_file(); (move_uploaded_file Docs)

E.g:

<?php

    $file = $_FILES['filename'];
    move_uploaded_file($file['tmp_name'], '/new/destination/for/the/filename.php');
Andriy Klitsuk
  • 564
  • 3
  • 14
  • I understand this method well. I was hoping there was a way to have a file auto-upload when it was opened. – Ken Dec 05 '17 at 19:56
  • @Ken try this - https://stackoverflow.com/questions/1217824/post-to-another-page-within-a-php-script – Andriy Klitsuk Dec 05 '17 at 20:04