Page 1 of 1
Image upload + Auto Increment
Posted: Wed Jul 02, 2008 4:52 am
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.
Re: Image upload + Auto Increment
Posted: Wed Jul 02, 2008 7:54 am
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.
Re: Image upload + Auto Increment
Posted: Wed Jul 02, 2008 3:54 pm
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.
Re: Image upload + Auto Increment
Posted: Wed Jul 02, 2008 4:40 pm
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...
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?
Re: Image upload + Auto Increment
Posted: Wed Jul 02, 2008 5:34 pm
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.
Re: Image upload + Auto Increment
Posted: Thu Jul 03, 2008 5:57 am
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
Re: Image upload + Auto Increment
Posted: Fri Jul 04, 2008 8:46 am
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.
Re: Image upload + Auto Increment
Posted: Fri Jul 04, 2008 2:52 pm
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()
Re: Image upload + Auto Increment
Posted: Sat Jul 05, 2008 6:28 am
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.