View/Edit Images In One Folder

  • ace5p1d0r
  • Expert
  • Expert
  • User avatar
  • Posts: 630
  • Loc: UK

Post 3+ Months Ago

I run a simple image host Mr Image host.com

When a user uploads an image a thumbnail is automatically generated in the folder /thumbs and the main image is in the folder /uploads.

My site runs on a simple CMS, and I was wondering how I could view from my admin panel all the images in these folders at one time, and delete any if necessary.

Thanks for your help :)
AcE
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Vincent
  • Expert
  • Expert
  • User avatar
  • Posts: 721
  • Loc: Brisbane, Australia

Post 3+ Months Ago

Code: [ Select ]
//Base Directory
$baseDir = getcwd()."\\images\\";

// Slashes
$slash = "\\";

// Allowed Extensions
$ext = Array(
 1 => "jpg",
 2 => "jpeg",
 3 => "gif",
 4 => "bmp",
 5 => "png"
);

// Delete File
$pathinfo = pathinfo($baseDir.$slash.$_GET['delete']);
if($_GET['delete'] && array_search($pathinfo['extension'], $ext)) {
 if(unlink($baseDir.$slash.$_GET['delete'])) {
  echo "<font color=\"blue\">".$_GET['delete']." was deleted</font><br>\n";
 } else {
  echo "<font color=\"red\">".$_GET['delete']." could not be deleted</font><br>\n";
 }
}

// Get Directory
$dir = dir($baseDir);

// Start Table
echo "<table>\n";

// Start Array
while(false !== ($data = $dir->read())) {
 $pathinfo = pathinfo($baseDir.$slash.$data);
 if(array_search($pathinfo['extension'], $ext) && !is_dir($baseDir.$slash.$data)) {
  echo "<tr><td>".$data."</td><td><a href=\"".$base_file."?delete=".$data."\">Delete</a></td></tr>\n";
 }
}

// End Table
echo "</table>\n";
  1. //Base Directory
  2. $baseDir = getcwd()."\\images\\";
  3. // Slashes
  4. $slash = "\\";
  5. // Allowed Extensions
  6. $ext = Array(
  7.  1 => "jpg",
  8.  2 => "jpeg",
  9.  3 => "gif",
  10.  4 => "bmp",
  11.  5 => "png"
  12. );
  13. // Delete File
  14. $pathinfo = pathinfo($baseDir.$slash.$_GET['delete']);
  15. if($_GET['delete'] && array_search($pathinfo['extension'], $ext)) {
  16.  if(unlink($baseDir.$slash.$_GET['delete'])) {
  17.   echo "<font color=\"blue\">".$_GET['delete']." was deleted</font><br>\n";
  18.  } else {
  19.   echo "<font color=\"red\">".$_GET['delete']." could not be deleted</font><br>\n";
  20.  }
  21. }
  22. // Get Directory
  23. $dir = dir($baseDir);
  24. // Start Table
  25. echo "<table>\n";
  26. // Start Array
  27. while(false !== ($data = $dir->read())) {
  28.  $pathinfo = pathinfo($baseDir.$slash.$data);
  29.  if(array_search($pathinfo['extension'], $ext) && !is_dir($baseDir.$slash.$data)) {
  30.   echo "<tr><td>".$data."</td><td><a href=\"".$base_file."?delete=".$data."\">Delete</a></td></tr>\n";
  31.  }
  32. }
  33. // End Table
  34. echo "</table>\n";


it's basic, not very secure, and i'm sure you would be able to style it yourself...
  • ace5p1d0r
  • Expert
  • Expert
  • User avatar
  • Posts: 630
  • Loc: UK

Post 3+ Months Ago

Thanks that works great, except it does not display the image. Only the filename.
  • Vincent
  • Expert
  • Expert
  • User avatar
  • Posts: 721
  • Loc: Brisbane, Australia

Post 3+ Months Ago

ok... if you want the image as well...

Code: [ Select ]
//Base Directory
$baseDir = getcwd()."\\images\\";

// Slashes
$slash = "\\";

// Allowed Extensions
$ext = Array(
 1 => "jpg",
 2 => "jpeg",
 3 => "gif",
 4 => "bmp",
 5 => "png"
);

// Delete File
$pathinfo = pathinfo($baseDir.$slash.$_GET['delete']);
if($_GET['delete'] && array_search($pathinfo['extension'], $ext)) {
 if(unlink($baseDir.$slash.$_GET['delete'])) {
  echo "<font color=\"blue\">".$_GET['delete']." was deleted</font><br>\n";
 } else {
  echo "<font color=\"red\">".$_GET['delete']." could not be deleted</font><br>\n";
 }
}

