Create list of specific files in subdirectories - php

I have a structure:
[catalogues]
[catalogue-name-1]
[data_html]
name-1.html - table of posted values from another form
name-2.html - table of posted values from another form
name-3.html - table of posted values from another form
...
name-n.html - table of posted values from another form
catalogue-name-1.html
[catalogue-name-2]
catalogue-name-2.html
[catalogue-name-3]
catalogue-name-3.html
index.php
How shall I write the function in catalogue-name-?.html to have merged the content of all data_html-tables in one html-table in catalogue-name-?.html?
How shall I write the index.php to have listed all catalogue-name-?.html files as a list of links?
name-1
name-2
name-3
...
name-n

Thank you for the hints. There are 3 PHP files involved:
index.php - there is an entry form posting values into the work/data.php file, then values manipulated in it are saved in new HTML files in respective data_html folders to be then shown by work/preview.php. In data.php, there is about this code:
<?php
// directory names
$dir_catalogues = '../catalogues/';
$dir_catalogue = $dir_catalogues.$_POST['catalogue_name'];
$dir_html = $dir_catalogue.'/data_html/';
$dir_csv = $dir_catalogue.'/data_csv/';
//Check if the directory with the name already exists
//Create our directory if it does not exist
if (!is_dir($dir_catalogues)) {
mkdir($dir_catalogues);
echo "Directory ".$dir_catalogues." created<br>";
}
if (!is_dir($dir_catalogue)) {
mkdir($dir_catalogue);
echo "Directory ".$dir_catalogue." created<br>";
}
if (!is_dir($dir_html)) {
mkdir($dir_html);
echo "Directory ".$dir_html." created<br>";
}
if (!is_dir($dir_csv)) {
mkdir($dir_csv);
echo "Directory ".$dir_csv." created";
}
// file names
$img_name_html = $_POST['img_name'].'.html';
$img_name_csv = $_POST['img_name'].'.csv';
$catalogue_name = 'catalogue-'.$_POST['catalogue_name'].'.html';
// HTML file content
$html = '
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Images rights information</title>
<style>
...
</style>
</head>
<body>
<div class="wrapper">
<table cellpadding="5">
<th colspan="2">Image info</th>
<tr class="thumb_row""><td class="logo" colspan="2"><img class="img_thumb" src="'.$_POST['img_use_url'].'" alt="Thumbnail"><h3>'.$_POST['img_title'].'</h3></td></tr>
<tr><td width="140" class="lab">File name</td><td colspan="5">'.$_POST['img_name'].'</td></tr>
<tr><td width="140" class="lab">Description</td><td colspan="5">'.$_POST['img_description'].'</td></tr>
<tr><td width="140" class="lab">Source URL</td><td colspan="5">'.$_POST['img_source_url'].'</td></tr>
<tr><td width="140" class="lab">Image use URL</td><td colspan="5">'.$_POST['img_use_url'].'</td></tr>
<tr><td width="140" class="lab">Copyright owner</td><td colspan="5">'.$_POST['img_copyright_owner'].'</td></tr>
<tr><td width="140" class="lab">Author</td><td colspan="5">'.$_POST['img_author'].'</td></tr>
<tr><td width="140" class="lab">Author\'s URL</td><td colspan="5">'.$_POST['img_author_url'].'</td></tr>
<tr><td width="140" class="lab">License</td><td colspan="5">'.$_POST['img_license'].'</td></tr>
<tr><td width="140" class="lab">License Nr.</td><td colspan="5">'.$_POST['img_license_nr'].'</td></tr>
</table>
</div>
</body>
</html>';
......
// create catalogue HTML file
file_put_contents($dir_catalogue .'/'.$catalogue_name, $html, FILE_APPEND | LOCK_EX);

