dowaw80220 2019-04-18 20:06
浏览 50

PHP:is_uploaded_file似乎只知道每次重启应用程序时如何处理1个文件?

In C# I have code that uploads a file Async to a php file, then in the PHP file I assign a unique ID (given by the C# code), then store the file in a database. This can run multiple times BUT it only seems to be working with the very first file that gets uploaded, and not the subsequent.

Maybe (is_uploaded_file($_FILES['file']['tmp_name']) only knows how to reference the VERY FIRST file it receives and doesn't know to clear it from memory? So the other files it receives shortly later is called (is_uploaded_file($FILES['file2']['tmp_name'] or something. Just a shot in the dark.

I'm not going to post a bunch of code because my code is very simple and is not a problem with it as far as I am aware, this is the only reason this error might be happening as far as I know.

EDIT: Fine, its not that simple but here is code:

C#

 foreach (MailItem email in Globals.ThisAddIn.Application.ActiveExplorer().Selection)
            {
                string email_id = ArchiveEmail(email);

                if (chkBoxSyncEmail.Checked)
                {                                        
                    foreach (Attachment atch in email.Attachments)
                    {                        
                        try
                        {
                            if(AttachmentsBeingSent.Contains(atch.FileName))
                            {
                                //Sending attachment.
                                string filePath = Path.Combine(@"", (email_id + atch.FileName)); //We add the unique email_id to the beginning of the filename, it is always 13 chars long so we can pull the unique id off later with ease.
                                atch.SaveAsFile(filePath); //Save as file
                                WebClient wc = new WebClient();
                                await wc.UploadFileTaskAsync(new Uri("https://xxxx.com/xxxx/xxxx/xxxx/upload_attachment.php"), "POST", filePath);                             
                                MessageBox.Show(atch.FileName + " Uploaded successfully");

                                if (File.Exists(filePath))
                                {
                                    File.Delete(filePath);
                                }
                            }                        
                        }

PHP



<?php
include('../include/xxx.php');



if (is_uploaded_file($_FILES['file']['tmp_name']))
{
    global $xxx;

    $uploaddir = 'xxxxxxxx/'; // Relative Upload Location of data file, if its empty it should place in this location.
    $given_email_ID = substr(($_FILES['file']['name']),0,13); //Take first 13 chars off from beginning and that is our email_ID.
    $fileName = substr(($_FILES['file']['name']),13); //This should be our filename and extension intact. Still need to add a unique_ID to this one so it doesn't get overwritten in file system.
    $unique_ID = uniqid();
    $newName = $unique_ID.$fileName;

    $download_loc = ($uploaddir.$NewName);




    if (move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir.$newName))
    {

        $xxx->query("
         INSERT INTO attachments(
           email_id,
           file_name,
           download_loc) VALUES (
            '" . $given_email_ID . "',
            '" . $newName . "',
            '" . $download_loc . "'
            );
          ");
    }
}

QUERY (in PHP)This only gives me one result! I should have at least multiple. Only one thing gets entered in the table back in the other PHP script.

<?php
                    $attachments = $xxxx->query("
                        SELECT *
                        FROM attachments
                    ");
                    if($attachments->num_rows > 0) {
                        while ($row = $attachments->fetch_assoc()) {                        
                            echo $row['email_id'].'  ';
                        }
                    }

                    ?>

  • 写回答

2条回答 默认 最新

  • duanji2002 2019-04-19 04:42
    关注

    You've to make two small changes in your code:

    • the name in the input-field of the form has to be an array to take several file and it has to be noted / announced that upload of several files is possible:

      <input type="file" name="attachements[]" multiple="multiple" id="attachements" />
      

      The array of files is built when the square brackets are used [] after the field-name.

      multiple can be noted in several versions, probably it's enough just to use the word inside the html for the input-field without any assignment, the notation I used above comes from the old xml-syntax, you also can leave the assignment empty or use "true" as value.

    • The php-code for handling a single file has to be moved into a loop to handle all files. Before changing the form you won't be able to see more than one file in the incoming array $_FILES. According to the input-field above all files will be available in the variable $_FILES['attachements'].

    • Furthermore you've to adjust the form-element perhaps. If you handle the upload by the same site respectively file where the form is located you can just note it like this and leavinng action empty:

      <form action="" enctype="multipart/form-data" method="post">
      

      If you handle the upload on a different site just enter the filename in the action like this:

      <form action="url/to/file/handle-upload.php" enctype="multipart/form-data" method="post">
      

    Here you find an approach to realize it with ajax: PHP Handling file uploads

    Here you find a video with a solution that is building a few small features around the whole approach.

    Here you find the input-field with type file in the html5-standard.

    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来