// Get Directory
$dir = dir($baseDir);

// Start Table
echo "<table>\n";

// Start Array
while(false !== ($data = $dir->read())) {
 $pathinfo = pathinfo($baseDir.$slash.$data);
 if(array_search($pathinfo['extension'], $ext) && !is_dir($baseDir.$slash.$data)) {
  echo "<tr><td align=\"center\"><img src=\""$baseDir.$slash.$data."\"><br>".$data."</td><td><a href=\"".$base_file."?delete=".$data."\">Delete</a></td></tr>\n";
 }
}

// End Table
echo "</table>\n";
  1. //Base Directory
  2. $baseDir = getcwd()."\\images\\";
  3. // Slashes
  4. $slash = "\\";
  5. // Allowed Extensions
  6. $ext = Array(
  7.  1 => "jpg",
  8.  2 => "jpeg",
  9.  3 => "gif",
  10.  4 => "bmp",
  11.  5 => "png"
  12. );
  13. // Delete File
  14. $pathinfo = pathinfo($baseDir.$slash.$_GET['delete']);
  15. if($_GET['delete'] && array_search($pathinfo['extension'], $ext)) {
  16.  if(unlink($baseDir.$slash.$_GET['delete'])) {
  17.   echo "<font color=\"blue\">".$_GET['delete']." was deleted</font><br>\n";
  18.  } else {
  19.   echo "<font color=\"red\">".$_GET['delete']." could not be deleted</font><br>\n";
  20.  }
  21. }
  22. // Get Directory
  23. $dir = dir($baseDir);
  24. // Start Table
  25. echo "<table>\n";
  26. // Start Array
  27. while(false !== ($data = $dir->read())) {
  28.  $pathinfo = pathinfo($baseDir.$slash.$data);
  29.  if(array_search($pathinfo['extension'], $ext) && !is_dir($baseDir.$slash.$data)) {
  30.   echo "<tr><td align=\"center\"><img src=\""$baseDir.$slash.$data."\"><br>".$data."</td><td><a href=\"".$base_file."?delete=".$data."\">Delete</a></td></tr>\n";
  31.  }
  32. }
  33. // End Table
  34. echo "</table>\n";


and again, the styling of this whole thing is up to you...
  • ace5p1d0r
  • Expert
  • Expert
  • User avatar
  • Posts: 630
  • Loc: UK

Post 3+ Months Ago

Thanks Vincent, but I get this error:
Quote:
Parse error: parse error, unexpected T_VARIABLE, expecting ',' or ';' in /home/mrimageh/public_html/test.php on line 37
  • Vincent
  • Expert
  • Expert
  • User avatar
  • Posts: 721
  • Loc: Brisbane, Australia

Post 3+ Months Ago

that sucks... one dot missing...

Code: [ Select ]
//Base Directory
$baseDir = getcwd()."\\images\\";

// Slashes
$slash = "\\";

// Allowed Extensions
$ext = Array(
 1 => "jpg",
 2 => "jpeg",
 3 => "gif",
 4 => "bmp",
 5 => "png"
);

// Delete File
$pathinfo = pathinfo($baseDir.$slash.$_GET['delete']);
if($_GET['delete'] && array_search($pathinfo['extension'], $ext)) {
 if(unlink($baseDir.$slash.$_GET['delete'])) {
  echo "<font color=\"blue\">".$_GET['delete']." was deleted</font><br>\n";
 } else {
  echo "<font color=\"red\">".$_GET['delete']." could not be deleted</font><br>\n";
 }
}

// Get Directory
$dir = dir($baseDir);

// Start Table
echo "<table>\n";

// Start Array
while(false !== ($data = $dir->read())) {
 $pathinfo = pathinfo($baseDir.$slash.$data);
 if(array_search($pathinfo['extension'], $ext) && !is_dir($baseDir.$slash.$data)) {
  echo "<tr><td align=\"center\"><img src=\"".$baseDir.$slash.$data."\"><br>".$data."</td><td><a href=\"".$base_file."?delete=".$data."\">Delete</a></td></tr>\n";
 }
}

