Recent Topics

1 Mar 30, 2005 08:18    

I'm mystified that blogging tools seem to put all the pictures in one directory. I've had my b2evolution blog for a week or so and they're already... let's see... 35 pictures in the media directory. So, I just hacked ./b2/blogs/admin/b2upload.php to put files in ./b2/blogs/media/2005/03/29/pic.jpg as an example.

Here's the new file:

 * b2 File Upload
 * b2evolution - {@link}
 * Released under GNU GPL License - {@link}
 * @copyright (c)2003-2004 by Francois PLANQUE - {@link}
 * @package admin
 * @author original hack by

 * Includes:
require_once (dirname(__FILE__).'/_header.php');

// Check permissions:
$current_User->check_perm( 'upload', 'any', true );

?><html xml:lang="<?php locale_lang() ?>" lang="<?php locale_lang() ?>">
	<meta http-equiv="Content-Type" content="text/html; charset=<?php locale_charset() ?>" />
	<title><?php echo T_('b2evo') ?> &gt; <?php echo T_('upload images/files') ?></title>
	<link href="variation.css" rel="stylesheet" type="text/css" title="Variation" />
	<link href="desert.css" rel="alternate stylesheet" type="text/css" title="Desert" />
	<link href="legacy.css" rel="alternate stylesheet" type="text/css" title="Legacy" />
	<?php if( is_file( dirname(__FILE__).'/custom.css' ) ) { ?>
	<link href="custom.css" rel="alternate stylesheet" type="text/css" title="Custom" />
	<?php } ?>
	<script type="text/javascript" src="styleswitcher.js"></script>
	<script type="text/javascript">
	<!-- //'s popup tutorial rules !
	function targetopener(blah, closeme, closeonly) {
		if (! (window.focus && window.opener))return true;
		if (! closeonly) += blah;
		if (closeme)window.close();
		return false;
<div class="panelblock">

if (!isset($_POST['submit']))
	$i = explode(" ",$fileupload_allowedtypes);
	$i = implode(", ",array_slice($i, 1, count($i)-2));
	<p><strong><?php echo T_('File upload') ?></strong></p>
	<p><?php echo T_('Allowed file types:'), $i ?></p>
	<p><?php printf( T_('Maximum allowed file size: %d KB'), $fileupload_maxk ); ?></p>
	<form action="b2upload.php" method="post" enctype="multipart/form-data">
	<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $fileupload_maxk*1024 ?>" />
	<input type="file" name="img1" size="30" class="uploadform" />

	<p><?php echo T_('Description') ?>:<br />
	<input type="text" name="imgdesc" size="30" class="uploadform" /></p>
	<input type="submit" name="submit" value="<?php echo T_('Upload !') ?>" class="search" />
<?php exit();

 //Makes sure they choose a file


if (!empty($HTTP_POST_VARS)) { //$img1_name != "") {

	$imgalt = (isset($HTTP_POST_VARS['imgalt'])) ? $HTTP_POST_VARS['imgalt'] : '';

	$img1_name = (strlen($imgalt)) ? $HTTP_POST_VARS['imgalt'] : $HTTP_POST_FILES['img1']['name'];
	$img1_type = (strlen($imgalt)) ? $HTTP_POST_VARS['img1_type'] : $HTTP_POST_FILES['img1']['type'];
	$img1_size = (strlen($imgalt)) ? $HTTP_POST_VARS['img1_size'] : $HTTP_POST_FILES['img1']['size'];
	$imgdesc = str_replace('"', '&amp;quot;', $HTTP_POST_VARS['imgdesc']);

	$imgtype = explode(".",$img1_name);
	$imgtype = " ".$imgtype[count($imgtype)-1]." ";

	if (!ereg(strtolower($imgtype), strtolower($fileupload_allowedtypes))) {
	    die(sprintf( T_('File %s: type %s is not allowed.'), $img1_name, $imgtype ));

	// we're going to append login_ to the image name
	// for some reason image alt is used for naming sometimes... stupid
	// this sometimes adds the name twice... fix later... trident now.
	// $img1_name=$current_User->get('login')."_".$img1_name;

	// and now we create a reasonable directory structure for uploaded files
	// they go in /media/yyyy/mm/dd/
	// it is possible that you could end up with the wrong date this way.
	// if date got called on two different days...

		mkdir ($fileupload_realpath."/".$year, 0755);
		mkdir ($fileupload_realpath."/".$year."/".$month, 0755);
		mkdir ($fileupload_realpath."/".$year."/".$month."/".$day, 0755);

	if (strlen($imgalt)) {
		$pathtofile = $fileupload_realpath."/".$imgalt;
		$img1 = $HTTP_POST_VARS['img1'];
	} else {
		$pathtofile = $fileupload_realpath."/".$img1_name;
		$img1 = $HTTP_POST_FILES['img1']['tmp_name'];

	// makes sure not to upload duplicates, rename duplicates
	$i = 1;
	$pathtofile2 = $pathtofile;
	$tmppathtofile = $pathtofile2;

	$img2_name = $img1_name;

	while (file_exists($pathtofile2)) {
	    $pos = strpos($tmppathtofile, '.'.trim($imgtype));
	    $pathtofile_start = substr($tmppathtofile, 0, $pos);
	    $pathtofile2 = $pathtofile_start.'_'.zeroise($i++, 2).'.'.trim($imgtype);
	    $img2_name = explode('/', $pathtofile2);
	    $img2_name = $img2_name[count($img2_name)-1];

	if (file_exists($pathtofile) && !strlen($imgalt)) {
		$i = explode(" ",$fileupload_allowedtypes);
		$i = implode(", ",array_slice($i, 1, count($i)-2));
		move_uploaded_file($img1, $pathtofile2) 
		 or die( T_('Couldn\'t upload your file to:').' '.$pathtofile2);
	// duplicate-renaming function contributed by Gary Lawrence Murphy
	<p><strong><?php echo T_('Duplicate File?') ?></strong></p>
	<p><strong><em><?php printf( T_('The filename "%s" already exists!'), $img1_name ); ?></em></strong></p>
	<p><?php printf( T_('Filename "%s" moved to "%s"'), $img1, $pathtofile2 ); ?></p>
	<p><?php echo T_('Confirm or rename:') ?></p>
	<form action="b2upload.php" method="post" enctype="multipart/form-data">
	<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $fileupload_maxk*1024 ?>" />
	<input type="hidden" name="img1_type" value="<?php echo $img1_type;?>" />
	<input type="hidden" name="img1_size" value="<?php echo $img1_size;?>" />
	<input type="hidden" name="img1_name" value="<?php echo $img2_name;?>" />
	<input type="hidden" name="img1" value="<?php echo $pathtofile2;?>" />
	<?php echo T_('Alternate name') ?>:<br /><input type="text" name="imgalt" size="30" class="uploadform" value="<?php echo $img2_name;?>" /><br />
	<br />
	<?php echo T_('Description') ?>:<br /><input type="text" name="imgdesc" size="30" class="uploadform" value="<?php echo $imgdesc;?>" />
	<br />
	<p><input type="submit" name="submit" value="<?php echo T_('Confirm !') ?>" class="search" /></p>
</html><?php die();


	if (!strlen($imgalt)) {
		move_uploaded_file($img1, $pathtofile) //Path to your images directory, chmod the dir to 777
		 or die( T_('Couldn\'t upload your file to:').' '.$pathtofile);
	} else {
		rename($img1, $pathtofile)
		or die( T_('Couldn\'t upload your file to:').' '.$pathtofile);


if ( ereg('image/',$img1_type)) {
	$piece_of_code = "&lt;img src=&quot;$fileupload_url/$img1_name&quot; border=&quot;0&quot; alt=&quot;$imgdesc&quot; /&gt;"; 
} else {
	$piece_of_code = "&lt;a href=&quot;$fileupload_url/$img1_name&quot; title=&quot;$imgdesc&quot;&gt;$imgdesc&lt;/a&gt;"; 


<p><strong><?php echo T_('File uploaded !') ?></strong></p>
<p><?php printf( T_('Your file <strong>"%s"</strong> was uploaded successfully !'), $img1_name ); ?></p>
<p><?php echo T_('Here\'s the code to display it:') ?></p>
<p><form action="b2upload.php">
<!--<textarea cols="25" rows="3" wrap="virtual"><?php echo "&lt;img src=&quot;$fileupload_url/$img1_name&quot; border=&quot;0&quot; alt=&quot;&quot; /&gt;"; ?></textarea>-->
<input type="text" name="imgpath" value="<?php echo $piece_of_code; ?>" size="40" class="large" /><br />
<input type="button" name="close" value="<?php echo T_('Add the code to your post !') ?>" class="search" onclick="targetopener('<?php echo $piece_of_code; ?>')" />
<p><strong><?php echo T_('Image Details') ?></strong>: <br />
<?php echo T_('Name') ?>: 
<?php echo "$img1_name"; ?>
<br />
<?php echo T_('Size') ?>: 
<?php echo round($img1_size/1024,2); ?> KB
<br />
<?php echo T_('Type') ?>: 
<?php echo "$img1_type"; ?>
<p align="right">
<form action="b2upload.php">
<input type="button" name="close" value="<?php echo T_('Close this window') ?>" class="search" onclick="window.close()" />

2 Mar 30, 2005 13:51

Thanks, this looks very useful.
What immediate benefits does this give apart from being far more organised.

3 Mar 30, 2005 21:24

If someone is running a window file system there's a limit on the number of files in a directory.

If someone foolishly types ls in the media directory on a laggy connection it won't take 10 minutes to kill

There aren't going to be duplicate name conflicts nearly as often... Every time the battery on my digital camera dies it starts numbering at DSCF001.jpg again, so I have countless DSCF001.jpg s in my blog.

And this way, the image hierarchy looks like it does for the posts... at least the way the externally available structure looks.

That's all I can think of off the top of my head. Basically it's just the right way to do this.

4 Apr 01, 2005 15:19

jlackey,it works a treat on my older 09 version.

Just what I needed, thanks

5 Apr 14, 2005 21:11

Thanks, this is really nice!

However after adding it, I've noticed that I can not delete any of the pictures stored in Y/M/D folders.

I would guess it has to do with the chmod of 755, where the orginial code was 777. I tried changing it to 777, but that didn't work. I'm not totally familiar with permisions so I'm not sure whats going on here.

How can I delete files from these folders and prevent this from happening again?


6 Apr 18, 2005 15:20

Good question as it's the samefor me, look forward to a solution

7 Apr 21, 2005 22:07

Well, im sure this isnt correct, but this is how i got mine working:


/*	I had to run this code once to change the the rights on all my current folders that were already created.  I'm sure there's probably an easier way to read a folder structure and do them all in a loop, but I'm not sure how. 

   chmod ($fileupload_realpath."/2005/04/13",0777);
   chmod ($fileupload_realpath."/2005/04/14",0777);
   chmod ($fileupload_realpath."/2005/04",0777);
   chmod ($fileupload_realpath."/2005",0777);
      mkdir ($fileupload_realpath."/".$year, 0777); 
	  chmod ($fileupload_realpath."/".$year, 0777); 
      mkdir ($fileupload_realpath."/".$year."/".$month, 0777); 
	  chmod ($fileupload_realpath."/".$year."/".$month, 0777); 
      mkdir ($fileupload_realpath."/".$year."/".$month."/".$day, 0777); 
	  chmod ($fileupload_realpath."/".$year."/".$month."/".$day, 0777); 

You'll notice when it makes a folder i then added the chmod command even though the mkdir command should already set it, but in my experience that wasn't working.

I hope this helps, and someone comes up with a better solution! :)

8 Apr 21, 2005 22:44

If you change all the 0755s to 0777 then every user will have write permission.

The way your server ought (not as in likely, but correct) to be set up is to have php run by someone other than you. That way if something goes wrong with your b2 install, horrible things won't happen to your files.

Of course, if you are running b2 off some hosting place, they might force you to run b2 as yourself... in that case you'll need to set the permissions as 777.

Magic: the way you chose to change permissions is strange and hard. If you have shell access typing something like
find ./media -exec chmod 777 {} \;
would do it.

Continuing with the act of unix demystification... the first number is the user permssion 1+2+4=7. The second is group permission. The third is everyone else.

Form is loading...

Photo gallery software – This forum is powered by b2evolution CMS, a complete engine for your website.