PHP - Display Images From Folder And Delete Images
I have this script from http://lampload.com/...,view.download/ (I am not using a database) I can upload images fine, I can view files, but I want to delete them. When I press the delete button, nothing happens
http://www.jayg.co.u...oad_gallery.php
<form>
<?php $dir = dirname(__FILENAME__)."/images/gallery" ; $files1 = scandir($dir); foreach($files1 as $file){ if(strlen($file) >=3){ $foil = strstr($file, 'jpg'); // As of PHP 5.3.0 $foil = $file; $pos = strpos($file, 'css'); if ($foil==true){ echo '<input type="checkbox" name="filenames[]" value="'.$foil.'" />'; echo "<img width='130' height='38' src='images/gallery/$file' /><br/>"; // for live host //echo "<img width='130' height='38' src='/ABOOK/SORTING/gallery-dynamic/images/gallery/ $file' /><br/>"; } } }?> <input type="submit" name="mysubmit2" value="Delete"> </form>
any ideas please?
thanks
Similar TutorialsIm using this code to call all the images in a folder: Code: [Select] $handle = opendir(dirname(realpath(__FILE__)).'/images/'); while($file = readdir($handle)){ if($file !== '.' && $file !== '..'){ echo '<img src="admin/img/uploads/'.$file.'" border="0" />'; } } My html says the images are present but they aren't visable on screen: Code: [Select] <div id="contentbody"> <img src="admin/img/uploads/send-button-sprite copy.png" border="0" /> <img src="admin/img/uploads/test" border="0" /> <img src="admin/img/uploads/counter.jpg" border="0" /> <img src="admin/img/uploads/send-button-sprite.png" border="0" /> </div> Any help is much appreciated! Hello once again, i got this code that takes all images from a folder and displays them close to each other. Heres the code: <?php $dir = 'uploads/thumbs/watermarkedthumbs/'; $file_display = array ('jpg', 'jpeg', 'png', 'gif'); if (file_exists($dir) == false) { echo 'tt'; } else { $dir_contents = scandir($dir); foreach ($dir_contents as $file) { $file_type = strtolower(end(explode('.', $file))); if ($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true) { echo '<img src="', $dir, '/', $file, '" alt="', $file, '" />'; } } } ?> How do i add margins in between them? i wanna make it smth like 'margin-left:10px, margin-top-10px'. Also, how do i add an 'overflow'? i mean, my images currently are displayed in a div, and i wanna make it so that if the folder has lets say 9 images, it would only display 6 of them, and the rest would be displayed in the same div after i click a button or smth (like '1' for the first div and '2' would appear if theres an overflow, and when i click '2' the rest of the images would appear). To make these adjustments do i need to make a different php file or do i change something in this code? thanks in advance. Hi, I want to be able to click on the photo and go to the next one in a folder. I have this code already, I just am not quite sure how to finish it. -George Code: [Select] <?php $count = $_GET['count']; $dir = "images"; $names = array(); $handle = opendir($dir); while ($name = readdir($handle)){ if(is_dir("$dir/$name")) { if($name != '.' && $name != '..') { echo "directory: $name\n"; } } elseif ($name != '.DS_Store' ) { $names[] = $name; } } closedir($handle); $numberofitems = count($names); $numberofitems--; if ($count <= $numberofitems){ echo "<p>"; echo "<img src='images/".$names[$count]."'>"; } else {echo "end";} ?> I made an upload image system, the images are stored in a folder, while the image name is stored in database. When I try to execute the image name, it works successfully, but when I try to disply the image from the folder by using the image name in the database, I fail each time. The folder where the images are being stored is named: saveimage Here is the query: Code: [Select] <?php mysql_connect('localhost', 'root', '') or die ('Connection Failed'); mysql_select_db('imagedatabase'); $images = mysql_query("SELECT * FROM img WHERE email='$lemail'"); while($row = mysql_fetch_array($images)) { echo "<img src='saveimage/'".$row['img_description']; } ?> The problem: I'm trying to create a page which outputs images from a folder which I have been able to do, but the problem I'm having is not being able to get the page to display the most recent image according to file modification date at the top. The first set of code below outputs the image timestamps in descending order, from newest to oldest which is what I want, but as soon as I change/add a couple lines of code (Shown in the second lot of code) to get the image file name along with the timestamp, the echoed list (timestamp and file names) gets muddled up in a random order. In short; As soon as the file names are retrieved with the timestamp, the list goes from being organised descendingly, to not. Show timestamp only code (1st lot of code): <?php //Open images directory $ignore = array("..","."); $dir = opendir("images1"); $images = array(); $sortedimages = array(); //List files in images directory while (($file = readdir($dir)) !== false) if (!in_array($file, $ignore)) $images[] = $file; foreach ($images as $image) { $filetime = filemtime("images1/$image"); $sortedimages[] = $filetime; } rsort($sortedimages); foreach ($sortedimages as $sorted) { //foreach ($sorted as $key => $value) //{ echo "$sorted<br/>"; } //} closedir($dir); ?> The show timestamp and file name code (2nd lot of code): <?php //Open images directory $ignore = array("..","."); $dir = opendir("images1"); $images = array(); $sortedimages = array(); //List files in images directory while (($file = readdir($dir)) !== false) if (!in_array($file, $ignore)) $images[] = $file; foreach ($images as $image) { $filetime = filemtime("images1/$image"); $sortedimages[] = array($filetime => $image); } rsort($sortedimages); foreach ($sortedimages as $sorted) { foreach ($sorted as $key => $value) { echo "$key and $value<br/>"; } } closedir($dir); ?> The changes made in the 2nd script from the 1st: //Changed: $sortedimages[] = $filetime; ---> $sortedimages[] = array($filetime => $image); //Included the previously commented out: foreach ($sorted as $key => $value) { } //Changed: echo "$sorted<br/>"; ---> echo "$key and $value<br/>"; Thanks for any help! Can someone tell me how I can remove or delete an image from a folder on a server using PHP? I tried this: Code: [Select] unlink("http://midwestcreativeconsulting.com/jhrevell/wp-content/themes/twentyten_3/upload/" . $location); before my delete MySQL statement, but I keep getting this error: Warning: unlink() [function.unlink]: http does not allow unlinking in /home/midwestc/public_html/jhrevell/wp-content/themes/twentyten_3/removejewelry.php on line 22 Can anyone help and tell me how I can make it work? I'm trying to write code that will let me pull 10 out of 15 images out of a folder and display them on my site. The images are all different, and I don't want dupes to show. So far, I have the following code figured out: --------------- $s = array ("image.jpg", "image2.jpg"); // as many images as you want $n = rand(1,len($s)); // randomly pick a number between 1 and the length of the array echo "<img src='". $s[$n] .'">"; // create an image tag for the randomly selected imagine (value of the randomly defined key) array_pop($s, $n); // This piece isn't right, it needs to EXTRACT and delete the $n array element. // Next random image $n = rand(1,len($s)); echo "<img src='". $s[$n] .'">"; --------------- Any ideas what array_pop should be to work properly? Thank you for the help! please help me to solve this problem : when my client want to remove a record all the related images must be deleted with it ,is that possible?? Hi
I echo out images from a folder. The problem is that I have a html file in the same folder but I don't want to echo that out. How can I write my code to get rid of the html in the output?
Here my code:
<?php $filetype = '*.html'; $dirname = substr('$filetype'); $i=0; if (isset($_POST['submit2'])) { //get the dir from POST $selected_dir = $_POST['myDirs']; //now get the files from within the selected dir and echo them to the screen foreach(glob($selected_dir . DIRECTORY_SEPARATOR . '*') as $dirname) { echo substr($dirname, 0, -4); echo '<img src="'.$dirname.'" />'; echo "<label><div class=\"radiobt\"><input type='radio' name='radio1' value='$i'/></div></label>"; } } ?>P.S. when I echo out : substr($dirname, 0, -4); I want to get ride of the html filename there too. How can I do so something like this? I have searched around and found this code that suggests that I should be able to read an image file and echo it directly in to the page without hyperlinking to the file that is outside the public folder, but i get the error message that it is not there even though the file is. Warning: getimagesize() [function.getimagesize]: Unable to access "/home/****/upload/AAABBBCCC.JPG" in /home/****/public_html/client.php on line 40 Code: [Select] $image = "AAABBBCCC.JPG"; $path= "/home/****/upload/"; $details = getimagesize($path . $image); header ('Content-Type: ' . $details['mime']); echo file_get_contents($path . $image); I wonder whether someone may be able to help please. I've put together the following page http://www.mapmyfinds.co.uk/development/deletetest.php which allows users to view a gallery of their uploaded images. I'm now starting to work on the `deletion` functionality and I've attached a suitable icon and the Javascript code so the user can select the image to delete. From the research I've done, I know that to delete the files from my server I need to use the PHP `unlink` command. The problem I'm having is I'm not sure how to tie up the 'deletion' selection made by the user and the physical deletion of the files. I've added my `unlink` code below which uses the relative path and then for the thumbnails, Ive added the echo source filename, so the image in the Thumbnails folder matches the one of the same name in the gallery being deleted, but as I said, it's at this point that I'm not sure what to do next. Code: [Select] <?php unlink($path); unlink($path . 'Thumbnails/' . "<?php echo $source; ?>"); ?> I just wondered whether someone could perhaps have a look at this please and possibly provide some guidance on how I can link up the Javascript `click` event and the deletion of the files. Many thanks and kind regards Basically i have a folder with 100+ images they are NOT all the same extension, what im wanting to do is use PHP to find all the images and put them all in a database. how would i go about doing this? thanks Hey guys, i'm new to this site and would need some help with coding. So i'm making a car part website which should has brand/model search. It's a dropdown search which will get the brand / model from database and should display all the parts for that exact model, from folder. Database structure which i have is id, master, name. ( Here's some pictures to clear out what i'm doing. http://imgur.com/a/7XwVd ) So the problem is that it does not get the images from folder named ex: *_audi_a3.jpg. And link to codes what have been written already. Parts.php: http://pastebin.com/q6vdypge Update.php: http://pastebin.com/DymhGQ17 Search_images.php: http://pastebin.com/LF5Q0i8f Core.js: http://pastebin.com/bgc0y4TS I don't know what's wrong with it sadly. One thing i noticed when i used firebug it gives error on category when searching error:true. Hope you guys understand what i mean here, and also all help is appreciated. And move this post if it's in wrong place. Thanks! Edited by aeonius, 17 October 2014 - 12:15 PM. Hi all, i want delete old updated images with considering its index in array. <?php global $oldimg; $oldimg = array(); //action: edit news if (isset($_GET['id'])) { $NewsID = (int)$_GET['id']; if ($NewsID == 0) { $rdir = '<META HTTP-EQUIV="Refresh" CONTENT="1.4;URL=panel-news.php">'; die($rdir);} //get user from the database and put data into $_POST variables. //Include database connection details require_once('../config.php'); //Connect to mysql server $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } //Select database $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } $rs = mysql_query("SELECT newsimg1, newsimg2, ". " newsimg3 FROM news WHERE id = $NewsID"); if (mysql_num_rows($rs) == 0) die('no such a newsID!'); $row = mysql_fetch_assoc($rs); $oldimg[0] = $row['newsimg1']; $oldimg[1] = $row['newsimg2']; $oldimg[2] = $row['newsimg3']; } ?> <form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>" enctype="multipart/form-data"> <input type=file name="file[]" size=20 accept="image/jpg,image/jpeg,image/png"> <input type=file name="file[]" size=20 accept="image/jpg,image/jpeg,image/png"> <input type=file name="file[]" size=20 accept="image/jpg,image/jpeg,image/png"> <input type="hidden" name="MAX_FILE_SIZE" value="2097152" /> <input type="hidden" name="NewsID" value='<?php echo (isset($NewsID))?$NewsID:"0";?>'> <input type="submit" value="edit" id="save" name="save"/> </form> <?php if (isset($_POST['save']) && isset($_POST['NewsID'])){ $NewsID = (int)$_POST['NewsID']; //Include database connection details require_once('../config.php'); //Connect to mysql server $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } //Select database $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } //max fle size value $max_file_size = 2097152; //Global newsimg global $newsimg;global $ctr; $ctr = 0; //timestamp to make files unique names $timestamp = time(); //destination folder path $destpath = "../Dir/Newsimg/"; //looping each file or image from the form while(list($key,$value) = @each($_FILES["file"]["name"])) { //check if any empty or errors if(!empty($value)){ if ($_FILES["file"]["error"][$key] > 0) { $edir ='<div id="fail" class="info_div">'; $edir .='<span class="ico_cancel"><strong>'; $edir .="err : ". $_FILES["file"]["error"][$key] .'</strong></span></div>'; echo($edir); } else { //add the timestamp to filename $file_name = $timestamp.$_FILES['file']['name']; //temp name from upload form, key of each is set $source = $_FILES["file"]["tmp_name"][$key] ; //getting the file type $file_type = $_FILES["file"]["type"][$key]; //placing each file name into a variable $filename1 = $_FILES["file"]["name"][$key]; //lowering the file names $filename = strtolower($filename1); //adding timestamp to front of file name $filename = "$timestamp$filename"; ++$timestamp; if ($file_type != "image/jpeg" && $file_type != "image/png" && $file_type != "image/jpg") { die(" <div id='fail' class='info_div'> <span class='ico_cancel'><strong> Invalid Format!</strong></span><br /> <a href=\"javascript: history.go(-1)\">Retry</a> </div>"); } //moving the file to be saved from the temp location to the destination path move_uploaded_file($source, $destpath . $filename); //the actual final location with timestamped name $final_file_location = "$destpath$filename"; if (file_exists($final_file_location)) { if (isset($oldimg[$ctr])) { chdir('../Dir/Newsimg/'); echo $oldimg[$ctr]; unlink($oldimg[$ctr]);} $newsimg[$ctr] = $filename; $ctr++; ?>TNX. I want to display thumbnails for a picture gallery and when user clicks on image the larger image opens in a new window. I can't find a straight answer on the php.net website. Can someone help? I am looking to display image paths in a row separated by commas. There are 6 images that goes to each user and I would like only the 6 images at be in each " " Like this: "images/listings/listing_516013019A-only.jpg,images/listings/listing_848813453A-1.jpg,images/listings/listing_664613453A-2.jpg,images/listings/listing_520313453A-3.jpg,images/listings/listing_690513453A-4.jpg,images/listings/listing_125113453A-5.jpg,images/listings/listing_641013453A-6.jpg," "images/listings/listing_736913186A-1.jpg,images/listings/listing_822713186A-2.jpg,images/listings/listing_136513186A-3.jpg,images/listings/listing_700313186A-4.jpg,images/listings/listing_716013186A-5.jpg,images/listings/listing_213113186A-6.jpg," "images/listings/listing_292113254A..-1.jpg,images/listings/listing_854413254A..-2.jpg,images/listings/listing_446013254A..-3.jpg,images/listings/listing_676313254A..-4.jpg,images/listings/listing_563413254A..-5.jpg,images/listings/listing_341513254A..-6.jpg," Right now it is displaying them like this "images/listings/listing_516013019A-only.jpg," "images/listings/listing_848813453A-1.jpg," "images/listings/listing_664613453A-2.jpg," "images/listings/listing_520313453A-3.jpg," "images/listings/listing_690513453A-4.jpg," "images/listings/listing_125113453A-5.jpg," "images/listings/listing_641013453A-6.jpg," "images/listings/listing_736913186A-1.jpg," "images/listings/listing_822713186A-2.jpg," "images/listings/listing_136513186A-3.jpg," "images/listings/listing_700313186A-4.jpg," "images/listings/listing_716013186A-5.jpg," "images/listings/listing_213113186A-6.jpg," "images/listings/listing_292113254A..-1.jpg," "images/listings/listing_854413254A..-2.jpg," "images/listings/listing_446013254A..-3.jpg," "images/listings/listing_676313254A..-4.jpg," "images/listings/listing_563413254A..-5.jpg," "images/listings/listing_341513254A..-6.jpg," Here is the code I have: Code: [Select] <?php // Make a MySQL Connection mysql_connect("localhost", "xxxxxxxx", "xxxxxxxx") or die(mysql_error()); mysql_select_db("xxxxxxxx") or die(mysql_error()); $result = mysql_query("SELECT * FROM listimages ORDER BY listimages.listingid DESC ") or die(mysql_error()); while($row = mysql_fetch_array($result)) { echo "\""; echo "$row[imagepath],"; echo "\""; echo "<br>"; } ?> My tables for the images is "listimages" and the columns a id (which are the auto_increments) imagepath (which shows the path/image1.jpg) mainimage (which just shows 0 or 1 depending on the picture that is the default for that listing, 1 being default) listingid (shows numbers 1 2 3 etc corresponding to the Id in the listings table to show what images go with what listing) There are up to 6 images for each listing. Any idea how to fix this? I'm creating a funeral home site and on the side of the page I would like to display the last 5 funeral obituaries that are in mySQL database (name and certain sized image (images are in "image" field) w/ a link to an "obituaries detail page for the individual deceased person"). I am able to do this successfully listing only the names....how can I list the image associated with the name? This is what I have so far: Code: [Select] <?php include("connect.php"); ?> Code: [Select] <?php $query = "SELECT id, deceased_name, deceased_date, DATE_FORMAT(deceased_date, '%M %D, %Y') as datetext"; $query .= " FROM scales_obits ORDER BY deceased_date DESC LIMIT 5"; $listings = mysql_query($query); if (!listings) { echo("<p>Error retrieving listings from lookup table<br>". "Error: " . mysql_error()); exit(); } echo("<table border=\"0\" width=\"100%\" class=\"obit\">"); while ($listing = mysql_fetch_array($listings)) { $deceased_name = $listing["deceased_name"]; $deceased_date = $listing["datetext"]; $id = $listing["id"]; echo("<tr><td width=\"100%\"><a href=\"obitdetail.php?id=".$id."\"><strong>".$deceased_name."</strong></a></td><td> </td>"); } echo("</table>"); ?> I read everywhere where it says not to stoe images in MYSQL but I have a code where I'm trying to display the images. They are all jpegs that are stored in the table. Here is the code Code: [Select] <?php include "dbaptsConfig.php"; include "searchaptsstyle.css"; // test id, you need to replace this with whatever id you want the result from $id = "1"; // what you want to ask the db $query = "SELECT * FROM `apartments` WHERE `id` = ".$id; // actually asking the db $res = mysql_query($query, $ms); // recieving the answer from the db (you can only use this line if there is always only one result, otherwise will give error) $result = mysql_fetch_assoc($res); // if you uncomment the next line it prints out the whole result as an array (prints out the image as weird characters) // print_r($result); // print out specific information (not the whole array) echo "<br/>"; echo "<div id='title'>".$result['title']."<br/></div>"; echo "<br/>"; echo "<div id='description'>".$result['description']."<br /></div>"; echo "<br/>"; echo "<div id='table'><tr>"; echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Provider's Phone Number: ".$result['phone']."<br /></td>"; echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Provider: ".$result['service']."<br /></td>"; echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Borough: ".$result['county']."<br /></td>"; echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Town: ".$result['town']."<br /></td>"; echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Bedrooms: ".$result['rooms']."</td>"; echo "<td> </td>"; echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Bathrooms: ".$result['bath']."<br /></td>"; echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Square Footage: ".$result['square']."<br /></td>"; echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Rent: ".$result['rent']."<br /></td>"; echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Listed On: ".$result['time']."<br /></td>"; echo "</tr></div>"; header("Content-type: image/jpeg"); echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Listed On: ".$result['image1']."<br /></td>"; ?> Thanks |