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'].' ';
}
}
?>