Php : How To Use Scandir To Show Files Names

Php Code : How To Show Filenames Using Scandir

________________________________________________________


In this Php Tutorial we will Learn How To Use Scandir To Display Files Names From Folder Using Php .

I Use In This Tutorial:
- NetBeans IDE .
- XAMPP .



 




Php Source Code:

<?php

$dir_path = "folder/";
if(is_dir($dir_path))
{
    $files = scandir($dir_path);
    print_r($files);
    echo"<br>";
    for($i = 0; $i < count($files); $i++)
    {
        if($files[$i] != '.' && $files[$i] != '..')
        {
            echo "File $i -> $files[$i]<br>";
        }
    }
}




                     
php using scandir




Php : How To Delete File From Directory Using Php

Php Code To Remve File From A Folder Using Php 


________________________________________________________


In this Php Tutorial we will see How To Delete File From A Folder Using Php .

I Use In This Tutorial:
- NetBeans IDE .
- XAMPP .

 




Php Source Code:

<?php

if(isset($_POST['remve_file']))
{
    $file_Path = $_POST['fileToRemove'];
    
    // check if the file exist
    if(file_exists($file_Path))
    {
        unlink($file_Path);
        echo 'File Deleted';
    }else{
        echo 'File Not Exist';
    }
}

?>


<!DOCTYPE html>
<html>
    <head>
        <title>PHP DELETE FILE FROM FOLDER</title>
    </head>
    
    <body>
        
        <form action="php_delete_file_from_folder.php" method="post">
            
            File Path: <input type="text" name="fileToRemove"><br><br>
            
            <input type="submit" name="remve_file" value="Delete File">
            
        </form>
        
    </body>
    
</html>


php delete file


                     




Php How To Move Uploaded File To Another Folder

Php Code To Move Uploaded File To Another Directory


________________________________________________________


In this Php Tutorial we will see How To Move Uploded File To Another Folder Using Php .

I Use In This Tutorial:
- NetBeans IDE .
- XAMPP .


 




Php Source Code:

<?php

if(isset($_POST['moveFile']))
{
    $fileName = $_FILES['fileName']['name'];
    $tempName = $_FILES['fileName']['tmp_name'];
    
    if(isset($fileName))
    {
        if(!empty($fileName))
        {
            $location = "MyFilles/";
            if(move_uploaded_file($tempName, $location.$fileName))
            {
                echo 'File Uploaded';
            }
        }
    }
}

?>

<!DOCTYPE Html>
<html>
    <head>
        <title> PHP MOVE FILE </title>
    </head>
    <body>
    
        <form action="php_move_upload_file.php" method="post" enctype="multipart/form-data">
            <input type="file" name="fileName"><br><br>
            <input type="submit" name="moveFile" value="Upload">
        </form>
        
    </body>
</html>




php move file
                     




Php : How To Search And Filter Data In Html Table Using Php And MySQL

Php How To Filter Data In Html Table Using Php And MySQL


________________________________________________________


In this Php Tutorial we will see How To Use Search Data In Html Table Using MySQL Database And Php .

I Use In This Tutorial:
- NetBeans IDE .
- XAMPP .
- PhpMyAdmin .


 




Php Source Code:

<?php

if(isset($_POST['search']))
{
    $valueToSearch = $_POST['valueToSearch'];
    // search in all table columns
    // using concat mysql function
    $query = "SELECT * FROM `users` WHERE CONCAT(`id`, `fname`, `lname`, `age`) LIKE '%".$valueToSearch."%'";
    $search_result = filterTable($query);
    
}
 else {
    $query = "SELECT * FROM `users`";
    $search_result = filterTable($query);
}

// function to connect and execute the query
function filterTable($query)
{
    $connect = mysqli_connect("localhost", "root", "", "test_db");
    $filter_Result = mysqli_query($connect, $query);
    return $filter_Result;
}

?>

<!DOCTYPE html>
<html>
    <head>
        <title>PHP HTML TABLE DATA SEARCH</title>
        <style>
            table,tr,th,td
            {
                border: 1px solid black;
            }
        </style>
    </head>
    <body>
        
        <form action="php_html_table_data_filter.php" method="post">
            <input type="text" name="valueToSearch" placeholder="Value To Search"><br><br>
            <input type="submit" name="search" value="Filter"><br><br>
            
            <table>
                <tr>
                    <th>Id</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Age</th>
                </tr>

                <?php while($row = mysqli_fetch_array($search_result)):?>
                <tr>
                    <td><?php echo $row['id'];?></td>
                    <td><?php echo $row['fname'];?></td>
                    <td><?php echo $row['lname'];?></td>
                    <td><?php echo $row['age'];?></td>
                </tr>
                <?php endwhile;?>
            </table>
        </form>
        
    </body>
</html>



///////////////OUTPUT: 

php html table find
Search For W




                     




Php How To Get List Of All Filenames From A Folder

Php How To Get All Files Names From A Folder


________________________________________________________


In this Php Tutorial we will see How To Retrieve All Files Names In A Directory Using Php .

I Use In This Tutorial:
- NetBeans IDE .
- XAMPP .



 




Php Source Code:

<?php

$dir_path = "folder/";
$options = "";
if(is_dir($dir_path))
{
    $files = opendir($dir_path);
    {
        if($files)
        {
          while(($file_name = readdir($files)) !== FALSE)
          {
              if($file_name != '.' && $file_name != '..')
              {
               // select option with files names
               $options = $options."<option>$file_name</option>";   
               
               // display the file names
               echo $file_name."<br>";
              }
          }
        }
}
}

?>


