Solved

changing file name of uploaded file to mysql record id

Posted on 2004-08-10
20
313 Views
Last Modified: 2013-12-12
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
Comment
Question by:paulp75
  • 8
  • 6
  • 6
20 Comments
 
LVL 9

Expert Comment

by:AlanJDM
ID: 11761946
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
 
LVL 9

Expert Comment

by:AlanJDM
ID: 11761981
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
 
LVL 32

Assisted Solution

by:ldbkutty
ldbkutty earned 100 total points
ID: 11762122
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
 
LVL 9

Expert Comment

by:AlanJDM
ID: 11762960
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
 
LVL 32

Expert Comment

by:ldbkutty
ID: 11763510
>> 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
 
LVL 9

Expert Comment

by:AlanJDM
ID: 11763593
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
 
LVL 32

Expert Comment

by:ldbkutty
ID: 11763644
Ah... thats wonderful :-) . I Apologize.
0
 
LVL 9

Expert Comment

by:AlanJDM
ID: 11763694
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
 
LVL 6

Author Comment

by:paulp75
ID: 11769223
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
 
LVL 32

Expert Comment

by:ldbkutty
ID: 11770235
my mistake:

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

must be:

$table_id = mysql_result($result, 0, "id");
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 6

Author Comment

by:paulp75
ID: 11770627
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
 
LVL 32

Expert Comment

by:ldbkutty
ID: 11770888
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
 
LVL 9

Expert Comment

by:AlanJDM
ID: 11771593
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
 
LVL 6

Author Comment

by:paulp75
ID: 11774905
gettin error on the line

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

with yours alan

thanks
0
 
LVL 9

Accepted Solution

by:
AlanJDM earned 400 total points
ID: 11775689
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
 
LVL 6

Author Comment

by:paulp75
ID: 11780033
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
 
LVL 6

Author Comment

by:paulp75
ID: 11780611
looks like the error coming up is
Wrong parameter count for mysql_query()
0
 
LVL 9

Expert Comment

by:AlanJDM
ID: 11782838
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
 
LVL 6

Author Comment

by:paulp75
ID: 11789339
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
 
LVL 32

Expert Comment

by:ldbkutty
ID: 11789375
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now