简体   繁体   中英

How to move Inbound Multipart form-data file from tmp directory

I am writing a webhook to receive inbound faxes. The inbound POST makes it to the server. The problem is retrieving the attachments to the post. My error log has this message.

 PHP Warning:  copy(/tmp/php4qL9du/pdf-sample.pdf): failed to open stream: No such file or directory in 

The array that is in the $_FILES is this

Array
(
  [attachment] => Array
    (
        [name] => pdf-sample.pdf
        [type] => application/pdf
        [tmp_name] => /tmp/phppw5aIU
        [error] => 0
        [size] => 7945
    )

)

I put the array into variables.

 $inboundFaxDocumentName = $_FILES['attachment']['name'];
 $inboundFaxLocation = $_FILES['attachment']['tmp_name'];
 $inboundFaxFilesize = $_FILES['attachment']['size'];
 $local = $inboundFaxLocation."/".$inboundFaxDocumentName;
 copy($local, "/var/www/html/new_home/");

I thought I would go ahead and show what I have for headers on this inbound webhook

header("Access-Control-Allow-Origin: *");
header("Content-Type: multipart/form-data; charset-UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorizations, X-Requested-With");

I have tried to manually go look for the file. I have not been able to find any file. I am not sure what else I can do to troubleshoot this situation. Any suggestions for troubleshooting would be greatly appreciated.

Try just

$local = $inboundFaxLocation;

$_FILES['attachment']['tmp_name'] should already contain a full path to the temporary file, there's no need to add the original name as well.

PS Normally in PHP people use move_uploaded_file() specifically for the purpose of transferring an uploaded file from its temporary uploaded location to a more permanent one - there's no point in copying really, because the temp file will be removed anyway after a period of time when the script ends.

Final code that worked for me.

 move_uploaded_file($_FILES['attachment']['tmp_name'], "/var/www/html/new_home/" . $_FILES['attachment']['name']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM