{"id":3202,"date":"2014-03-20T02:40:32","date_gmt":"2014-03-20T02:40:32","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/20\/why-wont-my-phpmailer-script-work-to-get-save-files-collection-of-common-programming-errors\/"},"modified":"2014-03-20T02:40:32","modified_gmt":"2014-03-20T02:40:32","slug":"why-wont-my-phpmailer-script-work-to-get-save-files-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/20\/why-wont-my-phpmailer-script-work-to-get-save-files-collection-of-common-programming-errors\/","title":{"rendered":"Why wont my PHPmailer script work to get \/ save files?-Collection of common programming errors"},"content":{"rendered":"<p>First warning: You&#8217;re chmodding the <code>uploads<\/code> directory itself. Unless the directory is owned by your webserver&#8217;s user ID, you&#8217;ll get the permission denied error &#8211; you cannot chmod something that does not belong to you.<\/p>\n<p>Second and Third warning: Your array keys in the <code>$_FILES<\/code> array are incorrect. They should be<\/p>\n<pre><code>while(list($key,$value) = each($_FILES['images']['name']))\n<\/code><\/pre>\n<p>note the quotes &#8211; without the quotes, PHP assumes they&#8217;re constants that were created via <code>define()<\/code>. If there&#8217;s no constant by that name, PHP polite will treat them as strings of the same name, but issues the warning.<\/p>\n<p>Fourth warning: You&#8217;re misusing <code>each()<\/code>. Instead, just have:<\/p>\n<pre><code>foreach($_FILES['images']['name'] as $key =&gt; $value) {\n<\/code><\/pre>\n<p>Fifth warning: The only place you assign a value to <code>$success<\/code> is within the <code>if()<\/code> that does the image copy, when the file copy succeeds. Since your code is broken, the copy never takes place, so <code>$success<\/code> is never defined. To fix, put a<\/p>\n<pre><code>$success = false;\n<\/code><\/pre>\n<p>somewhere at the top of your file, so it&#8217;s defined with a default value.<\/p>\n<p>Beyond that, don&#8217;t use <code>copy()<\/code> on uploaded files. There&#8217;s security issues involved with PHP and file uploads on shared servers. Use <code>move_uploaded_file()<\/code> instead. This will be a far cheaper operation as well, since moving a file within a single filesystem is near instantaneous, while <code>copy()<\/code> actually duplicates the file &#8211; on large files this gets expensive (time + cpu + disk space) very quickly.<\/p>\n<p>comment followup:<\/p>\n<p>It does matter how you name the input fields, as that&#8217;ll determine how things show up in the files array.<\/p>\n<p>If you go with the <code>file1<\/code>, <code>file2<\/code>, etc&#8230; option, you end up with:<\/p>\n<pre><code>$_FILES = array(\n    'file1' =&gt; array('name' =&gt; ..., 'size' =&gt; ..., etc...),\n    'file2' =&gt; array('name' =&gt; ..., 'size' =&gt; ..., etc...),\n    etc...\n)\n<\/code><\/pre>\n<p>If you do <code>files[]<\/code>, you end up with:<\/p>\n<pre><code>$_FILES = array (\n   'files' =&gt; array\n        'name' =&gt; array(\n            0 = 'name of first file',\n            1 = 'name of second file',\n            ...\n        'size' =&gt; array(\n            0 = 'size of first file'\n            1 = 'name of second file'\n         etc...\n<\/code><\/pre>\n<p>A major structural difference. I have no idea why such a moronic difference was allowed to go into production, but since this is core PHP functionality, changing it to be more logically consistent is basically impossible.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>First warning: You&#8217;re chmodding the uploads directory itself. Unless the directory is owned by your webserver&#8217;s user ID, you&#8217;ll get the permission denied error &#8211; you cannot chmod something that does not belong to you. Second and Third warning: Your array keys in the $_FILES array are incorrect. They should be while(list($key,$value) = each($_FILES[&#8216;images&#8217;][&#8216;name&#8217;])) note [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3202","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3202","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3202"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3202\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}