{"id":2240,"date":"2013-02-08T06:57:42","date_gmt":"2013-02-08T06:57:42","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=2240"},"modified":"2013-02-08T06:57:42","modified_gmt":"2013-02-08T06:57:42","slug":"php-delete-file-or-remove-file","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/php-delete-file-or-remove-file\/","title":{"rendered":"How To Delete File Or Remove File Using PHP Examples"},"content":{"rendered":"<p>When working with files in PHP, there may be a\u00a0necessity\u00a0to create files dynamically and at the same time, <em><strong>remove file or delete file using PHP<\/strong><\/em>. \u00a0In this article, I am going to share a very easy way to delete a file using PHP. The actual code is just 1 line, so it&#8217;s super easy to follow.<!--more--><\/p>\n<h2>Examples On How To Remove File Or Delete File Using PHP<\/h2>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=2240\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p>To do this, we simply use the &#8220;unlink&#8221; function. So what unlink simply does is it <strong>&#8220;Deletes a file&#8221;<\/strong>. That&#8217;s it! Nothing more!<\/p>\n<p><strong>Command:<\/strong><\/p>\n<pre class=\"lang:php decode:true\" title=\"Example\">unlink( $filename );<\/pre>\n<h3>Example: Using PHP to delete a file with the help of unlink function<\/h3>\n<p>In this example, I am going to first create a sample test file and then once it is created in the following steps, I will delete the file using PHP.<\/p>\n<pre class=\"lang:php decode:true\" title=\"Example\">&lt;?php\n$temp_file_name = tempnam ('tmp\/', \"Test\"); \n\nif( $temp_file_name )\n{\n\techo 'File successfully CREATED.&lt;br \/&gt;';\t\n}\nelse\n{\n\techo 'Unable to CREATE file.&lt;br \/&gt;';\t\t\n}\n\n$fp = fopen($temp_file_name, \"w\"); \nfwrite($fp, 'Some sample content'); \nfclose($fp); \t\t\t\t\n\n$delete_result = unlink($temp_file_name); \n\nif( $delete_result )\n{\n\techo 'File successfully DELETED.&lt;br \/&gt;';\t\n}\nelse\n{\n\techo 'Unable to DELETE the file.&lt;br \/&gt;';\t\t\n}\n?&gt;<\/pre>\n<p>If you have tried the demo, you will notice that first, the file is created &amp; in the succeeding step, the file is deleted. If you have not yet tried the demo, then do it by clicking the button below.<\/p>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=2240\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p><strong>Your Turn!<\/strong><br \/>\nDo you know of any other ways of using\u00a0<em><strong>PHP to delete file or remove a file<\/strong><\/em>? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When working with files in PHP, there may be a\u00a0necessity\u00a0to create files dynamically and at the same time, remove file or delete file using PHP. \u00a0In this article, I am going to share a very easy way to delete a file using PHP. The actual code is just 1 line, so it&#8217;s super easy to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":141,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_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":""},"categories":[10,13],"tags":[125,33],"class_list":["post-2240","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-tips-and-tricks","category-tips-and-tricks","tag-php-demos-2","tag-php-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-A8","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/2240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=2240"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/2240\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=2240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=2240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=2240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}