I've got a area on my website which allows users to upload images. I rename the images, before copying them to a permanent folder, so that they can be accessed later in line with a related ID number in my database.
In order to do this, I have to find out the extension of the file, so that I can add .jpg or .gif to the end of it, but the code I'm using to do this randomly (perhaps 1 in 10 times) fails to identify the extension and add it to the end of the name in the database.
It's not the image itself, since in each of these cases, uploading a second time works fine. Is it that the method of requesting the file type is unstable, or am I doing something else wrong? I've included the relevant chunk of code below - anyone got any ideas?
// Where image file uploaded is called 'photoname', find file extensions
if ($photoname_type == "image/pjpeg")
{
$ext = ".jpg";
}
if ($photoname_type == "image/gif")
{
$ext = ".gif";
}
// code to add photo details into db & copy file to correct destination
$timest = date("Y-m-d");
$author = $cookiefirst.' '.$cookielast;
$photodesc = str_replace("'","",$photodesc);
$photodesc = str_replace('"','',$photodesc);
$phototitle = str_replace("'","\'",$phototitle);
$phototitle = str_replace('"','\"',$phototitle);
$query = "INSERT INTO photos (phototitle, photodesc, phototime, userid, contentid) VALUES ('$phototitle', '$photodesc', '$timest', '$cookieid', '$contentid');";
ExecuteQuery($linkdb, $result, $query);
$query = "UPDATE content SET photocheck = '1' WHERE t1 = '$contentid';";
ExecuteQuery($linkdb, $result, $query); }
$query = "SELECT photoid FROM photos ORDER BY photoid DESC LIMIT 1;";
if ($mysql_result = mysql_query($query, $linkdb)) {
if (ExecuteQuery($linkdb, $result, $query)) {
while ($row = NextRow($result)) {
$photoplace = $row[0];
$photonamed = $cookieid.$photoplace.$ext;
copy($photoname, "/home/sites/site113/web/photos/$photonamed");
}}}
$query = "UPDATE photos SET photo = '$photonamed' WHERE photoid = '$photoplace';";
ExecuteQuery($linkdb, $result, $query);
// end of photo just added
Many thanks for your time in advance!
Sam.