{"id":988,"date":"2019-02-24T13:44:40","date_gmt":"2019-02-24T13:44:40","guid":{"rendered":"https:\/\/www.copahost.com\/blog\/?p=988"},"modified":"2020-05-31T14:05:42","modified_gmt":"2020-05-31T14:05:42","slug":"php-upload-multiple-files","status":"publish","type":"post","link":"https:\/\/www.copahost.com\/blog\/php-upload-multiple-files\/","title":{"rendered":"PHP upload multiple files"},"content":{"rendered":"<p>In this example, we will show a script in PHP to upload multiple files at once. This PHP upload multiple files script works in any standard PHP based server.\u00a0 First of all, the HTML page will prompt you to select multiple files. Then, as you click Upload, the server will process the files one by one.<\/p>\n<p>First of all, make sure the directive <strong>file_uploads<\/strong> is enabled in your <a href=\"http:\/\/php.net\/manual\/en\/configuration.file.php\" target=\"_blank\" rel=\"noopener noreferrer\"><strong>php.ini<\/strong><\/a> file. This line must be set as follows:<\/p>\n<pre class=\"lang:default decode:true \">file_uploads = On<\/pre>\n<p>Basically, the only variable\u00a0you must change is $path, right at the beginning of this PHP script. The PHP upload multiple files script is almost ready to use. Note that you have to create the upload folder. Thus, if your <a href=\"https:\/\/www.copahost.com\/blog\/benchmark-linux-unixbench\/\" >server is Linux<\/a> based, you may need to set write permissions on it. These permissions can be configured by <a href=\"https:\/\/www.copahost.com\/blog\/ftp-online-cpanel-web-ftp-client\/\">FTP Online<\/a>, or else, using your <a href=\"https:\/\/www.copahost.com\/blog\/ftp-file-transfer\/\">FTP client<\/a>.<\/p>\n<h3>Basic PHP multiple files upload script<\/h3>\n<p><a href=\"https:\/\/www.copahost.com\/blog\/javascript-parsefloat\/\" >This first example<\/a> is a basic script. Here&#8217;s the example. We have created a script named upload.php with this content:<\/p>\n<pre class=\"lang:php decode:true\">&lt;?php\r\n\r\n\/\/ Change this to your destination folder\r\n\/\/ Make sure to set write permissions (777) in your server.\r\n$path = \"\/home\/mysample\/public_html\/upload\/files\/\";\r\n\r\n\/\/ If your server is Windows, use this line\r\n\/\/ $path = \"C:\\path\\to\\upload\\folder\";\r\n\r\nif (isset($_POST['submit'])) {\r\n    \r\n    \r\n    \r\n    foreach($_FILES['documents']['tmp_name'] as $key =&gt; $tmp_name)\r\n    {\r\n        \r\n      \/\/ echo \"===\".$key.$_FILES['documents']['name'][$key]; exit;\r\n        \r\n        $file_name = $key.$_FILES['documents']['name'][$key];\r\n        $file_size =$_FILES['documents']['size'][$key];\r\n        $file_tmp =$_FILES['documents']['tmp_name'][$key];\r\n        $file_type=$_FILES['documents']['type'][$key];  \r\n        move_uploaded_file($file_tmp,$path.time().$file_name);\r\n        \r\n        echo \"Processed $file_name &lt;br&gt;\";\r\n    }\r\n\r\n    echo \"&lt;br&gt;\";\r\n    echo \"Finished processing files\";\r\n    exit;\r\n}\r\n?&gt;\r\n\r\n&lt;form action=\"&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;\" method=\"post\" enctype=\"multipart\/form-data\"&gt;\r\n&lt;input name=\"documents[]\" type=\"file\" size=\"20\" multiple=\"multiple\" \/&gt;\r\n&lt;input name=\"submit\" type=\"submit\" value=\"Upload files\" \/&gt;\r\n&lt;\/form&gt;<\/pre>\n<h3>Explanation<\/h3>\n<p>For instance, in our example we have placed these files:<\/p>\n<p>\/home\/mysample\/public_html\/upload\/upload.php &#8211; The PHP multiple files upload script<br \/>\n\/home\/mysample\/public_html\/upload\/files\/\u00a0 &#8211; The folder that will receive the uploaded files<\/p>\n<h3>Advanced PHP multiple files upload script example<\/h3>\n<p>In this second example, one advanced PHP with some features. First, this advanced PHP example will include some\u00a0file verifications.<\/p>\n<p>This way,\u00a0<strong>if the file already exists in the server<\/strong>, it will show an error. Second,\u00a0it will<strong> check for the filesize<\/strong>. If it exceeds 500kb, will show an error. Then, it includes a<strong>\u00a0file extension verification<\/strong>. This way you can enable only specific file extensions to upload.<\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\r\n\r\n$path = \"\/home\/mysample\/public_html\/upload\/files\/\";\r\n\r\nif (isset($_POST['submit'])) {\r\n       \r\n    \r\n    foreach($_FILES['documents']['tmp_name'] as $key =&gt; $tmp_name)\r\n    {\r\n        \r\n      \/\/ echo \"===\".$key.$_FILES['documents']['name'][$key]; exit;\r\n        \r\n        $file_name = $key.$_FILES['documents']['name'][$key];\r\n        $file_size =$_FILES['documents']['size'][$key];\r\n        $file_tmp =$_FILES['documents']['tmp_name'][$key];\r\n        $file_type=$_FILES['documents']['type'][$key];  \r\n        $target_file = $path . basename($_FILES[\"documents\"][\"name\"][$key]);\r\n\r\n        \/\/ ADVANCED OPTIONS - Check if file already exists\r\n        if (file_exists($target_file)) {\r\n            echo \"Sorry, file already exists: $file_name&lt;br&gt;\";\r\n            continue;\r\n        }\r\n        \r\n        \/\/ ADVANCED OPTIONS- Check file size\r\n        if ($_FILES['documents']['size'][$key] &gt; 500000) {\r\n            echo \"Sorry, your file is too large: $file_name&lt;br&gt;\";\r\n            continue;\r\n        }\r\n        \r\n        \/\/ ADVANCED OPTIONS - Allow certain file formats\r\n        $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));\r\n        if($imageFileType != \"jpg\" &amp;&amp; $imageFileType != \"png\" &amp;&amp; $imageFileType != \"jpeg\"\r\n        &amp;&amp; $imageFileType != \"gif\" ) {\r\n            echo \"Sorry, only JPG, JPEG, PNG &amp; GIF files are allowed: $file_name&lt;br&gt;\";\r\n            continue;\r\n        }\r\n        \r\n        move_uploaded_file($file_tmp,$target_file);\r\n        \r\n        echo \"Processed $file_name &lt;br&gt;\";\r\n    }\r\n\r\n    \r\n    echo \"&lt;br&gt;Finished processing files\";\r\n    exit;\r\n}\r\n?&gt;\r\n\r\n&lt;form action=\"&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;\" method=\"post\" enctype=\"multipart\/form-data\"&gt;\r\n&lt;input name=\"documents[]\" type=\"file\" size=\"20\" multiple=\"multiple\" \/&gt;\r\n&lt;input name=\"submit\" type=\"submit\" value=\"Upload files\" \/&gt;\r\n&lt;\/form&gt;<\/pre>\n<p>&nbsp;<\/p>\n<h3>Conclusion<\/h3>\n<p>PHP is perfectly compatible with file uploads. Either single or multiple file uploads. There are some PHP upload multiple files scripts. Other verifications can be added to these files. For instance, file sizes allowed\u00a0extensions, etc.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this example, we will show a script in PHP to upload multiple files at once. This PHP upload multiple files script works in any standard PHP based server.\u00a0 First of all, the HTML page will prompt you to select multiple files. Then, as you click Upload, the server will process the files one by [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[133],"tags":[],"class_list":["post-988","post","type-post","status-publish","format-standard","hentry","category-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PHP upload multiple files - Copahost<\/title>\n<meta name=\"description\" content=\"Example of a working PHP upload multiple files script. Ready to use, and compatible with any PHP server, windows or linux.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.copahost.com\/blog\/php-upload-multiple-files\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP upload multiple files - Copahost\" \/>\n<meta property=\"og:description\" content=\"Example of a working PHP upload multiple files script. Ready to use, and compatible with any PHP server, windows or linux.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.copahost.com\/blog\/php-upload-multiple-files\/\" \/>\n<meta property=\"og:site_name\" content=\"Copahost\" \/>\n<meta property=\"article:published_time\" content=\"2019-02-24T13:44:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-05-31T14:05:42+00:00\" \/>\n<meta name=\"author\" content=\"Gustavo Gallas\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Gustavo Gallas\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.copahost.com\/blog\/php-upload-multiple-files\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/php-upload-multiple-files\/\"},\"author\":{\"name\":\"Gustavo Gallas\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#\/schema\/person\/386b3f1f79299d43f4ceb33d26428246\"},\"headline\":\"PHP upload multiple files\",\"datePublished\":\"2019-02-24T13:44:40+00:00\",\"dateModified\":\"2020-05-31T14:05:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/php-upload-multiple-files\/\"},\"wordCount\":333,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/#organization\"},\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.copahost.com\/blog\/php-upload-multiple-files\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.copahost.com\/blog\/php-upload-multiple-files\/\",\"url\":\"https:\/\/www.copahost.com\/blog\/php-upload-multiple-files\/\",\"name\":\"PHP upload multiple files - Copahost\",\"isPartOf\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/#website\"},\"datePublished\":\"2019-02-24T13:44:40+00:00\",\"dateModified\":\"2020-05-31T14:05:42+00:00\",\"description\":\"Example of a working PHP upload multiple files script. Ready to use, and compatible with any PHP server, windows or linux.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/php-upload-multiple-files\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.copahost.com\/blog\/php-upload-multiple-files\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.copahost.com\/blog\/php-upload-multiple-files\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.copahost.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP upload multiple files\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#website\",\"url\":\"https:\/\/www.copahost.com\/blog\/\",\"name\":\"Copahost\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.copahost.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#organization\",\"name\":\"Copahost\",\"url\":\"https:\/\/www.copahost.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2016\/03\/copahostlogo.png\",\"contentUrl\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2016\/03\/copahostlogo.png\",\"width\":223,\"height\":40,\"caption\":\"Copahost\"},\"image\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#\/schema\/person\/386b3f1f79299d43f4ceb33d26428246\",\"name\":\"Gustavo Gallas\",\"description\":\"Graduated in Computing at PUC-Rio, Brazil. Specialized in IT, networking, systems administration and human and organizational development\u200b. Also have brewing skills.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/gustavo-gallas-107926196\/\"],\"url\":\"https:\/\/www.copahost.com\/blog\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP upload multiple files - Copahost","description":"Example of a working PHP upload multiple files script. Ready to use, and compatible with any PHP server, windows or linux.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.copahost.com\/blog\/php-upload-multiple-files\/","og_locale":"en_US","og_type":"article","og_title":"PHP upload multiple files - Copahost","og_description":"Example of a working PHP upload multiple files script. Ready to use, and compatible with any PHP server, windows or linux.","og_url":"https:\/\/www.copahost.com\/blog\/php-upload-multiple-files\/","og_site_name":"Copahost","article_published_time":"2019-02-24T13:44:40+00:00","article_modified_time":"2020-05-31T14:05:42+00:00","author":"Gustavo Gallas","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Gustavo Gallas","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.copahost.com\/blog\/php-upload-multiple-files\/#article","isPartOf":{"@id":"https:\/\/www.copahost.com\/blog\/php-upload-multiple-files\/"},"author":{"name":"Gustavo Gallas","@id":"https:\/\/www.copahost.com\/blog\/#\/schema\/person\/386b3f1f79299d43f4ceb33d26428246"},"headline":"PHP upload multiple files","datePublished":"2019-02-24T13:44:40+00:00","dateModified":"2020-05-31T14:05:42+00:00","mainEntityOfPage":{"@id":"https:\/\/www.copahost.com\/blog\/php-upload-multiple-files\/"},"wordCount":333,"commentCount":0,"publisher":{"@id":"https:\/\/www.copahost.com\/blog\/#organization"},"articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.copahost.com\/blog\/php-upload-multiple-files\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.copahost.com\/blog\/php-upload-multiple-files\/","url":"https:\/\/www.copahost.com\/blog\/php-upload-multiple-files\/","name":"PHP upload multiple files - Copahost","isPartOf":{"@id":"https:\/\/www.copahost.com\/blog\/#website"},"datePublished":"2019-02-24T13:44:40+00:00","dateModified":"2020-05-31T14:05:42+00:00","description":"Example of a working PHP upload multiple files script. Ready to use, and compatible with any PHP server, windows or linux.","breadcrumb":{"@id":"https:\/\/www.copahost.com\/blog\/php-upload-multiple-files\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.copahost.com\/blog\/php-upload-multiple-files\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.copahost.com\/blog\/php-upload-multiple-files\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.copahost.com\/blog\/"},{"@type":"ListItem","position":2,"name":"PHP upload multiple files"}]},{"@type":"WebSite","@id":"https:\/\/www.copahost.com\/blog\/#website","url":"https:\/\/www.copahost.com\/blog\/","name":"Copahost","description":"","publisher":{"@id":"https:\/\/www.copahost.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.copahost.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.copahost.com\/blog\/#organization","name":"Copahost","url":"https:\/\/www.copahost.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.copahost.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2016\/03\/copahostlogo.png","contentUrl":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2016\/03\/copahostlogo.png","width":223,"height":40,"caption":"Copahost"},"image":{"@id":"https:\/\/www.copahost.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.copahost.com\/blog\/#\/schema\/person\/386b3f1f79299d43f4ceb33d26428246","name":"Gustavo Gallas","description":"Graduated in Computing at PUC-Rio, Brazil. Specialized in IT, networking, systems administration and human and organizational development\u200b. Also have brewing skills.","sameAs":["https:\/\/www.linkedin.com\/in\/gustavo-gallas-107926196\/"],"url":"https:\/\/www.copahost.com\/blog\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/posts\/988","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/comments?post=988"}],"version-history":[{"count":11,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/posts\/988\/revisions"}],"predecessor-version":[{"id":2460,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/posts\/988\/revisions\/2460"}],"wp:attachment":[{"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/media?parent=988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/categories?post=988"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/tags?post=988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}