...

View Full Version : help with image upload needed



waynesimmonds
01-31-2007, 12:38 PM
Hi,

I am trying to create an image upload that changes the name of the image i am uploading, from "example.jpg" to "photo1.jpg" so when i upload a new photo to the database it adds the id number onto the end of the name, so when i upload image 306, the name will be "photo306", hope this makes sence.

//database table//

album_id | p_id | p_name
- - - - - - - - - - - - - - -
_ _1 _ _ _ _ 1 _ _ image1.jpg
_ _1 _ _ _ _ 2 _ _ image2.jpg

- - - - - - - - - - - - - - -

p_id "stands for photo_id" = index key
p_name "stands for photo_name" = name of file

//end database//

I have created some code that uploads the image to the directory and places the name of the image into the database but im not sure how to change the name to "photo1" "photo2" and so on, instead of the original image name. Im not even sure if this can be done? I am a bit of a beginner to php and mysql so it all seems a bit tricky! lol

heres my code:




<?php
$host="localhost";
$username="root";
$password="";
$db_name="mydb";
$tbl_name="photos";
$id=$_GET['uid'];
$album_id=$_POST['txtaid'];

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
?>

<?php
if (isset($_POST['submit'])) {
if (is_uploaded_file($_FILES['image']['tmp_name'])){
$iname = $_POST['id'] . "_" . $_FILES['image']['name'];
$istring = "../../photos/thumbs/" . $iname;
if (move_uploaded_file($_FILES['image']['tmp_name'], $istring)){
echo'The File Has Been Uploaded';

$query ="INSERT into $tbl_name(album_id, p_name)VALUES('$album_id', '$iname')";

if ($result =@mysql_query ($query)) {
echo '<br>Successfull';

}else{
echo 'not added';
echo 'back to try again';
}
}else{
echo'sorry not worked';

$i="";
}

}else{
$i="";
}
}else{
?>



hope somebody can help, thanks for looking :thumbsup:

cheers

aedrin
01-31-2007, 10:08 PM
After you insert the record into the database. You get the ID of it this way:



$id = mysql_insert_id();


Then use rename() to rename the file:



rename($oldfile, 'photo' . $id . '.jpg');


Then of course don't forget to update the database with the new filename.

waynesimmonds
02-01-2007, 01:13 PM
Hi thanks for your reply.

I entered the code in as you said but i got a couple of errors.


i had an // Notice: Undefined variable: oldfile
and // Warning: rename(,photo0.jpg): Permission denied

I think i have probably entered the code in wrong but am not sure :confused:



<?php
if (isset($_POST['submit'])) {
if (is_uploaded_file($_FILES['image']['tmp_name'])){
$iname = $_POST['id'] . "_" . $_FILES['image']['name'];
$istring = "../../photos/thumbs/" . $iname;
if (move_uploaded_file($_FILES['image']['tmp_name'], $istring)){//move the file over.

echo'The File Has Been Uploaded';


$query ="INSERT into $tbl_name(album_id, p_name)VALUES('$album_id', '$iname')";

$id = mysql_insert_id();
rename($oldfile, 'photo' . $id . '.jpg');
$query ="UPDATE $tbl_name SET p_name='' where p_id='$id'";



if ($result =@mysql_query ($query)) {//worked
echo '<br>Successfull';




sorry for this but im a beginner and its taken me a while to get used to it and seeing what im missing.

cheers for help!!! :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum