php FILE POST upload without save

前端 未结 6 2314
深忆病人
深忆病人 2021-02-18 23:49

I am using a plain text file in a PhP script. Is there any way to process the file in the PhP script without saving it locally? Everywhere I see simply uploading a file and sav

相关标签:
6条回答
  • 2021-02-18 23:55

    If its text file (assuming size is not that huge) usually you could read it by

    $contents= file_get_contents($_FILES['file']['tmp_name']);
    

    If you are not sure which type of upload it was, check the request method

    if(strtolower($_SERVER['REQUEST_METHOD'])=='post')
        $contents= file_get_contents($_FILES['file']['tmp_name']);
    elseif(strtolower($_SERVER['REQUEST_METHOD'])=='put')
        $contents= file_get_contents("php://input");
    else
        $contents="";
    
    0 讨论(0)
  • 2021-02-18 23:55

    When the form is submitted, check the $_FILES['input_name']['tmp_name'] property. This is the path to your file saved in a temporary /tmp system path. You can proceed reading the name using, say, file_get_contents() and then simply forget the file. System will take care of removing it.


    Just to stand out of the other answers, you could theoretically read the file without even uploading it using JavaScript, see http://www.html5rocks.com/en/tutorials/file/dndfiles/. Then submit only the data you need as part of AJAX request.

    0 讨论(0)
  • 2021-02-18 23:55

    no it would have to be saved to be read afaik, my normally routine is the same as yours, upload, read and delete.

    0 讨论(0)
  • 2021-02-18 23:55

    You could just use $_FILES["file"]["tmp_name"] which would be the temp file that was uploaded. Once your script is done running, PHP should even clean up after itself and delete the temp file.

    0 讨论(0)
  • 2021-02-19 00:08

    The file has to be saved somewhere before reading in this case is the temporary directory; You can get the contents of the file from the temporary directory, then if you really need to you delete. See the following code:

    $file = file_get_contents($_FILES["ufiley"]["tmp_name"]);
    
    unlink($_FILES["ufiley"]["tmp_name"]); 
    
    //contents would be stored in $file
    
    0 讨论(0)
  • 2021-02-19 00:16

    When you upload a file, before you save it locally it get's saved to a temporary file. The location of which can be accessed by:

    $_FILES['uploadedfile']['tmp_name'] 
    

    You can choose not to save the file and fopen() the temporary file, as long as you do this within the same script that recieves the POST.

    0 讨论(0)
提交回复
热议问题