Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Sending File Path to PHP

Discussion in 'Scripting' started by DoPie, Feb 18, 2020.

  1. DoPie

    DoPie

    Joined:
    Jun 20, 2013
    Posts:
    64
    Hi there,

    I would like to help me on my problem. my plan is to send File Path like video using unity to php and then upload it to my server.. i was very stuck on this problem :(

    Thanks
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,644
    Do you want to send a file path or a file itself?
     
  3. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    U can send file path with "C:\\my documents\\folder"

    You make sure to use quotation marks.
     
    Last edited: Feb 19, 2020
  4. DoPie

    DoPie

    Joined:
    Jun 20, 2013
    Posts:
    64
    i have this code :

    in PHP
    Code (CSharp):
    1.  
    2. <!DOCTYPE html>
    3. <html>
    4. <head>
    5.   <title>Upload your files</title>
    6. </head>
    7. <body>
    8. <form enctype="multipart/form-data" action="POFileUpload.php" method="POST">
    9.      Choose a file to Upload:
    10.      <input type="file" name="files[]" multiple="multiple" /><br>
    11.      <input type="submit" value="Upload File" />
    12. </form>
    13. </body>
    14. </html>
    15. <?php
    16.     header("Access-Control-Allow-Origin: *");
    17.     $total = count($_FILES['files']['name']);
    18.     $uploadError = false;
    19.     for ( $i = 0; $i < $total; $i++)
    20.     {
    21.       $tmpFilePath = $_FILES['files']['tmp_name'][$i];
    22.       if ($tmpFilePath != "")
    23.       {
    24.           $newFilePath = "Uploads/".$_FILES['files']['name'][$i];
    25.           if (!move_uploaded_file($tmpFilePath, $newFilePath))
    26.               $uploadError = true;
    27.       }
    28.     }
    29.     if ($uploadError)
    30.         echo "Upload Error";
    31.     else
    32.         echo "Uploaded Successfully";
    33. ?>
    and then this to call it in unity


    Code (CSharp):
    1.  
    2.     IEnumerator UploadMultipleFiles ()
    3.     {
    4.         string[] path = new string[3];
    5.         path[0] = "C:/Users/username/Desktop/Project/Assets//Texture/TestIcons/Refrence/Ultra Deep Bass Test !!.mp4";
    6.         path[1] = "C:/Users/username/Desktop/Project/Assets/Texture/TestIcons/Refrence/Ultra Deep Bass Test !!.mp3";
    7.         path[2] = "C:/Users/username/Desktop/Project/Assets/Texture/TestIcons/Refrence/scroll.png";
    8.  
    9.         UnityWebRequest[] files = new UnityWebRequest[path.Length];
    10.  
    11.         WWWForm form = new WWWForm();
    12.  
    13.         for (int i = 0; i < files.Length; i++)
    14.         {
    15.             files[i] = UnityWebRequest.Get(path[i]);  
    16.             Debug.Log (path[i]);
    17.  
    18.             yield return files[i].SendWebRequest();
    19.             form.AddBinaryData("files[]", files[i].downloadHandler.data, Path.GetFileName(path[i]));
    20.         }
    21.  
    22.         UnityWebRequest req = UnityWebRequest.Post("http://localhost/POFileUpload.php", form);
    23.         yield return req.SendWebRequest();
    24.  
    25.         if (req.isHttpError || req.isNetworkError)
    26.             Debug.Log(req.error);
    27.         else
    28.             Debug.Log("Uploaded " + files.Length + " files Successfully");
    29.     }
    And now my problem is the file that save on my server is 0mb or EMPTY File Size..
     
    Last edited: Feb 19, 2020
  5. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    try first without unity, see if move to folder, usign php

    If yes check unity code error seem here C:/Users/username/Desktop/Project/Assets//Texture/TestIcons/Refrence/Ultra Deep Bass Test !!.mp4"

    show in red but not sure if u can use filepath like this to transfer
     
    Last edited: Feb 19, 2020
  6. DoPie

    DoPie

    Joined:
    Jun 20, 2013
    Posts:
    64
    i already fix that but it also the same when i try to deploy it in unity webgl platform. the file still empty or 0mb size.
     
  7. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    try with small file 1st sometimes big not work, u set max file in php ini or use chunked file transfer protocol aswel sir