// End Table
echo "</table>\n";
  1. //Base Directory
  2. $baseDir = getcwd()."\\images\\";
  3. // Slashes
  4. $slash = "\\";
  5. // Allowed Extensions
  6. $ext = Array(
  7.  1 => "jpg",
  8.  2 => "jpeg",
  9.  3 => "gif",
  10.  4 => "bmp",
  11.  5 => "png"
  12. );
  13. // Delete File
  14. $pathinfo = pathinfo($baseDir.$slash.$_GET['delete']);
  15. if($_GET['delete'] && array_search($pathinfo['extension'], $ext)) {
  16.  if(unlink($baseDir.$slash.$_GET['delete'])) {
  17.   echo "<font color=\"blue\">".$_GET['delete']." was deleted</font><br>\n";
  18.  } else {
  19.   echo "<font color=\"red\">".$_GET['delete']." could not be deleted</font><br>\n";
  20.  }
  21. }
  22. // Get Directory
  23. $dir = dir($baseDir);
  24. // Start Table
  25. echo "<table>\n";
  26. // Start Array
  27. while(false !== ($data = $dir->read())) {
  28.  $pathinfo = pathinfo($baseDir.$slash.$data);
  29.  if(array_search($pathinfo['extension'], $ext) && !is_dir($baseDir.$slash.$data)) {
  30.   echo "<tr><td align=\"center\"><img src=\"".$baseDir.$slash.$data."\"><br>".$data."</td><td><a href=\"".$base_file."?delete=".$data."\">Delete</a></td></tr>\n";
  31.  }
  32. }
  33. // End Table
  34. echo "</table>\n";
  • ace5p1d0r
  • Expert
  • Expert
  • User avatar
  • Posts: 630
  • Loc: UK

Post 3+ Months Ago

OK two errors:

It seems to work, there are image placeholders, but no images. When I view the 'location' of the image, i get folders added like this:
Quote:
http://www.mrimagehost.com/home/mrimageh/public_html/uploads/%5C3001green_3Q4C1t.gif


Also, when deleting an image, this is the error I recieve:
Quote:
Warning: unlink(/home/mrimageh/public_html/uploads/\3001green_3Q4C1t.gif): No such file or directory in /home/mrimageh/public_html/test.php on line 20


Another note: I replaced \\images\\ with /uploads/ is that OK?
  • Vincent
  • Expert
  • Expert
  • User avatar
  • Posts: 721
  • Loc: Brisbane, Australia

Post 3+ Months Ago

ace5p1d0r wrote:
Another note: I replaced \\images\\ with /uploads/ is that OK?


the only reason that the slashes can be differed is because of the difference between how windows and linux works. In this case it looks ok to me... except for the second example - uploads/\3001green_3Q4C1t.gif - the variable $slash needs to be changed to "/"
  • ace5p1d0r
  • Expert
  • Expert
  • User avatar
  • Posts: 630
  • Loc: UK

Post 3+ Months Ago

OK fixed that problem.

but the images still dont display. For example, the script thinks an image is stored here:
Quote:
http://www.mrimagehost.com/home/mrimageh/public_html//uploads////3001green_3Q4C1t.gif


When its actually stored:
Quote:
http://www.mrimagehost.com/uploads/3001green_3Q4C1t.gif


:(
  • Vincent
  • Expert
  • Expert
  • User avatar
  • Posts: 721
  • Loc: Brisbane, Australia

Post 3+ Months Ago

for your case, replace the existing part of the code with this:-
Code: [ Select ]
// Start Array
while(false !== ($data = $dir->read())) {
 $pathinfo = pathinfo($baseDir.$slash.$data);
 if(array_search($pathinfo['extension'], $ext) && !is_dir($baseDir.$slash.$data)) {
  echo "<tr><td align=\"center\"><img src=\"/upload/".$data."\"><br>".$data."</td><td><a href=\"".$base_file."?delete=".$data."\">Delete</a></td></tr>\n";
 }
}
  1. // Start Array
  2. while(false !== ($data = $dir->read())) {
  3.  $pathinfo = pathinfo($baseDir.$slash.$data);
  4.  if(array_search($pathinfo['extension'], $ext) && !is_dir($baseDir.$slash.$data)) {
  5.   echo "<tr><td align=\"center\"><img src=\"/upload/".$data."\"><br>".$data."</td><td><a href=\"".$base_file."?delete=".$data."\">Delete</a></td></tr>\n";
  6.  }
  7. }
  • ace5p1d0r
  • Expert
  • Expert
  • User avatar
  • Posts: 630
  • Loc: UK

Post 3+ Months Ago

That works great :)

One more question - how would I restrict the size of the images?

Thanks so much for your help Vincent.
  • Vincent
  • Expert
  • Expert
  • User avatar
  • Posts: 721
  • Loc: Brisbane, Australia

