Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

changing file name of uploaded file to mysql record id

is there a way that after i've submitted a file that i could change it to the id of the mysql record which is auto increment.

so if it was for site 23 the header file would be renamed to header23.jpg, the name of the background file would be bg23.jpg and the name of the zipfile of pics would be piczip23.zip instead of the file name which i originally uploaded.
this is the code i have for after submit is clicked on the form.


<?
if($_POST['submit']) //If submit is hit
{
$dirheader = "headers";
$dirbackground = "backgrounds";
$dirpiczip = "piczips";

$destheader = $dirheader."/".$image_name['header'];
$destbackground = $dirbackground."/".$image_name['background'];
$destpiczip = $dirpiczip."/".$zip_name['photos'];

// upload file set
if(copy($image['header'], $destheader) && copy($image['background'], $destbackground)  && copy($zip['photos'], $destpiczip))
    {
        echo 'image set has been uploaded <p></p>';
    }else{
        echo 'image set DID NOT upload <p></p>';
    }


   mysql_connect("localhost","***","***");

   mysql_select_db("sitemaker");

  $sitename = $_POST['sitename'];
   $domain = $_POST['domain'];
$sitepath = $_POST['sitepath'];
   $category = $_POST['category'];
   $description = $_POST['description'];
  $siteurl = $_POST['siteurl'];

   $result=MYSQL_QUERY("INSERT INTO sites (id,sitename,domain,sitepath,category,description,header,bg,piczip,siteurl)".
      "VALUES ('NULL', '$sitename', '$domain', '$sitepath', '$category', '$description', '$destheader', '$destbackground', '$destpiczip', '$siteurl')");

   echo "Query Finished";
}



thanks for any help


Paul
0
paulp75
Asked:
paulp75
  • 8
  • 6
  • 6
2 Solutions
 
AlanJDMCommented:
mysql_insert_id($result) will qive you the id of the record inserted that you need to have to rename the files.

So, after your query...

$result=MYSQL_QUERY("INSERT INTO sites (id,sitename,domain,sitepath,category,description,header,bg,piczip,siteurl)"."VALUES ('NULL', '$sitename', '$domain', '$sitepath', '$category', '$description', '$destheader', '$destbackground', '$destpiczip', '$siteurl')");

do this...

$ID = mysql_insert_id($result);
rename($destheader, $dirheader."/header".$ID.".jpeg";);
rename($destbackground, $dirbackground."/background".$ID.".jpeg";);
rename($destpiczip, $dirpiczip."/piczip".$ID.".jpeg";);


Alan







0
 
AlanJDMCommented:
ooops, typed to fast. Should be....

$ID = mysql_insert_id($result);
rename($destheader, $dirheader."/header".$ID.".jpg");
rename($destbackground, $dirbackground."/background".$ID.".jpg");
rename($destpiczip, $dirpiczip."/piczip".$ID.".zip");
0
 
ldbkuttyCommented:
AlanJDM, but the filename would be same in the DB, right ?

how about this:

This code might be messy. you can use this if you wish.

<?
if($_POST['submit']) //If submit is hit
{

$result = mysql_query("SELECT id FROM yourtable ORDER BY id DESC LIMIT 1") or die("SQL Error : " . mysql_error());

$table_id = $myql_result($result, 0, "id");
$table_id = $table_id + 1;

$dirheader = "headers";
$dirbackground = "backgrounds";
$dirpiczip = "piczips";

$destheader = $dirheader."/".$image_name['header'];
$destbackground = $dirbackground."/".$image_name['background'];
$destpiczip = $dirpiczip."/".$zip_name['photos'];

$destheaderparts = explode(".", $destheader);
$dirbackgroundparts = explode(".", $destbackground);
$dirpiczipparts = explode(".", $destpiczip);

$destheader = $destheaderparts[0] . $table_id . '.' . $destheaderparts[1];
$dirbackground = $dirbackgroundparts[0] . $table_id . '.' . $dirbackgroundparts[1];
$dirpiczip = $dirpiczipparts[0] . $table_id . '.' . $dirpiczipparts[1];

// upload file set
if(copy($image['header'], $destheader) && copy($image['background'], $destbackground)  && copy($zip['photos'], $destpiczip))
    {
        echo 'image set has been uploaded <p></p>';
    }else{
        echo 'image set DID NOT upload <p></p>';
    }


   mysql_connect("localhost","***","***");

   mysql_select_db("sitemaker");

  $sitename = $_POST['sitename'];
   $domain = $_POST['domain'];
$sitepath = $_POST['sitepath'];
   $category = $_POST['category'];
   $description = $_POST['description'];
  $siteurl = $_POST['siteurl'];

   $result=MYSQL_QUERY("INSERT INTO sites (id,sitename,domain,sitepath,category,description,header,bg,piczip,siteurl)".
      "VALUES ('NULL', '$sitename', '$domain', '$sitepath', '$category', '$description', '$destheader', '$destbackground', '$destpiczip', '$siteurl')");

   echo "Query Finished";
}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AlanJDMCommented:
This is risky because if 2 people run the query at the same time you could get the wrong ID returned.