<!DOCTYPE html>
<html>
    <head>
        
    <title>PHP GET List Of FILES NAMES FROM A Directory </title>
    </head>
    
    <body>
        
        <select>
            <?php echo $options;?>
        </select>
        
    </body>
</html>


php files names




                     




Php: Insert Update Delete Search In MySQL Database Using Php

Php Add , Edit , Remove , Find In MySQL Database Using Php With Source Code


________________________________________________________


In this Php Tutorial we will see How To Insert Update Delete Search Data In MySQL Database Table From html From With Php .

I Use In This Tutorial:
- NetBeans IDE .
- XAMPP .
- PhpMyAdmin .



Part 1


Part 2



Php Source Code:

<?php

$host = "localhost";
$user = "root";
$password ="";
$database = "test_db";

$id = "";
$fname = "";
$lname = "";
$age = "";

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

// connect to mysql database
try{
    $connect = mysqli_connect($host, $user, $password, $database);
} catch (mysqli_sql_exception $ex) {
    echo 'Error';
}

// get values from the form
function getPosts()
{
    $posts = array();
    $posts[0] = $_POST['id'];
    $posts[1] = $_POST['fname'];
    $posts[2] = $_POST['lname'];
    $posts[3] = $_POST['age'];
    return $posts;
}

// Search

if(isset($_POST['search']))
{
    $data = getPosts();
    
    $search_Query = "SELECT * FROM users WHERE id = $data[0]";
    
    $search_Result = mysqli_query($connect, $search_Query);
    
    if($search_Result)
    {
        if(mysqli_num_rows($search_Result))
        {
            while($row = mysqli_fetch_array($search_Result))
            {
                $id = $row['id'];
                $fname = $row['fname'];
                $lname = $row['lname'];
                $age = $row['age'];
            }
        }else{
            echo 'No Data For This Id';
        }
    }else{
        echo 'Result Error';
    }
}


// Insert
if(isset($_POST['insert']))
{
    $data = getPosts();
    $insert_Query = "INSERT INTO `users`(`fname`, `lname`, `age`) VALUES ('$data[1]','$data[2]',$data[3])";
    try{
        $insert_Result = mysqli_query($connect, $insert_Query);
        
        if($insert_Result)
        {
            if(mysqli_affected_rows($connect) > 0)
            {
                echo 'Data Inserted';
            }else{
                echo 'Data Not Inserted';
            }
        }
    } catch (Exception $ex) {
        echo 'Error Insert '.$ex->getMessage();
    }
}

// Delete
if(isset($_POST['delete']))
{
    $data = getPosts();
    $delete_Query = "DELETE FROM `users` WHERE `id` = $data[0]";
    try{
        $delete_Result = mysqli_query($connect, $delete_Query);
        
        if($delete_Result)
        {
            if(mysqli_affected_rows($connect) > 0)
            {
                echo 'Data Deleted';
            }else{
                echo 'Data Not Deleted';
            }
        }
    } catch (Exception $ex) {
        echo 'Error Delete '.$ex->getMessage();
    }
}

// Edit
if(isset($_POST['update']))
{
    $data = getPosts();
    $update_Query = "UPDATE `users` SET `fname`='$data[1]',`lname`='$data[2]',`age`=$data[3] WHERE `id` = $data[0]";
    try{
        $update_Result = mysqli_query($connect, $update_Query);
        
        if($update_Result)
        {
            if(mysqli_affected_rows($connect) > 0)
            {
                echo 'Data Updated';
            }else{
                echo 'Data Not Updated';
            }
        }
    } catch (Exception $ex) {
        echo 'Error Update '.$ex->getMessage();
    }
}



?>


<!DOCTYPE Html>
<html>
    <head>
        <title>PHP INSERT UPDATE DELETE SEARCH</title>
    </head>
    <body>
        <form action="php_insert_update_delete_search.php" method="post">
            <input type="number" name="id" placeholder="Id" value="<?php echo $id;?>"><br><br>
            <input type="text" name="fname" placeholder="First Name" value="<?php echo $fname;?>"><br><br>
            <input type="text" name="lname" placeholder="Last Name" value="<?php echo $lname;?>"><br><br>
            <input type="number" name="age" placeholder="Age" value="<?php echo $age;?>"><br><br>
            <div>
                <!-- Input For Add Values To Database-->
                <input type="submit" name="insert" value="Add">
                
                <!-- Input For Edit Values -->
                <input type="submit" name="update" value="Update">
                
                <!-- Input For Clear Values -->
                <input type="submit" name="delete" value="Delete">
                
                <!-- Input For Find Values With The given ID -->
                <input type="submit" name="search" value="Find">
            </div>
        </form>
    </body>
</html>


///////////////OUTPUT:  
php add edit remove find




                     




Php : How To Use Pdo Row Count Php

Php : How To Use Pdo Row Count Php

Php Code To Get Count Of Rows In Pdo Result Using Mysql Database


________________________________________________________


In this Php Tutorial we will see How To Use rowCount() To Get Number Of Rows From Pdo Result Using MySQL Database Table With Php .

I Use In This Tutorial:
- NetBeans IDE .
- XAMPP .
- PhpMyAdmin .


 




Php Source Code:

<?php

// PHP PDO Using Row Count
// get number of data in pdo result

try{
    $pdoConnect = new PDO("mysql:host=localhost;dbname=test_db","root","");
} catch (PDOException $ex) {
    echo $ex->getMessage();
    exit();
}

$pdoQuery = "SELECT * FROM users";

$pdoResult = $pdoConnect->query($pdoQuery);

$pdoRowCount = $pdoResult->rowCount();

echo "<h1>$pdoRowCount</h1>";


///////////////OUTPUT:  16