Post 3+ Months Ago

This script will need GD enabled

Code: [ Select ]
<?php
/**
* Code By PhsyckBoy
*
* Resizes a gif, jpeg, or png image and saves it to a new file. Aspect ratio will be preserved.
* Requires GD
*
* @param srcFilePath Path the file to be resized
* @param destFilePath Path where new file will be saved
* @param maxWidth Maximum width of new image
* @param maxHeight Maximum height of new image
* @param quality Quality of new image (1-100), only matters for jpegs
*
* @return True/False indicating whether or not operation succeeded.
*/
function createResizedImage($srcFilePath, $destFilePath, $maxWidth, $maxHeight, $quality) {
if (!file_exists($srcFilePath)) return false;

$imgSize = getimagesize($srcFilePath);
$imgWidth = $imgSize[0];
$imgHeight = $imgSize[1];
$imgType = $imgSize[2];

switch ($imgType) {
case IMAGETYPE_GIF:
$srcImg = imagecreatefromgif($srcFilePath);
break;

case IMAGETYPE_JPEG:
$srcImg = imagecreatefromjpeg($srcFilePath);
break;

case IMAGETYPE_PNG:
$srcImg = imagecreatefrompng($srcFilePath);
break;

default:
return false;
}

//-- Find actual sizes to be used
if ($imgWidth - $maxWidth >= $imgHeight - $maxHeight) {
$newWidth = $maxWidth;
$newHeight = ($maxWidth / $imgWidth) * $imgHeight;
}
else {
$newWidth = ($maxHeight / $imgHeight) * $imgWidth;
$newHeight = $maxHeight;
}

if ($imgType == IMAGETYPE_GIF) $destImg = imagecreate($newWidth, $newHeight);
else $destImg = imagecreatetruecolor($newWidth, $newHeight);

imagecopyresampled($destImg, $srcImg, 0, 0, 0, 0, $newWidth, $newHeight, $imgWidth, $imgHeight);

switch ($imgType) {
case IMAGETYPE_GIF:
if (!imagegif($destImg, $destFilePath))
throw new Exception('Could not output gif to file');
break;

case IMAGETYPE_JPEG:
if (!imagejpeg($destImg, $destFilePath, $quality))
throw new Exception('Could not output jpeg to file');
break;

case IMAGETYPE_PNG:
if (!imagepng($destImg, $destFilePath))
throw new Exception('Could not output png to file');
break;
}

imagedestroy($destImg);
imagedestroy($srcImg);

return true;
}
?>
  1. <?php
  2. /**
  3. * Code By PhsyckBoy
  4. *
  5. * Resizes a gif, jpeg, or png image and saves it to a new file. Aspect ratio will be preserved.
  6. * Requires GD
  7. *
  8. * @param srcFilePath Path the file to be resized
  9. * @param destFilePath Path where new file will be saved
  10. * @param maxWidth Maximum width of new image
  11. * @param maxHeight Maximum height of new image
  12. * @param quality Quality of new image (1-100), only matters for jpegs
  13. *
  14. * @return True/False indicating whether or not operation succeeded.
  15. */
  16. function createResizedImage($srcFilePath, $destFilePath, $maxWidth, $maxHeight, $quality) {
  17. if (!file_exists($srcFilePath)) return false;
  18. $imgSize = getimagesize($srcFilePath);
  19. $imgWidth = $imgSize[0];
  20. $imgHeight = $imgSize[1];
  21. $imgType = $imgSize[2];
  22. switch ($imgType) {
  23. case IMAGETYPE_GIF:
  24. $srcImg = imagecreatefromgif($srcFilePath);
  25. break;
  26. case IMAGETYPE_JPEG:
  27. $srcImg = imagecreatefromjpeg($srcFilePath);
  28. break;
  29. case IMAGETYPE_PNG:
  30. $srcImg = imagecreatefrompng($srcFilePath);
  31. break;
  32. default:
  33. return false;
  34. }
  35. //-- Find actual sizes to be used
  36. if ($imgWidth - $maxWidth >= $imgHeight - $maxHeight) {
  37. $newWidth = $maxWidth;
  38. $newHeight = ($maxWidth / $imgWidth) * $imgHeight;
  39. }
  40. else {
  41. $newWidth = ($maxHeight / $imgHeight) * $imgWidth;
  42. $newHeight = $maxHeight;
  43. }
  44. if ($imgType == IMAGETYPE_GIF) $destImg = imagecreate($newWidth, $newHeight);
  45. else $destImg = imagecreatetruecolor($newWidth, $newHeight);
  46. imagecopyresampled($destImg, $srcImg, 0, 0, 0, 0, $newWidth, $newHeight, $imgWidth, $imgHeight);
  47. switch ($imgType) {
  48. case IMAGETYPE_GIF:
  49. if (!imagegif($destImg, $destFilePath))
  50. throw new Exception('Could not output gif to file');
  51. break;
  52. case IMAGETYPE_JPEG:
  53. if (!imagejpeg($destImg, $destFilePath, $quality))
  54. throw new Exception('Could not output jpeg to file');
  55. break;
  56. case IMAGETYPE_PNG:
  57. if (!imagepng($destImg, $destFilePath))
  58. throw new Exception('Could not output png to file');
  59. break;
  60. }
  61. imagedestroy($destImg);
  62. imagedestroy($srcImg);
  63. return true;
  64. }
  65. ?>
  • qakbar
  • Born
  • Born
  • qakbar
  • Posts: 1

