Showing posts with label PHP unlink. Show all posts
Showing posts with label PHP unlink. Show all posts

Monday, 20 July 2015

PHP: Delete image from folder

Some programmer using image upload coding that image file move on particular folder. after delete  that database record data only deleteed but image stay on same folder only it's not delete. So add below code remove deleted record folder image. 

unlink('foldername/'.$imagename);

Sample code
$Q2="SELECT * FROM image WHERE id='".$id."'";
$row2 = dbRow($Q2);
$image_path=$row2['image'];
if(file_exists('imagesfolder/'.$image_path))unlink('imagesfolder/'.$image_path);

Friday, 26 June 2015

PHP: Delete Uploaded Files From Folder using PHP and MySQL

This is my continuation post of how to upload and view files , In this post we will see that how to completely delete(remove) uploaded files from folder and MySql database table using PHP , you can delete any files pdf, mp3, image, video any...,when you upload any file it moves on particular folder, after delete  that database record data only deleted but files stay on same folder only it's not delete. So how to do let's see.

Here i am going to use same old Database crediantials which are used in my file uploading tutorial.
using unlink() function you can delete files from folder.
example :
unlink("folder_name/".file_name);

Sample Code :
$res=mysql_query("SELECT file FROM tbl_uploads WHERE id=".$_GET['remove_id']);
$row=mysql_fetch_array($res);
mysql_query("DELETE FROM tbl_uploads WHERE id=".$_GET['remove_id']);
unlink("uploads/".$row['file']);

Script explained :
- First write select query to fetch file from database.
- This code can be work with if(isset($_GET[]) condition.
- After fetching file put the fetched file into the unlink() function like above.

Wednesday, 3 June 2015

PHP: Unzip a Zip File

 This code allows you to Unzip a ZIP file with PHP.
<?php   
function unzip($location,$new_location){
    if(exec("unzip $location",$arr)){
        mkdir($new_location);
        for($i = 1;$i< count($arr);$i++){
            $file = trim(preg_replace("~inflating: ~","",$arr[$i]));
                    copy($location."/".$file,$new_location."/".$file);
                    unlink($location."/".$file);
            }
        return true;
    }
    return false;      
}
 
// usage of this code
if(unzip('ziped_files/test.zip','unziped_files/newfile')){
    echo 'Successfully unzipped!';
}else{
    echo 'Error while processing your file!';
 
}
?>

Saturday, 4 October 2014

unlink in PHP

unlink — Deletes a file
Syntax:

bool unlink ( string $filename [, resource $context ] )
Deletes filename. Similar to the Unix C unlink() function. A E_WARNING level error will be generated on failure.

Parameters:

filename
Path to the file.

context
Note: Context support was added with PHP 5.0.0. For a description of contexts, refer to Streams.
Return values: Returns TRUE on success or FALSE on failure.

Changelog ¶

Version Description
5.0.0 As of PHP 5.0.0 unlink() can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers for a listing of which wrappers support unlink().


Example #1 Basic unlink() usage

<?php
$fh = fopen('test.html', 'a');
fwrite($fh, '<h1>Hello world!</h1>');
fclose($fh);

unlink('test.html');
?>