I would handle this by doing an update after renaming the files...

$ID = mysql_insert_id($result);
rename($destheader, $dirheader."/header".$ID.".jpg");
rename($destbackground, $dirbackground."/background".$ID.".jpg");
rename($destpiczip, $dirpiczip."/piczip".$ID.".zip");

$result=MYSQL_QUERY("UPDATE sites set header = ".$dirheader."/header".$ID.".jpg,
bg = ,".$dirbackground."/background".$ID.".jpg",piczip = ".$dirpiczip."/piczip".$ID.".jpg");


Alan
0
 
ldbkuttyCommented:
>> This is risky because if 2 people run the query at the same time you could get the wrong ID returned.
If that is risky, then:
>> $ID = mysql_insert_id($result);
is also risky.

But, what you proposed is a good idea. :-)
0
 
AlanJDMCommented:
No, mysql_insert_id() isnt risky because mysql cotrols the ID returned. If you do an insert and call mysgl_return_id() even after other records have been inserted from other scripts mysql ensures that the ID returned is the one from the last record inserted "from within that script".

From the manual...

"The value of mysql_insert_id() is affected only by statements issued within the current client connection. It is not affected by statements issued by other clients."



Alan
0
 
ldbkuttyCommented:
Ah... thats wonderful :-) . I Apologize.
0
 
AlanJDMCommented:
No need to appologize... just pointing out that there is no risk so the original poster won't be affraid to use it. In low volume applications your method is very low risk as 2 people would have to submit at virtually the same time, but it could happen. Better safe than sorry though, right?


Alan
0
 
paulp75Author Commented:
having all sorts of trouble with this

for the one you wrote ldkutty i'm getting this

Notice: Undefined variable: mysql_result in path/addsites.php on line 68
Fatal error: Call to undefined function: () in path/addsites.php on line 68

which is for this line

$table_id = $myql_result($result, 0, "id");
which i changed to
$table_id = $mysql_result($result, 0, "id");   after it first didnt work


and with alajdm's i'm getting parse error on this line

$result=MYSQL_QUERY("UPDATE sites set header = ".$dirheader."/header".$ID.".jpg",
bg = ".$dirbackground."/background".$ID.".jpg",piczip = ".$dirpiczip."/piczip".$ID.".jpg");

thanks for the help. still not working. but much appreciated though. anything to fix these?


0
 
ldbkuttyCommented:
my mistake:

$table_id = $mysql_result($result, 0, "id");

must be:

$table_id = mysql_result($result, 0, "id");
0
 
paulp75Author Commented:
nearly there i think
what that one did was rename the header to *filename*header9.jpg for the header.  which was good except it doesnt need the filename though. didnt effect the others though.
just uploaded their normal file name.
this is the code now


$result = mysql_query("SELECT id FROM sites ORDER BY id DESC LIMIT 1") or die("SQL Error : " . mysql_error());

$table_id = mysql_result($result, 0, "id");
$table_id = $table_id + 1;

$dirheader = "headers";
$dirbackground = "backgrounds";
$dirpiczip = "piczips";
$dirthumbzip = "thumbzips";

$destheader = $dirheader."/".$image_name['header'];
$destbackground = $dirbackground."/".$image_name['background'];
$destpiczip = $dirpiczip."/".$zip_name['photos'];
$destthumbzip = $dirthumbzip."/".$zip_name['thumbs'];

$destheaderparts = explode(".", $destheader);
$dirbackgroundparts = explode(".", $destbackground);
$dirpiczipparts = explode(".", $destpiczip);
$dirthumbzipparts = explode(".", $destthumbzip);

$destheader = $destheaderparts[0] . $table_id . '.' . $destheaderparts[1];
$dirbackground = $dirbackgroundparts[0] . $table_id . '.' . $dirbackgroundparts[1];
$dirpiczip = $dirpiczipparts[0] . $table_id . '.' . $dirpiczipparts[1];
$dirthumbzip = $dirthumbzipparts[0] . $table_id . '.' . $dirthumbzipparts[1];