I made have achieved some progress (found and applied som working scripts). But I stuck at the following:
sofar I have in my code about this:
<!-- FILE HEADDINGS -->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Images rights information</title>
<style>
*{font-family: arial, tahoma, verdana;}
.wrapper{width: 900px; margin:auto;box-sizing:border-box;}
table{width:100%}
th{font-size:2.2em; font-weight:bold;}
.thumb_row{background:#eee;}
td.logo, th{border-bottom:2px solid #999;}
.img_thumb{max_height: 180px; width:auto; margin: .2em; float:left;}
td.logo img{ float: left; margin-right:2em;}
td a img{border: 2px solid blue;}
td a:hover img{border: 2px solid orange;}
td.lab{font-weight:bold;vertical-align:top;}
td h3{font-size:2em; font-weight:bold}
</style>
</head>
<body>
<div class="wrapper">
<!-- FILE HEADDINGS END -->
<table cellpadding="5">
<th colspan="2">Image info</th>
<tr class="thumb_row"><td class="logo" colspan="2"><img class="img_thumb" src="'.$_POST['img_use_url'].'" alt="Thumbnail"><h3>'.$_POST['img_title'].'</h3></td></tr>
<tr><td width="140" class="lab">File name</td><td colspan="5">'.$_POST['img_name'].'</td></tr>
<tr><td width="140" class="lab">Description</td><td colspan="5">'.$_POST['img_description'].'</td></tr>
<tr><td width="140" class="lab">Source URL</td><td colspan="5">'.$_POST['img_source_url'].'</td></tr>
<tr><td width="140" class="lab">Image use URL</td><td colspan="5">'.$_POST['img_use_url'].'</td></tr>
<tr><td width="140" class="lab">Copyright owner</td><td colspan="5">'.$_POST['img_copyright_owner'].'</td></tr>
<tr><td width="140" class="lab">Author</td><td colspan="5">'.$_POST['img_author'].'</td></tr>
<tr><td width="140" class="lab">Author\'s URL</td><td colspan="5">'.$_POST['img_author_url'].'</td></tr>
<tr><td width="140" class="lab">License</td><td colspan="5">'.$_POST['img_license'].'</td></tr>
<tr><td width="140" class="lab">License Nr.</td><td colspan="5">'.$_POST['img_license_nr'].'</td></tr>
</table>
<!-- FILE FOOTER -->
</div>
</body>
</html>
// CSV file content
$csv = $_POST['catalogue_name'].'|'.$_POST['img_title'].'|'.$_POST['img_name'].'|'.$_POST['img_description'].'|'.$_POST['img_source_url'].'|'.$_POST['img_copyright_owner'].'|'.$_POST['img_author'].'|'.$_POST['img_author_url'].'|'.$_POST['img_license'].'|'.$_POST['img_license_nr'];
// create catalogue HTML file
file_put_contents($dir_catalogue .'/'.$catalogue_name.'.html', $html, FILE_APPEND | LOCK_EX);
file_put_contents($dir_catalogue .'/index.html', $html, FILE_APPEND | LOCK_EX);
// create catalogue CSV file
file_put_contents($dir_catalogue .'/'.$catalogue_name.'.csv', $csv, FILE_APPEND | LOCK_EX);
file_put_contents($dir_catalogue .'/index.csv', $csv, FILE_APPEND | LOCK_EX);
}
But the result is that the entire $html code is inserted into the catalog file (I want to have the "headings" added only in the beginning, the all individually saved tables, and the "footer" also always only on end of the HTML-catalog file.
The CSV output has all variables put into one endless line. I want to have each record in one line.

Related

How to open an image from the database as a popup?

I have this script. I have searched dozens of questions on this site and none of them seem to work. However, none of the ones I saw are calling for the image from a database either, so that may be the issue.
My obejctive is to make it so this can open in the center of the screen as a popup instead of to another page or on top of the page as a link. Any help or guidance is appreciated.
I don't mind using java or css if that is what is needed, however I will tell you that my understanding of both is very small so far, but I'm learning
EDIT 2
I have tried to use lightbox but that didn't work at all. I also tried onclick="window.open... but that didn't work either. At best it opened a blank page in the corner of the window
Here is what I have
<img src="<?= $imageURL; ?>" width="350" />
EDIT
Here is the entire page script I have
<table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#000000">
<tr valign="top" align="left">
<td width="4%"><a style="text-decoration:none" href="/apppages/more.html"><img src="../appimg/arrow.png" width="108" height="68" border="0"></a></td>
<td width="96%" valign="middle"><a style="text-decoration:none" href="/apppages/more.html"> <font color="#CCCCCC" size="6" face="Verdana, Arial, Helvetica, sans-serif">More
Links Options</font></a></td>
</tr>
</table>
<p><br>
</p>
<p> <img src="header2.jpg" width="921" height="479"><br>
<font size="7"><br>
<a href="app-form.php" target="_blank"><font face="Verdana, Arial, Helvetica, sans-serif" color="#9999FF">TAP
HERE TO ADD YOUR SELFIE</font></a></font><br>
<br>
<?php
include 'config.php';
// Get images from the database
$query = $db->query("SELECT nameviewer, file_name FROM image ORDER BY uploaded_on DESC");
?> </p>
</div>
<table width="20%" border="1" cellspacing="0" cellpadding="0" align="center">
<?php
$i = 0;
while ($row = $query->fetch_assoc()){
$nameviewer = 'uploads/'.$row["nameviewer"];
$imageURL = 'uploads/'.$row["file_name"];
if ($i++ % 4 == 0) { // start new row before each group of 4
echo '<tr>';
}
?>
<td valign="top">
<div align="center"><img src="<?= $imageURL; ?>" width="350" /><br>
<font color='lightblue'> <b><font size="6"><?php echo htmlspecialchars($row["nameviewer"]); ?></font></b></font><br>
<font size="6"><i><font color="#999999">Tap The Pic To Expand</font></i></font>
</div>
</td>
<?php
if ($i % 4 == 0) { // end row after a group of 4
echo '</tr>';
}
}
if ($i % 4 != 0) { // end the last row if it wasn't an even multiple of 4
echo '</tr>';
}
?>
</table>
One of the ways is to use a jquery lightbox
First, add the necessary CSS and JS at the top of your script
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript" src="https://www.jqueryscript.net/demo/Responsive-Touch-enabled-jQuery-Image-Lightbox-Plugin/dist/simple-lightbox.jquery.min.js"></script>
<link href='https://www.jqueryscript.net/demo/Responsive-Touch-enabled-jQuery-Image-Lightbox-Plugin/dist/simple-lightbox.min.css' rel='stylesheet' type='text/css'>
<link href='https://www.jqueryscript.net/demo/Responsive-Touch-enabled-jQuery-Image-Lightbox-Plugin/demo.css' rel='stylesheet' type='text/css'>
<link href="https://jquery.app/jqueryscripttop.css" rel="stylesheet" type="text/css">
Enclose your table containing the photos by <div class="gallery"> and </div>
Add the following block to trigger the lightbox script:
<script>
$(function(){
var gallery = $('.gallery a').simpleLightbox({navText: ['‹','›']});
});
</script>
Please further adjust the code if necessary to suit your needs.
See the Sandbox (based on your script and slightly amended) for a working example (check the source and you will be able to see that it is applying the above 3 steps) :
http://www.createchhk.com/SOanswers/sub6/testSO15Oct2022.html

how to retrieve the image stored in the database via php?

I have created a database using phpmyadmim and to access it, I made a name.php file.
Accessing the registration number, name was easy, but the image retrieval is not so.
I stored the image via phpmyadmin using LongBlob as the type..but I'm not able to display it..
how to retrieve the image?
Please if someone can help,it would be high appreciated.
Thanks,
LJ
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>PHYTOCHEMICAL DB</title>
<style type="text/css">
body {
font-family: Georgia, "Times New Roman",
Times, serif;
color: purple;
background-color: #EEEEEE}
</style>
</head>
<body>
<table width="800" border="" align="center">
<tr>
<td colspan="2" style="background-color:#FFA500;height:30px;width:700px">
<div> <img src="leaves.jpg" height="300" width="900"/> </div> <br/>
</td>
</tr>
<tr>
<td style="background-color:#FFD700;width:250px;">
<b>Menu</b><br>
<i> Home<br>
<i> Search by Database ID (DID) <br>
<i><a href="mw.php">Search by Molecular weight<br>
</td>
<td style="background color:#EEEEEE;height:500px;width:800px;">
<form action="" method="POST">
<p> Enter the name </p>
<input type="text" name="pname">
<input type="submit" value="submit" name="submit1">
<br>
</form>
<?php
$mysqli = new mysqli("localhost", "root", "osddosdd", "phychem");
if (mysqli_connect_errno()) {
printf("Connect failed:%s\n", mysqli_connect_error());
exit();
}
if (isset($_POST['submit1'])) {
$pname = $_POST['pname'];
$query = ("select * from pchem where name LIKE '$pname'");
$result = $mysqli->query($query);
echo 'The retrieved query is:';
echo "<table border='1'>
<tr>
<th>DID</th>
<th>Name</th>
<th>Molecular weight</th>
</tr>";
while ($row = $result->fetch_row()) {
echo '<tr>';
printf("<th>%d</th> <th> %s </th> <th> %2f </th>", $row[0], $row[1], $row[2]);
echo '</tr>';
echo '</table>';
}
}
$mysqli->close();
?>
</td>
</table>
</body>
</html>
don't store the images directly into the database as BLOBs. Just store them as files and only store the file names in the database.
I have googled and found some links that would solve you problem. Refer these links :
http://www.phpro.org/tutorials/Storing-Images-in-MySQL-with-PHP.html
http://www.coderslexicon.com/inserting-images-into-mysql-and-retrieving-them-using-php/
http://pujanpiya.com.np/?q=node/25

gambio gx2 module implementation. Filename CONSTANT

I have got problems to Implement my own Module, wich I want to develop.
My Steps:
in /admin/includes/application_top.php + define('FILENAME_MYMODULE', 'test_mymodule.php');
/lang/german/admin/gm_german.php + define('BOX_MYMODULE', 'MyModule');
/lang/german/sections/_samples/admin_menu.lang.inc.php + 'BOX_MYMODULE' => 'MyModule',
INSERT INTO ``gm_lang_files_content`` VALUES('', 246, 'BOX_MYMODULE', 'MyModule');
/system/conf/AdminMenu/gambio_menu.xml + <menuitem sort="10" link="FILENAME_MYMODULE" title="{$txt.BOX_MYMODULE}" />
Now my Problem is, when I clear the cache and reload, I didn't see my menu option.
If I change test_mymodule.php in gm_ebay.php or somethink else it will work.
Why it dosent work with my file?
I would be extremely grateful for your help
i found the solution, i think some newbie like me in gambio search it to.
last point insert in database
ALTER TABLE admin_access ADD myfile INT( 1 ) NOT NULL DEFAULT '0';
and update it to 1 for true
Updated solution to stay updateable:
Menu entry:
/user_classes/conf/admin_menu/menu_mymodule.xml
<?xml version="1.0"?>
<!-- {load_language_text section="admin_menu"} -->
<admin_menu>
<menugroup id="BOX_HEADING_IMPORT_EXPORT">
<menuitem sort="10" link="mymodule.php" link_param="your=param" title="MyModule" />
</menugroup>
</admin_menu>
Create /admin/includes/modules/mymodule/css/mymodule_backend.css
Create /admin/includes/modules/mymodule/index.php
<?php
$string = '<td class="boxCenter" width="100%" valign="top">
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td class="pageHeading" style="background-image: url(images/icons/amicicard_30.png);float:left;"> MyModule</td>
<td class="pageHeading" align="right"><img src="images/pixel_trans.gif" border="0" alt="" width="HEADING_IMAGE_WIDTH" height="HEADING_IMAGE_HEIGHT"></td>
</tr>
</table>
<span class="main">
';
echo $string;
echo "Do your work here like that.<br>";
echo "You can include classes etc. in here and use echo as output.";
echo '</div>';
Create /admin/mymodule.php
<?php
$version = phpversion();
$majorVersion = explode('.', $version);
$majorVersion = intval($majorVersion[0]);
if ($majorVersion < 5)
{
return;
}
require('includes/application_top.php');
$isPopup = isset($_GET['popup']) ? true : false;
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo $_SESSION['language_charset']; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<link rel="stylesheet" type="text/css" href="includes/modules/mymodule/css/mymodule_backend.css">
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<?php
if (!$isPopup)
{
//include header:
require(DIR_WS_INCLUDES . 'header.php');
//include body:
echo '<table border="0" width="100%" cellspacing="2" cellpadding="2">
<tr>
<td class="columnLeft2" width="'.BOX_WIDTH.'" valign="top"><table border="0" width="'.BOX_WIDTH.'" cellspacing="1" cellpadding="1" class="columnLeft">';
//include left navigation:
require(DIR_WS_INCLUDES . 'column_left.php');
echo '</table></td>';
}
else
{
//include body:
echo '<table border="0" width="100%" cellspacing="2" cellpadding="2">
<tr>
<td class="columnLeft2" width="100%" valign="top"><table border="0" width="0" cellspacing="1" cellpadding="1" class="columnLeft">';
}
require_once('./includes/modules/mymodule/index.php');
echo '</tr></table>';
if (!$isPopup)
{
//footer:
require(DIR_WS_INCLUDES . 'footer.php');
}
echo '<br>
</body>
</html>';
require(DIR_WS_INCLUDES . 'application_bottom.php');
?>
Update your Database like mentioned by user unfair
last point insert in database ALTER TABLE admin_access ADD myfile INT( 1 ) NOT NULL DEFAULT '0';
and update it to 1 for true
Thats it.
Result Image

PHP. session_start() [function.session-start]: Cannot send session cookie error

Im having problem in my web where it works perfectly on my local server which is a WAMPSERVER i can run my website completely but after adding to the web server it give me error saying
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/multisl/public_html/udrivetest/index.php:2) in /home/multisl/public_html/udrivetest/init.php on line 4
init.php
<?php
ob_start();
session_start();
mysql_connect("localhost","***","***");
mysql_select_db('****');
include 'function/user.func.php';
include 'function/car.func.php';
include 'function/image.func.php';
?>
header1.php
<head>
<title></title>
<link href="css/style.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.style4 {
font-size: 16px;
font-weight: bold;
}
.style5 {
font-size: 12px
}
.a {
color:#000000;
text-decoration:none;
}
-->
</style>
</head>
<body>
<div style="margin-left:10px;">U Drive.lk</div>
<div class="style5" style="text-align:right; padding:10px;">
Contact Us | Disclaimer | Site Map</div>
</div>
<div>
<div style="background-image:url(images/Car3.png); border:#FFFFFF; border-style:solid; border-width:2px; border-top:none; border-bottom:none; width:920px; height:450px; margin-left:auto; margin-right:auto;" align="center">
<br/>
<img src="images/Car6.png" width="920px" height="135px" />
<div style="margin-top:-22px; height:22px; margin-left:55px;" align="left"><a class="a">Home</a></div>
<div style="margin-top:-22px; height:22px; margin-left:200px;" align="left"><a class="a">Gallery</a></div>
<div style="margin-top:-22px; height:22px; margin-left:350px;" align="left"><a class="a">Leasing</a></div>
<div style="margin-top:-22px; height:22px; margin-left:490px;" align="left"><a class="a">Testimonial</a></div>
<div style="margin-top:-22px; height:22px; margin-left:640px;" align="left"><a class="a">Forum</a></div>
<div style="margin-top:-22px; height:22px; margin-left:790px;" align="left"><a class="a">FAQ</a></div>
<div style="margin-left:auto; margin-right:auto; margin-top:20px;" >
<script type="text/javascript">
AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0','width','920','height','135','src','Flash/Flash Header2','quality','high','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','movie','Flash/Flash Header2' ); //end AC code
</script><noscript><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="920" height="135">
<param name="movie" value="Flash/Flash Header2.swf" />
<param name="quality" value="high" />
<embed src="Flash/Flash Header2.swf" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="920" height="135"></embed>
</object></noscript>
</div>
<div align="left">
<table width="100%" border="0" style="margin-top:10px;">
<tr>
<td width="20%" valign="top" ><div align="left"><img src="images/Buy_men.png" /> <img src="images/Sell_men.png" /> <img src="images/Rent_men.png" /></div>
<div style="color:#FFFFFF;margin-top:-90px; width:38px; margin-left:10px;">Buy</div>
<div style="color:#FFFFFF;margin-top:-18px; width:38px; margin-left:65px;">Sell</div>
<div style="color:#FFFFFF;margin-top:-20px; width:38px; margin-left:115px;"><a style="color:#FFFFFF;">Rent</a></div></td>
<td width="38%"><fieldset style="background-color:#cccccc">
<span class="style4">Quick Search</span>
</fieldset></td>
<td width="42%"><fieldset style="background-color:#cccccc">
<span class="style4">Login</span>
<?php include_once('login.php'); ?>
</fieldset></td>
</tr>
</table>
</div>
</div>
</div>
<div style="margin-left:auto; margin-right:auto; width:920px;">
login.php
<?php
if (logged_in()) {
echo '<br/> View User Profile <br/>Log Out';
} else {
?>
<form action="" method="post">
<table width="100%" border="0" style="margin-top:-5px;" >
<tr>
<td><div align="left">User Name :</div></td>
<td><input type="text" name="Quick_Name" size="25" /></td>
</tr>
<tr>
<td><div align="left">Password :</div></td>
<td> <input type="text" name="PassWord" size="25" /> <input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
Forgot your password / Register User
<?php
}
if (isset($_POST['Quick_Name'], $_POST['PassWord'])) {
$forgotEmail = $_POST['Quick_Name'];
$login_password = $_POST['PassWord'];
if (empty($forgotEmail) || empty($login_password)) {
$errors[] = 'Email and Password required';
} else {
$login = login($forgotEmail, $login_password);
if ($login === false) {
$errors[] = 'Unable to Log you in';
}
}
if (!empty($errors)) {
foreach ($errors as $error) {
echo $error, '<br/>';
}
} else {
$_SESSION['user_id'] = $login;
header('Location:index.php');
exit();
}
}
?>
index.php
<?php include 'init.php'; include 'widgets/header1.php'; ?>
<div>
</div>
</div>
</body>
</html>
Please Can any one help me out to solve this problem or give me a clue what I am doing here?
Thanks in Advance
In PHP, headers get sent as soon as the page starts to print something - which can occur unintentionally due to whitespace.
Based on your error message (output started at /home/multisl/public_html/udrivetest/index.php:2)
I am guessing this line of index.php:
<?php include 'init.php'; include 'widgets/header1.php'; ?>
appears on line 2 of the file? But you cannot have a line of whitespace above it, it needs to be line 1 since one of the includes is dealing with headers (cookie for session).
Otherwise the blank line 1 is "printing something" - a new line - which sends headers probably before you are intending.
Basically, check all files for not having blank lines above opening tags <?php and don't have blank lines below closing tags ?>.
This is most likely that there exists some whitespace that is sent before the session_start command, try removing the closing PHP tag (?>) and report back the results.
By doing this you remove the possibility of it outputting any invalid whitespace.
There should have some blank space sending back to client. It is a good practise to leave php closing tag ?> if your last line of code is a php code.
Make sure that you dont leave any blank space before and after the php opening tag and php closing tag
You can prevent any output to send to client by using object buffering with ob_start() .
put the ob_start at the very beginning of your php code can save you time.
I would like to consider removing ob_start() from init.php to index.php.
thanks

delete selected images with the help of checkboxes

This is a page where users can edit their uploaded images. There is a checkbox near each image. I want to delete the selected images after user clicks the "delete selected images" button (each checkbox contains the image name as value). How can I do that?
<?php
session_start();
//////////////if user already logged in go to login.php/////////
if (isset($_SESSION['email'] )&& isset($_SESSION['password'] ))
{
} else{header( "Location: login.php" ); }
include('includes/config.php');
if (isset($_POST['esubmit']) ){
$checkbox=$_POST['delete'];
echo $checkbox;
}//main one
if (isset($_POST['esubmit']) ){
} else { $clickeditid=$_GET["id"];
$_SESSION['eid']= $clickeditid ;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="css/css.css" rel="stylesheet" type="text/css" />
<style rel="stylesheet" type="text/css">
input {
border-style: solid;
border-color: #000000;
border-width: 1px;
background-color: #ffffff;
}
</style>
<script src="js/css_browser_selector.js" type="text/javascript"></script>
</head>
<body>
<?php
include('includes/topmenu.php');//top menu
echo '<br />';
include('includes/usermenu.php');///bcoz of this menu error occurs
////////////////////
include('includes/edit_img_menu.php');
?>
<form id="form1" name="form1" method="post" action="editimg.php?id=<?php echo $_SESSION['eid'];?>">
<table align="center" width="70%" cellspacing="0">
<tr>
<td colspan="3" align="center" bgcolor="#FFFFFF"></td>
</tr>
<tr>
<td colspan="3" align="center" bgcolor="#FFFFFF"><?php
$imgcheck=mysql_query("
SELECT *
FROM `images`
WHERE `deal_id` =$_SESSION[eid]
LIMIT 0 , 30
");
$numimgcheck=mysql_num_rows($imgcheck);
if($numimgcheck==0){echo '<span style=color:#ff0000; >No pictures uploaded</span>';}
while ($rowimg2= mysql_fetch_array($imgcheck)){
$imgname=$rowimg2['name'];
{
echo ' <a href="users/'.$_SESSION['userid'].'/images/'.$imgname.'" rel="lightbox[slide]" caption=".">';
}
{ echo '<img src="users/'.$_SESSION['userid'].'/images/thumbs/'.$imgname.'" border="0" />';}
{ echo '</a><input type="checkbox" name="delete" id="delete" value="'.$imgname.'">
';}
}
?></td>
</tr>
<tr>
<td colspan="3" align="center" bgcolor="#FFFFFF"> </td>
</tr>
<tr>
<td colspan="3" align="center" bgcolor="#95F8FD"><input name="esubmit" type="submit" class="red" id="esubmit" value="Delete selected images" /></td>
</tr>
<tr>
<td width="89"> </td>
<td colspan="2"> </td>
</tr>
</table>
</form>
</body>
</html>
You have to create a form to submit the data of the checked checkboxes and treate the submited result with the php code : if you want to put the php code on the same page, you can check if there is any data in the POST vars and create a SQL Query to delete the corresponding pictures.
You can find a very well documented tutorial here : http://sharemyphp.wordpress.com/2009/12/21/ajax-jquery-php-multiple-delete-item-with-check-box/
Regards,
Max
I use the below code to delete images in my admin panel. I saved the file as deletephoto.php. Note that this script works without login. You need to make some changes to fit your use case
<form method="post" action="deletepho.php">
<center>
<input type="submit" value="Delete" name="Delete">
</center>
<?php
\\lets assign the folder name in $title
\\You can assign any name
$title= "test";
\*$directory corresponds to whole path. Edit to your preference. (i assume u store your
images in directory named "images") */
$directory = "$title/images";
\\The array specifies the format of the files
$allowed_types=array('jpg','jpeg','gif','png');
$file_parts=array();
$ext='';
$title='';
$i=0;
$dir_handle = #opendir($directory) or die("There is an error with your image directory!");
while ($file = readdir($dir_handle))
{
if($file=='.' || $file == '..') continue;
$file_parts = explode('.',$file);
$ext = strtolower(array_pop($file_parts));
$title = implode('.',$file_parts);
$title = htmlspecialchars($title);
$nomargin='';
if(in_array($ext,$allowed_types))
{
if(($i+1)%4==0)
$nomargin='nomargin';
echo'
<div id="picture">
<img src="'.$directory.'/'.$file.'" width="150" height="150"><br>
Select <input type="checkbox" value="'.$directory.'/'.$file.'" name="imgfile[]">
\* I specified input name as an array . So that we can store in an array and delete it.*/
</div>';
}
$i++;
}
closedir($dir_handle);
\\Now we have the list of images within form elements
?>
</form>
Now here is the actual code to delete the photos. I saved it as deletepho.php.
$file = $_REQUEST['imgfile'];
$num_files=count($file);
for($i=0;$i<$num_files;$i++)
{
unlink ("$file[$i]");
}
echo "Images successfully deleted.Go <a href='deletephoto.php'>back</a>";

Categories