Post 3+ Months Ago

Hello,

I am having problems with this script.

It does not display the images.

Do you have any solutions please.

the code i have is:
PHP Code: [ Select ]
<?php
 
//Base Directory
$baseDir = getcwd()."\\../imagefolder\\";
 
// Slashes
$slash = "\\";
 
// Allowed Extensions
$ext = Array(
 1 => "jpg",
 2 => "jpeg",
 3 => "gif",
 4 => "bmp",
 5 => "png"
);
 
// Delete File
$pathinfo = pathinfo($baseDir.$slash.$_GET['delete']);
if($_GET['delete'] && array_search($pathinfo['extension'], $ext)) {
 if(unlink($baseDir.$slash.$_GET['delete'])) {
  echo "<font color=\"blue\">".$_GET['delete']." was deleted</font><br>\n";
 } else {
  echo "<font color=\"red\">".$_GET['delete']." could not be deleted</font><br>\n";
 }
}
 
// Get Directory
$dir = dir($baseDir);
 
// Start Table
echo "<table>\n";
 
// Start Array
while(false !== ($data = $dir->read())) {
 $pathinfo = pathinfo($baseDir.$slash.$data);
 if(array_search($pathinfo['extension'], $ext) && !is_dir($baseDir.$slash.$data)) {
  echo "<tr><td align=\"center\"><img src=\"".$baseDir.$slash.$data."\"><br>".$data."</td><td><a href=\"".$base_file."?delete=".$data."\">Delete</a></td></tr>\n";
 }
}
 
// End Table
echo "</table>\n";
?>
  1. <?php
  2.  
  3. //Base Directory
  4. $baseDir = getcwd()."\\../imagefolder\\";
  5.  
  6. // Slashes
  7. $slash = "\\";
  8.  
  9. // Allowed Extensions
  10. $ext = Array(
  11.  1 => "jpg",
  12.  2 => "jpeg",
  13.  3 => "gif",
  14.  4 => "bmp",
  15.  5 => "png"
  16. );
  17.  
  18. // Delete File
  19. $pathinfo = pathinfo($baseDir.$slash.$_GET['delete']);
  20. if($_GET['delete'] && array_search($pathinfo['extension'], $ext)) {
  21.  if(unlink($baseDir.$slash.$_GET['delete'])) {
  22.   echo "<font color=\"blue\">".$_GET['delete']." was deleted</font><br>\n";
  23.  } else {
  24.   echo "<font color=\"red\">".$_GET['delete']." could not be deleted</font><br>\n";
  25.  }
  26. }
  27.  
  28. // Get Directory
  29. $dir = dir($baseDir);
  30.  
  31. // Start Table
  32. echo "<table>\n";
  33.  
  34. // Start Array
  35. while(false !== ($data = $dir->read())) {
  36.  $pathinfo = pathinfo($baseDir.$slash.$data);
  37.  if(array_search($pathinfo['extension'], $ext) && !is_dir($baseDir.$slash.$data)) {
  38.   echo "<tr><td align=\"center\"><img src=\"".$baseDir.$slash.$data."\"><br>".$data."</td><td><a href=\"".$base_file."?delete=".$data."\">Delete</a></td></tr>\n";
  39.  }
  40. }
  41.  
  42. // End Table
  43. echo "</table>\n";
  44. ?>

Post Information

  • Total Posts in this topic: 13 posts
  • Users browsing this forum: ScottG, wpas and 102 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.