Image uploader

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Darkside
Forum Commoner
Posts: 43
Joined: Wed Oct 30, 2002 4:18 pm

Image uploader

Post by Darkside »

I had this code:

Code: Select all

<?php 
function getNextSerial($prefix, $path) 
&#123; 
   $max = -1; 
   $dir = opendir($path); 
   while($fname = readdir($dir)) 
   &#123; 
      if(strpos($fname, $prefix) === (int)0) 
         $max = max($max, (int)substr($fname, strlen($prefix))); 
   &#125; 
   return ++$max; 
&#125; 

$postfix = strrchr($_FILES&#1111;'filename']&#1111;'name'], '.'); 
$fname = '../../darkside/Upload/fubar' . getNextSerial('fubar', '../../darkside/Upload/fubar' ).$postfix; 

if(!(copy($_FILES&#1111;'userfile']&#1111;'tmp_name'], "$fname" . ".jpg")));

$post_data = "posts.php";
$data = fopen($post_data, "a");
fwrite($data, "$fname");
fclose($data);
?>
I edited it so that from a form on a page you can upload an image to a folder and add stuff like a title and the users email etc..

But i want it so the the user can choose from 5 diffrent folders to upload the image to (from a drop down menu on the forum)

Code: Select all

$postfix = strrchr($_FILES&#1111;'filename']&#1111;'name'], '.'); 
$fname = ("$god").('fubar') . getNextSerial('fubar',"$god".$postfix);
I have used the base code and that code you see above along with some other stuff to make it all work the only problem is as you can see if the base code it sets the name to fubar0 and if there is fubar0 to make it to 1 then 2 however when i try to change my code to add the drop down menu results eg ../../darkside/Upload/fubar, ../../wr4ck/Upload/fubar. (this being $god)
the right hand code works

Code: Select all

$postfix = strrchr($_FILES&#1111;'filename']&#1111;'name'], '.'); 
$fname = ("$god").('fubar')
But the left hand doesnt

Code: Select all

getNextSerial('fubar',"$god".$postfix);
So when i run the post i end up finding the image in ../../darkside folder name fubar1Upload, fubar2Upload.

Ok then there is alot of code up and round there but its the only way this is going to make any sence. Any help wouldnt go a miss.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

take a look at the original example code (I knew this functioname sounds familiar ;) )
$postfix = strrchr($_FILES['filename']['name'], '.');
$fname = 'Upload/fubar' . getNextSerial('fubar', 'Upload').$postfix;
echo $fname;
The first parameter is only the static part of the filename, the second only the path. If you set error_reporting to E_ALL (and maybe display_errors to on) you will see that after your changes the function is unable to open the directory you've passed. I'm not fully awaken by now ;) but maybe

Code: Select all

$fname = $god.'fubar'.getNextSerial('fubar',$god).$postfix;
will work. Does $god have a trailing / ?
Darkside
Forum Commoner
Posts: 43
Joined: Wed Oct 30, 2002 4:18 pm

no

Post by Darkside »

no it doesnt

../../darkside/Upload
is an example..

please dont tell me all i need was
../../darkside/Upload/

ps: yeh that code should like very familiar :P

Crappeh it was as well its all working now :D
Post Reply