Home > Software design >  Upload a file to php server with filename in the url
Upload a file to php server with filename in the url

Time:06-16

I have a file named "test.txt" which I need to upload to a php server. The contents of the file changes but the file name does not. I want to automate it using curl (it is being upload from a unix box).

So the url would look like: curl http://myserver.com/upload.php?fname="test.txt"

I am having a problem with the php code to accept the file name automatically. I am able to upload the file using the html input/submit, but that requires manual intervention.

Here is the code I have. I works properly for manual input. Can some please show me how to change the manual input to auto (from the url)?

I am fairly new to php so any guidance would be appreciated.

Thanks

<?php
   if(isset($_FILES['fname'])){
      $errors= array();
      $file_name = $_FILES['fname']['name'];
      $file_tmp =$_FILES['fname']['tmp_name'];
      $file_type=$_FILES['fname']['type'];
      $file_ext=strtolower(end(explode('.',$_FILES['fname']['name'])));
      
      $extensions= array("txt");
      
       if(empty($errors)==true){
         move_uploaded_file($file_tmp,"download/".$file_name);
         echo "Success";
      }else{
         print_r($errors);
      }
   }
?>
<html>
   <body>
      
      <form action="" method="post" enctype="multipart/form-data">
         <input type="file" name="fname" />
         <input type="submit"/>
      </form>
      
   </body>
</html>

CodePudding user response:

SOLUTION: The php file is fine as is. The solution lies in the correct curl script. In this case: curl -F "uploadedfile=@/path/to/my/file" mywebserver.com/uploads.php This will post the file name into the query without user intervention. Thank you to those who gave advice.

  • Related