0

I am working on my PHP to fetch the data from the mysql database. I have got a problem with storing the filename in the array using the values when I have stored both filenames in a database.

Here is what I have stored in the blob:

draft_attachment.rar
draft_attachment - Copy.rar

Here is the code:

$mailbox = $link->prepare("SELECT * FROM draft WHERE id = ?");
$mailbox->execute([$id]);

// set the resulting array to associative
$row = $mailbox->fetch(PDO::FETCH_ASSOC);
$attachments = array();

if($mailbox->rowCount() == 1)
{
    $attachment = array();
    $draft_to = htmlspecialchars($row['to_email']);
    $draft_subject = quoted_printable_decode($row['subject']);
    $draft_message = $row['message'];
    $attach[] = $row['attachments'];
}
?>

When I try this:

$attach[] = $row['attachments'];

It will store both strings in the array:

Array
{
   [0] => draft_attachment.rar
draft_attachment - Copy.rar
}

So I have tried this:

$attachments = array();  
$i = 0;

if($mailbox->rowCount() == 1) {
    $attachments[$i] = $row['attachments'];
    $i++;
}

It wont store the strings separate so I dont know what to do.

Here is what I want to achieve:

Array
{
   [0] => draft_attachment.rar
   [1] => draft_attachment - Copy.rar
}

Can you please show me an example how I could get both strings to go separate so I could be able to store them in the array using the value?

Thank you.

2
  • 1
    Are you actually storing any binary data in that blob? If not, you need a better approach. If you were to use a lookup table (varchar) that would provide the one => many relationships you appear to need. Then no need to separate any strings. Commented Sep 23, 2019 at 23:17
  • "'s are valid chars in an email address, htmlspecialchars is going to bork it, XSS is not a thing with to address, instead you should be preventing header injection, which htmlspecialchars wont prevent. Commented Sep 23, 2019 at 23:28

2 Answers 2

1

You can split the string using preg_split on \R (any sequence of new line characters) (explode can also work if you know the exact line ending characters):

$attachments = preg_split('/\R/', $row['attachments']);

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for this as this is what I am looking for exactly.
0

you can use explode function, something like explode("\n", $row['attachments'])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.