Image upload + Auto Increment

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
josephman1988
Forum Newbie
Posts: 15
Joined: Mon Apr 21, 2008 8:29 am

Image upload + Auto Increment

Post by josephman1988 »

Hi,

I have the following EXTREMELY simple upload script, as i'm learning still.

Code: Select all

if($_FILES["img1"] !="") {
    $uploaddir = "css/";
    
        if (move_uploaded_file($_FILES["img1"]["tmp_name"], $uploaddir . $_FILES["img1"]["name"])) {
        echo("file uploaded");
        } else {
          echo ("error!");
     }
}
As it is a restricted form, and as i did say, still learning, the security issues will be fixed up later.

Basically, i want to rename the names of the images so their name resembles something like:

001.png
002.png
003.png
004.png
005.png

Each time I upload, the next image will be
006.png
etc.

A mySQL resolution would be IDEAL.

Thanks in advanced.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Image upload + Auto Increment

Post by jaoudestudios »

create a table for the images that has an id column that auto increments on insert.

Then use that new id to rename the file (or should I say move the file from temp location on the upload to new location with new name);

So your move line will become:
move_uploaded_file($_FILES["img1"]["tmp_name"], $uploaddir . mysql_insert_id());

The mysql_insert_id() returns the last auto increment (in this case id) that was created by the previous insert statement.
josephman1988
Forum Newbie
Posts: 15
Joined: Mon Apr 21, 2008 8:29 am

Re: Image upload + Auto Increment

Post by josephman1988 »

That helped a bunch =]

However, each time I upload another file using the same form, it gets overwritten in both the mysql db, and upload directory.

How would i make it, so if it already exists the number would increase and not overwrite the previous inserted file.

Thanks again.
RafaelLopez
Forum Newbie
Posts: 6
Joined: Wed Jul 02, 2008 4:24 pm

Re: Image upload + Auto Increment

Post by RafaelLopez »

Column "id" in your MySQL table should be "auto-increment", so each time you add a new entry it uses the next integer in sequence, 1, 2, 3, 4, 5 ... 9 billion, 9 billion and 1, 9 billion and 2... :wink:

You must have forgotten to set that when you created your table.

But Jaoude, wouldn't your solution strip the images of the desirable ".png" extension?
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Image upload + Auto Increment

Post by jayshields »

Yes, his solution would strip the extension.

To find a suitable filename just do something like:

Code: Select all

 
$x = 0;
$tmp = explode('.', $_FILES['img1']['name']);
while(file_exists($uploaddir . $x . '.' . $tmp[1]))
  $x++;
Then change your move_uploaded_file() call to:

Code: Select all

 
move_uploaded_file($_FILES['img1']['tmp_name'], $uploaddir . $x . '.' . $tmp[1])
 
You don't need a database for something like this.
josephman1988
Forum Newbie
Posts: 15
Joined: Mon Apr 21, 2008 8:29 am

Re: Image upload + Auto Increment

Post by josephman1988 »

shields, that helped tremendously. Thanks.

The reason I wanted to use a DB for this, is because once uploaded, it will automatically be displayed on a page. I guess you could call it a gallery type of thing.

All tables related so I can pull all data from a paticular 'category' (Ie. Images, details, name, blah blah).

With that in mind, how would i store the filename in the DB
josephman1988
Forum Newbie
Posts: 15
Joined: Mon Apr 21, 2008 8:29 am

Re: Image upload + Auto Increment

Post by josephman1988 »

Cant we use variables in a INSERT query?
I was thinking of inserting the information as follows:

Code: Select all

$sql = mysql_query("INSERT INTO Images (id, Name, Date, TitlesId) VALUES ($x, $x, $CURDATE(), 1") ;
No errors are produced when put into practice though, but no data is inserted.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Image upload + Auto Increment

Post by jaoudestudios »

Yes you can, it would go like this...

Code: Select all

 
$sql = mysql_query("INSERT INTO Images (id, Name, Date, TitlesId) VALUES (".$x.", ".$x.", ".$CURDATE().", 1") ;
 
But you should be more specific in your queries as this method allows room for errors, this would be better...

Code: Select all

 
$sql = mysql_query("INSERT INTO images SET id = NULL, name = '".$x."', date = '".curdate()."', titlesid = '1'");
 
With the date I would recommend using unix time stamp. So instead of your curdate() function use the php function time()
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Image upload + Auto Increment

Post by jayshields »

You don't need a database to display images. Things can get messy when associating image descriptions though.

Your original query was fine, except use NOW() instead of $CURDATE(). You don't need to precede MySQL functions with dollar symbols.
Post Reply