Note that there are some explanatory texts on larger screens.

plurals
  1. POHaving a hard time with PHP arrays
    primarykey
    data
    text
    <p>I am trying to make a file upload script for my website but I can't figure out how to get the <code>name</code> and <code>size</code> values out my array (I am not very good with arrays).</p> <p>I can get it to work with uploading a single file, but how can I get it to loop?</p> <p>I need to upload it to my server, and it's also created an entry in the database.</p> <p>Here is my code...</p> <p>HTML:</p> <pre><code>&lt;form method="post" action="" enctype="multipart/form-data"&gt; &lt;textarea name="reply_message"&gt;&lt;/textarea&gt; &lt;input name="attachments[]" type="file" size="20"&gt; &lt;input type="submit" name="ReplyForm" value="Send"&gt; &lt;/form&gt; </code></pre> <p>PHP:</p> <pre><code>if(!empty($_POST['reply_message'])) { $reply_messages = $_POST['reply_message']; $database-&gt;query('INSERT INTO email_response (email_id, message) VALUES (?, ?)', array($email_id, $reply_message)); if(isset($_FILES['attachments'])) { require("upload.class.php"); $upload = new upload(); $last_email_response_id = $database-&gt;lastInsertId(); $attachments = $_FILES['attachments']; foreach($attachments as $key =&gt; $value) { print_r($attachments); $database-&gt;query('INSERT INTO email_attachments (email_id, reply_id, file_name, created) VALUES (?, ?, ?, NOW())', array($email_id, $last_email_response_id, $attachments['name'][0])); $last_attachment_id = $database-&gt;lastInsertId(); $upload-&gt;set('attachments', ATTACHMENTS.$reply_result-&gt;sender_email.'/'.$email_id.'/'.$last_attachment_id); $upload-&gt;upload(); } } } </code></pre> <p>Array (with uploading two files):</p> <pre><code>Array ( [name] =&gt; Array ( [0] =&gt; linkdownarrow.gif [1] =&gt; overlay-button.png ) [type] =&gt; Array ( [0] =&gt; image/gif [1] =&gt; image/png ) [tmp_name] =&gt; Array ( [0] =&gt; F:\xampp\tmp\phpFEC0.tmp [1] =&gt; F:\xampp\tmp\phpFEC1.tmp ) [error] =&gt; Array ( [0] =&gt; 0 [1] =&gt; 0 ) [size] =&gt; Array ( [0] =&gt; 63 [1] =&gt; 135 ) ) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload