{"id":124,"date":"2011-07-01T13:22:48","date_gmt":"2011-07-01T18:22:48","guid":{"rendered":"http:\/\/kurtzenisek.com\/articles\/?p=124"},"modified":"2011-07-01T13:22:48","modified_gmt":"2011-07-01T18:22:48","slug":"php-prevent-uploads-from-being-overwitten","status":"publish","type":"post","link":"https:\/\/kurtzenisek.com\/articles\/php-prevent-uploads-from-being-overwitten\/","title":{"rendered":"PHP: Prevent uploads from being overwitten"},"content":{"rendered":"<p>The following PHP file upload script makes the filename server-friendly, checks for filesize limitations, makes the filename unique so it doesn&#8217;t overwrite existing files (while using a sane naming scheme rather than simply using random generation &amp; hoping for the best, lol), and outputs the final filename\/location when finished.<\/p>\n<p><code> $file = basename($_FILES['file']['name']); \/\/ Get the uploaded file's filename (input's name being set to \"file\")<br \/>\n$file = utf8_decode($file); \/\/ Convert to utf8<br \/>\n$file = strtr($file, utf8_decode(\"\\'\u00c0\u00c1\u00c2\u00c3\u00c4\u00c5\u00c7\u00c8\u00c9\u00ca\u00cb\u00cc\u00cd\u00ce\u00cf\u00d2\u00d3\u00d4\u00d5\u00d6\u00d9\u00da\u00db\u00dc\u00dd\u00d1\u00f1\u00e0\u00e1\u00e2\u00e3\u00e4\u00e5\u00e7\u00e8\u00e9\u00ea\u00eb\u00ec\u00ed\u00ee\u00ef\u00f0\u00f2\u00f3\u00f4\u00f5\u00f6\u00f9\u00fa\u00fb\u00fc\u00fd\u00ff\"), \"--AAAAAACEEEEIIIIOOOOOUUUUYNnaaaaaaceeeeiiiioooooouuuuyy\"); \/\/ Replace special characters<br \/>\n$file = preg_replace('\/( +)\/i', '_', $file); \/\/ Replace spaces<br \/>\n$file = strtolower($file); \/\/ Make lowercase<br \/>\n$original_file = $file; \/\/ Save this as a reference<\/code><\/p>\n<p><code>$size = $_FILES['file']['size']; \/\/ Get filesize<br \/>\nif($size&gt;52428800){ \/\/ Check if filesize is greater than a specified amount<br \/>\necho \"error file size &gt; 50 MB\"; \/\/ Output error message<br \/>\nunlink($_FILES['file']['tmp_name']); \/\/ Clean up upload<br \/>\nexit; \/\/ Don't bother to run any more code<br \/>\n}<\/p>\n<p>$file_i = 1; \/\/ Prep the number that'll be appended when there's an existing file<br \/>\nfunction prevent_overwrite(){<br \/>\nglobal $file_i,$file,$original_file; \/\/ Pull in our external variables<br \/>\nif(file_exists($file)){ \/\/ Check for existing file<br \/>\n$parts = explode('.',$file); \/\/ Blow apart<br \/>\n$parts[0] = $parts[0].$file_i; \/\/ Add incremental number after filename<br \/>\n$file = $parts[0].'.'.$parts[1]; \/\/ Join filename &amp; extension back together<br \/>\nif(file_exists($file)){ \/\/ If it still has an existing counterpart<br \/>\n$file_i++; \/\/ Increase the number<br \/>\n$file = $original_file; \/\/ Revert back to the filename w\/o the number (so they don't stack up so it ends up being \"filename6.jpg\" instead of \"filename123456.jpg\")<br \/>\nprevent_overwrite(); \/\/ Run it again<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\nprevent_overwrite(); \/\/ Run this to prep to filename to account for the files already on the server<\/p>\n<p><\/code><\/p>\n<p><code>if(move_uploaded_file($_FILES['file']['tmp_name'], $file)){ \/\/ Try to upload the file<br \/>\necho $file; \/\/ If successful, output the file's final location &amp; filename<br \/>\n}else{<br \/>\necho \"error \".$_FILES['file']['error'].\" --- \".$_FILES['file']['tmp_name'].\" %%% \".$file.\"($_FILES['file']['size'])\"; \/\/ Provide an error message<br \/>\n}<br \/>\n<\/code><\/p>\n<p>Hopefully somebody out there finds this useful. I provided more than what&#8217;s necessary for simply preventing uploads from being overwritten, but you can take the parts that you need and possibly take some stuff that you find useful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The following PHP file upload script makes the filename server-friendly, checks for filesize limitations, makes the filename unique so it doesn&#8217;t overwrite existing files (while using a sane naming scheme rather than simply using random generation &amp; hoping for the best, lol), and outputs the final filename\/location when finished. $file = basename($_FILES[&#8216;file&#8217;][&#8216;name&#8217;]); \/\/ Get the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"episode_type":"","audio_file":"","podmotor_file_id":"","podmotor_episode_id":"","cover_image":"","cover_image_id":"","duration":"","filesize":"","filesize_raw":"","date_recorded":"","explicit":"","block":"","itunes_episode_number":"","itunes_title":"","itunes_season_number":"","itunes_episode_type":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-124","post","type-post","status-publish","format-standard","hentry","category-news"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PHP: Prevent uploads from being overwitten - Kurt Zenisek<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/kurtzenisek.com\/articles\/php-prevent-uploads-from-being-overwitten\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP: Prevent uploads from being overwitten - Kurt Zenisek\" \/>\n<meta property=\"og:description\" content=\"The following PHP file upload script makes the filename server-friendly, checks for filesize limitations, makes the filename unique so it doesn&#8217;t overwrite existing files (while using a sane naming scheme rather than simply using random generation &amp; hoping for the best, lol), and outputs the final filename\/location when finished. $file = basename($_FILES[&#039;file&#039;][&#039;name&#039;]); \/\/ Get the [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kurtzenisek.com\/articles\/php-prevent-uploads-from-being-overwitten\/\" \/>\n<meta property=\"og:site_name\" content=\"Kurt Zenisek\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/kzeni\" \/>\n<meta property=\"article:published_time\" content=\"2011-07-01T18:22:48+00:00\" \/>\n<meta name=\"author\" content=\"Kurt\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kurt\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/php-prevent-uploads-from-being-overwitten\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/php-prevent-uploads-from-being-overwitten\\\/\"},\"author\":{\"name\":\"Kurt\",\"@id\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/#\\\/schema\\\/person\\\/f3528f54a8466c58edb0533fcb1c2ec9\"},\"headline\":\"PHP: Prevent uploads from being overwitten\",\"datePublished\":\"2011-07-01T18:22:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/php-prevent-uploads-from-being-overwitten\\\/\"},\"wordCount\":96,\"commentCount\":0,\"articleSection\":[\"News\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/php-prevent-uploads-from-being-overwitten\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/php-prevent-uploads-from-being-overwitten\\\/\",\"url\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/php-prevent-uploads-from-being-overwitten\\\/\",\"name\":\"PHP: Prevent uploads from being overwitten - Kurt Zenisek\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/#website\"},\"datePublished\":\"2011-07-01T18:22:48+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/#\\\/schema\\\/person\\\/f3528f54a8466c58edb0533fcb1c2ec9\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/php-prevent-uploads-from-being-overwitten\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/php-prevent-uploads-from-being-overwitten\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/php-prevent-uploads-from-being-overwitten\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP: Prevent uploads from being overwitten\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/#website\",\"url\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/\",\"name\":\"Kurt Zenisek\",\"description\":\"Assorted Code Snippets &amp; Articles\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/#\\\/schema\\\/person\\\/f3528f54a8466c58edb0533fcb1c2ec9\",\"name\":\"Kurt\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d243c95df571db785649246aa036b5c16c30e9dc0dbcf2fc24297b1a5d6eebd8?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d243c95df571db785649246aa036b5c16c30e9dc0dbcf2fc24297b1a5d6eebd8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d243c95df571db785649246aa036b5c16c30e9dc0dbcf2fc24297b1a5d6eebd8?s=96&d=mm&r=g\",\"caption\":\"Kurt\"},\"sameAs\":[\"http:\\\/\\\/kzeni.com\",\"https:\\\/\\\/www.facebook.com\\\/kzeni\",\"https:\\\/\\\/x.com\\\/KZeni\"],\"url\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/author\\\/kzeni\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP: Prevent uploads from being overwitten - Kurt Zenisek","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:\/\/kurtzenisek.com\/articles\/php-prevent-uploads-from-being-overwitten\/","og_locale":"en_US","og_type":"article","og_title":"PHP: Prevent uploads from being overwitten - Kurt Zenisek","og_description":"The following PHP file upload script makes the filename server-friendly, checks for filesize limitations, makes the filename unique so it doesn&#8217;t overwrite existing files (while using a sane naming scheme rather than simply using random generation &amp; hoping for the best, lol), and outputs the final filename\/location when finished. $file = basename($_FILES['file']['name']); \/\/ Get the [&hellip;]","og_url":"https:\/\/kurtzenisek.com\/articles\/php-prevent-uploads-from-being-overwitten\/","og_site_name":"Kurt Zenisek","article_author":"https:\/\/www.facebook.com\/kzeni","article_published_time":"2011-07-01T18:22:48+00:00","author":"Kurt","twitter_misc":{"Written by":"Kurt","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/kurtzenisek.com\/articles\/php-prevent-uploads-from-being-overwitten\/#article","isPartOf":{"@id":"https:\/\/kurtzenisek.com\/articles\/php-prevent-uploads-from-being-overwitten\/"},"author":{"name":"Kurt","@id":"https:\/\/kurtzenisek.com\/articles\/#\/schema\/person\/f3528f54a8466c58edb0533fcb1c2ec9"},"headline":"PHP: Prevent uploads from being overwitten","datePublished":"2011-07-01T18:22:48+00:00","mainEntityOfPage":{"@id":"https:\/\/kurtzenisek.com\/articles\/php-prevent-uploads-from-being-overwitten\/"},"wordCount":96,"commentCount":0,"articleSection":["News"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/kurtzenisek.com\/articles\/php-prevent-uploads-from-being-overwitten\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/kurtzenisek.com\/articles\/php-prevent-uploads-from-being-overwitten\/","url":"https:\/\/kurtzenisek.com\/articles\/php-prevent-uploads-from-being-overwitten\/","name":"PHP: Prevent uploads from being overwitten - Kurt Zenisek","isPartOf":{"@id":"https:\/\/kurtzenisek.com\/articles\/#website"},"datePublished":"2011-07-01T18:22:48+00:00","author":{"@id":"https:\/\/kurtzenisek.com\/articles\/#\/schema\/person\/f3528f54a8466c58edb0533fcb1c2ec9"},"breadcrumb":{"@id":"https:\/\/kurtzenisek.com\/articles\/php-prevent-uploads-from-being-overwitten\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kurtzenisek.com\/articles\/php-prevent-uploads-from-being-overwitten\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/kurtzenisek.com\/articles\/php-prevent-uploads-from-being-overwitten\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/kurtzenisek.com\/articles\/"},{"@type":"ListItem","position":2,"name":"PHP: Prevent uploads from being overwitten"}]},{"@type":"WebSite","@id":"https:\/\/kurtzenisek.com\/articles\/#website","url":"https:\/\/kurtzenisek.com\/articles\/","name":"Kurt Zenisek","description":"Assorted Code Snippets &amp; Articles","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/kurtzenisek.com\/articles\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/kurtzenisek.com\/articles\/#\/schema\/person\/f3528f54a8466c58edb0533fcb1c2ec9","name":"Kurt","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d243c95df571db785649246aa036b5c16c30e9dc0dbcf2fc24297b1a5d6eebd8?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d243c95df571db785649246aa036b5c16c30e9dc0dbcf2fc24297b1a5d6eebd8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d243c95df571db785649246aa036b5c16c30e9dc0dbcf2fc24297b1a5d6eebd8?s=96&d=mm&r=g","caption":"Kurt"},"sameAs":["http:\/\/kzeni.com","https:\/\/www.facebook.com\/kzeni","https:\/\/x.com\/KZeni"],"url":"https:\/\/kurtzenisek.com\/articles\/author\/kzeni\/"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/p2Xgc6-20","_links":{"self":[{"href":"https:\/\/kurtzenisek.com\/articles\/wp-json\/wp\/v2\/posts\/124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kurtzenisek.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kurtzenisek.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kurtzenisek.com\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kurtzenisek.com\/articles\/wp-json\/wp\/v2\/comments?post=124"}],"version-history":[{"count":2,"href":"https:\/\/kurtzenisek.com\/articles\/wp-json\/wp\/v2\/posts\/124\/revisions"}],"predecessor-version":[{"id":127,"href":"https:\/\/kurtzenisek.com\/articles\/wp-json\/wp\/v2\/posts\/124\/revisions\/127"}],"wp:attachment":[{"href":"https:\/\/kurtzenisek.com\/articles\/wp-json\/wp\/v2\/media?parent=124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kurtzenisek.com\/articles\/wp-json\/wp\/v2\/categories?post=124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kurtzenisek.com\/articles\/wp-json\/wp\/v2\/tags?post=124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}