Return a value from else section of if statement
Posted: Mon Oct 12, 2009 8:30 am
Hello, I'm quite new to php and am trying to make a site where you can upload files. As I don't want to over write files I've made some (probably horribly written) code to check its name and add a bracketed number on the end. It is two functions, one to check the name against a database and the other to change it (if necessary). I want to put the files url (or url-to-be) as the argument, and return the new, non-duplicating url.
If you look at the end of my code, it returns this value. The only problem is, it will only return the value if it is NOT a duplicate file - i.e. if it launches into the other function (checkBrackets) it forgets all about the fact it was going to return a function to my other lines of code.
oh yes and the way I am calling this (incase i'm doing it wrong) is:
It does work very nicely, really, its just hard to make it return something nicely!! Any help is really really appreciated, I'm very new to this and have been struggling a bit!! Many thanks in advance,
Dave
If you look at the end of my code, it returns this value. The only problem is, it will only return the value if it is NOT a duplicate file - i.e. if it launches into the other function (checkBrackets) it forgets all about the fact it was going to return a function to my other lines of code.
Code: Select all
<?php
//$WORD IS THE CURRENT FILE NAME
function checkBrackets($word)
{
//STRIP OFF (AND SAVE) THE EXTENSION
$ext = strrchr($word, ".");
$ext_length = strlen($ext);
$extless = substr($word, 0, -$ext_length);
//IF THE NEW FILE ENDS IN '(*)'
if(substr("$extless", -3, 1) === "(" && substr("$extless", -1, 1) === ")")
{
$version_number = substr("$extless", -2, 1);
//IF THE BIT BETWEEN THE NUMBER IS NUMRERIC, INCREMENT BY 1
if(is_numeric($version_number))
{
++$version_number;
$extless = substr_replace($extless,"$version_number" . ")",-2);
echo "the new name is: " . $extless . "<br/>";
$new_name = $extless . $ext;
//CHECK THIS NEW NAME IN THE DATABASE
checkDatabaseEntry($new_name,1);
}
else
{
// ELSE PUT SOME BRACKETS ON WITH A NUMBER
$extless .= "(1)";
$new_name = $extless . $ext;
checkDatabaseEntry($new_name,1);
//CHECK THIS NEW NAME IN THE DATABASE
}
}
else
{
//OR IF THERE WERE NO BRACKETS ON IT IN THE FIRST PLACE, ADD THEM
$extless .= "(1)";
//REBUILD THE FULL NAME WITH EXTENSION
$new_name = $extless . $ext;
checkDatabaseEntry($new_name,1);
}
}
function checkDatabaseEntry($url,$folder)
{
echo " / / CHECK FILE NAME: / / <br/>";
$conn_server = "----";
$conn_user = "----";
$conn_pword = "----";
$con = mysql_connect($conn_server, $conn_user, $conn_pword);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
else
{
//
}
mysql_select_db("----");
$duplicate = false;
$query = "SELECT * FROM files WHERE folder_id=$folder";
$results = mysql_query($query);
if(!$results = mysql_query("SELECT * FROM files WHERE folder_id=$folder"))
{
die(mysql_error());
}
while($line = mysql_fetch_array($results))
{
//IF THE FILE'S URL-TO-BE IS ALREADY TAKEN, MARK IT AS DUPLICATE
if($url === $line["url"]){
$duplicate = true;
mysql_close($con);
break;
}
else
{
echo "not this one...<br/>";
}
}
if($duplicate === true)
{
echo "the file name was a duplicate<br/>";
checkBrackets($url);
}
else
{
//----THIS IS THE BIT THAT ISN'T WORKING AS I'D LIKE
return $url;
//----------------------
}
}
?>Code: Select all
<?php
$folder = 1;
echo checkDatabaseEntry("http://localhost/stuf/files/Presentation_1/1(1).jpg",$folder);
?>
Dave