// upload file set
if(copy($image['header'], $destheader) && copy($image['background'], $destbackground)  && copy($zip['photos'], $destpiczip) && copy($zip['thumbs'], $destthumbzip))
    {
        echo 'image set has been uploaded <p></p>';
    }else{
        echo 'image set DID NOT upload <p></p>';
    }


thanks for your help. has been excellent
0
 
ldbkuttyCommented:
will for sure come back to my script after sometime(now  have some other works)...meanwhile check whether Alan's script is working with the following Query:

$result = MYSQL_QUERY("UPDATE sites SET header = ".$dirheader."/header".$ID.".jpg, bg = ".$dirbackground."/background".$ID.".jpg, piczip = ".$dirpiczip."/piczip".$ID.".jpg") or die("SQL Error : ". mysql_query());
0
 
AlanJDMCommented:
Opps, sorry about that... had an extra quote.

Try this one...

$result=MYSQL_QUERY("UPDATE sites set header = ".$dirheader."/header".$ID.".jpg, bg = ,".$dirbackground."/background".$ID.".jpg, piczip= ".$dirpiczip."/piczip".$ID.".jpg");



Alan
0
 
paulp75Author Commented:
gettin error on the line

mysql_insert_id(): supplied argument is not a valid MySQL-Link resource

with yours alan

thanks
0
 
AlanJDMCommented:
Yep... I passed the wrong parameter in my example, sorry. You need to pass it the connection to the database which is returned by mysql_connect(). You didn't include you connection code in your post so I don't know what varname you used.

example....

$con = mysql_connect(server,user,pass);
mysql_insert_id($con);



Alan


0
 
paulp75Author Commented:
ok now its changing the names of the files successfully but not changing them in the database though.
any suggestions here.
i've tried changing around a few things on the update line but no luck yet.
this is the code now.
nearly there

$con = mysql_connect("localhost","***","***");
$ID = mysql_insert_id($con);
rename($destheader, $dirheader."/header".$ID.".jpg");
rename($destbackground, $dirbackground."/background".$ID.".jpg");
rename($destpiczip, $dirpiczip."/piczip".$ID.".zip");

$result=MYSQL_QUERY("UPDATE sites set header = ".$dirheader."/header".$ID.".jpg, bg = ".$dirbackground."/background".$ID.".jpg, piczip= ".$dirpiczip."/piczip".$ID.".jpg");


i also tried adding where id=$ID but that didnt work either though.
thanks for all your help. can i increase the points past 500? both of you have given such great help.
0
 
paulp75Author Commented:
looks like the error coming up is
Wrong parameter count for mysql_query()
0
 
AlanJDMCommented:
Actually, you bring up a good point that I did overlook. You do need to have where id=$ID in your query or else it will update all recs each time. As far as the error, I don't have an answer because it simply isn't true. mysql_query only requires one parameter and accepts an optional second parameter. The only thing I can suggest is trying the query without the concatination and see if that is the problem....

$result=MYSQL_QUERY("UPDATE sites set header = $dirheader/header$ID.jpg, bg = $dirbackground/background$ID.jpg, piczip= $dirpiczip/piczip$ID.jpg where id = $ID");



Alan
0
 
paulp75Author Commented:
tried that and it didnt work,
so i put ' ' around the names.
and it worked. :)

so this is the final code.

$con = mysql_connect("localhost","****","****");
$ID = mysql_insert_id($con);
rename($destheader, $dirheader."/header".$ID.".jpg");
rename($destbackground, $dirbackground."/background".$ID.".jpg");
rename($destpiczip, $dirpiczip."/piczip".$ID.".zip");
rename($destthumbzip, $dirthumbzip."/thumbzip".$ID.".zip");

$result=MYSQL_QUERY("UPDATE sites set header = '$dirheader/header$ID.jpg', bg = '$dirbackground/background$ID.jpg', piczip= '$dirpiczip/piczip$ID.jpg', thumbzip= '$dirthumbzip/thumbzip$ID.jpg' where id = $ID");


thanks heaps for all the help. much appreciated.
0
 
ldbkuttyCommented:
For a Note:

Always have mysql_error() right after mysql_query() to trace out SQL Errors (Like you said you missed '' .that would easily tracable with mysql_error() ). Use like this:

$result=MYSQL_QUERY("UPDATE sites set header = '$dirheader/header$ID.jpg', bg = '$dirbackground/background$ID.jpg', piczip= '$dirpiczip/piczip$ID.jpg', thumbzip= '$dirthumbzip/thumbzip$ID.jpg' where id = $ID") or die("SQL Error : " . mysql_error());

:-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now