By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,319 Members | 2,361 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,319 IT Pros & Developers. It's quick & easy.

Possible bug? Strange problem trying to return a value from a function

P: n/a
Hello all,

Have a really interesting problem (at least to me) with my
upload_file() function. I have it working now, with a bit of a work
around, but would like to know what everyone thinks in regards to this
being a bug or perhaps my ignorance. Basically the function takes two
arguments, one is the desired file path I want the upload file to be
moved to, and the second is an arbitrary integer. If the file doesn't
already exist, the file is moved just fine, it returns the file name,
and it the CSV file is displayed via the parse_csv function. However,
when the file already exists in the upload directory, the file name is
appended with "_".$i, and returns the name and $i++ value to the
upload_file() function and loops around until a unique name is found.
When one is found, it DOES move the file and assign the file path/name
to the $target_path variable (verified with an echo statement and
seeing the files in the directory) but it refuses to return the value
of $target_path from the function. This only occurs if the file name
originally existed and needed to go through my re-naming process.

Basically what I had to do to get it to work was to assign the file
path/name to the global $file variable and pass that to the parse_csv
function. I was never able to get: return $target_path; to work when
the file name originally existed and I had to append the name.

My question is why this wasn't working? Thoughts are that it may be a
possibly PHP bug with the way it points to the memory when returning
the value.

Thanks for the help,

Steve Siebert

try {
$i = 0;
$file = "D:\\bnm\\www\\systems_management\\asset_managemen t\\uploads\
\".basename($_FILES['upload_asset']['name']);
$file_path = upload_file($file, $i);
parse_csv($file_path);
}
catch (Exception $e) {
echo "ERROR UPLOADING FILE: ".$e->getMessage()."\n";
}
}
function parse_csv ($file_path) {

$row = 1;
echo "<table rules='all' cellpadding='0' cellspacing='0'>";
if ($handle = fopen($file_path, "r")) {
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
echo "<tr>\n";
$num = count($data);
for ($c=0; $c < $num; $c++) {
if ($row == 1) {
echo "<td class='csv_head'>".$data[$c]."</td>\n";
}
else {
if (is_null($data[$c])) {
echo "<td class='csv_data'>&nbsp;</td>\n";
}
else {
echo "<td class='csv_data'>".$data[$c]."</td>\n";
}
}
}
echo "</tr>\n";
$row++;
}
echo "</table>";
echo $row." records returned.";
}
fclose($handle);
} // end parse_csv() function

function upload_file($target_path, $i) {

if (!file_exists($target_path))
{
if (move_uploaded_file($_FILES['upload_asset']['tmp_name'],
$target_path)) {
return $target_path;
}
else {
throw new Exception ("Unable to move file to upload directory.
Upload filed.");
}
}
else {
/* ## if the uploaded file name is already used, add a "version
number" to the end of the file.
## This loops through the upload_file() function until the file
has a unique name. */

if (!isset($file)) {
global $file;
}
list($new_name, $ext) = explode('.', $file);
$new_name = $new_name."_".$i.".".$ext;
$i++;
upload_file($new_name, $i);
}

} // end upload_file() function

Some debugging code I tried within the upload_file() function (didn't
help):

/*
try {
$foo = move_uploaded_file($_FILES['upload_asset']['tmp_name'],
$target_path);
if ($foo == true) {
echo "Hurray!";
if (file_exists($target_path)) {
echo "File Exists";
$file_path = $target_path;
return $file_path;
}
else {
echo "File Doesn't Exists";
}
}
else {
echo "Boo!";
throw new Exception ("Could not move file");
}
}
catch (Exception $e) {
echo "ERROR: ".$e->getMessage();
}
*/

May 1 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ELINTPimp kirjoitti:
Hello all,

Have a really interesting problem (at least to me) with my
upload_file() function. I have it working now, with a bit of a work
around, but would like to know what everyone thinks in regards to this
being a bug or perhaps my ignorance. Basically the function takes two
arguments, one is the desired file path I want the upload file to be
moved to, and the second is an arbitrary integer. If the file doesn't
already exist, the file is moved just fine, it returns the file name,
and it the CSV file is displayed via the parse_csv function. However,
when the file already exists in the upload directory, the file name is
appended with "_".$i, and returns the name and $i++ value to the
upload_file() function and loops around until a unique name is found.
When one is found, it DOES move the file and assign the file path/name
to the $target_path variable (verified with an echo statement and
seeing the files in the directory) but it refuses to return the value
of $target_path from the function. This only occurs if the file name
originally existed and needed to go through my re-naming process.

Basically what I had to do to get it to work was to assign the file
path/name to the global $file variable and pass that to the parse_csv
function. I was never able to get: return $target_path; to work when
the file name originally existed and I had to append the name.

My question is why this wasn't working? Thoughts are that it may be a
possibly PHP bug with the way it points to the memory when returning
the value.

Thanks for the help,

Steve Siebert

try {
$i = 0;
$file = "D:\\bnm\\www\\systems_management\\asset_managemen t\\uploads\
\".basename($_FILES['upload_asset']['name']);
$file_path = upload_file($file, $i);
parse_csv($file_path);
}
catch (Exception $e) {
echo "ERROR UPLOADING FILE: ".$e->getMessage()."\n";
}
}
function parse_csv ($file_path) {

$row = 1;
echo "<table rules='all' cellpadding='0' cellspacing='0'>";
if ($handle = fopen($file_path, "r")) {
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
echo "<tr>\n";
$num = count($data);
for ($c=0; $c < $num; $c++) {
if ($row == 1) {
echo "<td class='csv_head'>".$data[$c]."</td>\n";
}
else {
if (is_null($data[$c])) {
echo "<td class='csv_data'>&nbsp;</td>\n";
}
else {
echo "<td class='csv_data'>".$data[$c]."</td>\n";
}
}
}
echo "</tr>\n";
$row++;
}
echo "</table>";
echo $row." records returned.";
}
fclose($handle);
} // end parse_csv() function

function upload_file($target_path, $i) {

if (!file_exists($target_path))
{
if (move_uploaded_file($_FILES['upload_asset']['tmp_name'],
$target_path)) {
return $target_path;
}
else {
throw new Exception ("Unable to move file to upload directory.
Upload filed.");
}
}
else {
/* ## if the uploaded file name is already used, add a "version
number" to the end of the file.
## This loops through the upload_file() function until the file
has a unique name. */

if (!isset($file)) {
global $file;
}
list($new_name, $ext) = explode('.', $file);
$new_name = $new_name."_".$i.".".$ext;
$i++;
upload_file($new_name, $i);
you need to return this, ie. return uploaded_file(...);
}

} // end upload_file() function
I think doing this with recursion is a bit unnecessary, a simple loop
would do fine, but since you want to use recursion, at least use it
right and return the recursive call.

--
Ra*********@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
May 1 '07 #2

P: n/a
On May 1, 5:32 pm, Rami Elomaa <rami.elo...@gmail.comwrote:
ELINTPimpkirjoitti:
Hello all,
Have a really interesting problem (at least to me) with my
upload_file() function. I have it working now, with a bit of a work
around, but would like to know what everyone thinks in regards to this
being a bug or perhaps my ignorance. Basically the function takes two
arguments, one is the desired file path I want the upload file to be
moved to, and the second is an arbitrary integer. If the file doesn't
already exist, the file is moved just fine, it returns the file name,
and it the CSV file is displayed via the parse_csv function. However,
when the file already exists in the upload directory, the file name is
appended with "_".$i, and returns the name and $i++ value to the
upload_file() function and loops around until a unique name is found.
When one is found, it DOES move the file and assign the file path/name
to the $target_path variable (verified with an echo statement and
seeing the files in the directory) but it refuses to return the value
of $target_path from the function. This only occurs if the file name
originally existed and needed to go through my re-naming process.
Basically what I had to do to get it to work was to assign the file
path/name to the global $file variable and pass that to the parse_csv
function. I was never able to get: return $target_path; to work when
the file name originally existed and I had to append the name.
My question is why this wasn't working? Thoughts are that it may be a
possibly PHP bug with the way it points to the memory when returning
the value.
Thanks for the help,
Steve Siebert
try {
$i = 0;
$file = "D:\\bnm\\www\\systems_management\\asset_managemen t\\uploads\
\".basename($_FILES['upload_asset']['name']);
$file_path = upload_file($file, $i);
parse_csv($file_path);
}
catch (Exception $e) {
echo "ERROR UPLOADING FILE: ".$e->getMessage()."\n";
}
}
function parse_csv ($file_path) {
$row = 1;
echo "<table rules='all' cellpadding='0' cellspacing='0'>";
if ($handle = fopen($file_path, "r")) {
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
echo "<tr>\n";
$num = count($data);
for ($c=0; $c < $num; $c++) {
if ($row == 1) {
echo "<td class='csv_head'>".$data[$c]."</td>\n";
}
else {
if (is_null($data[$c])) {
echo "<td class='csv_data'>&nbsp;</td>\n";
}
else {
echo "<td class='csv_data'>".$data[$c]."</td>\n";
}
}
}
echo "</tr>\n";
$row++;
}
echo "</table>";
echo $row." records returned.";
}
fclose($handle);
} // end parse_csv() function
function upload_file($target_path, $i) {
if (!file_exists($target_path))
{
if (move_uploaded_file($_FILES['upload_asset']['tmp_name'],
$target_path)) {
return $target_path;
}
else {
throw new Exception ("Unable to move file to upload directory.
Upload filed.");
}
}
else {
/* ## if the uploaded file name is already used, add a "version
number" to the end of the file.
## This loops through the upload_file() function until the file
has a unique name. */
if (!isset($file)) {
global $file;
}
list($new_name, $ext) = explode('.', $file);
$new_name = $new_name."_".$i.".".$ext;
$i++;
upload_file($new_name, $i);

you need to return this, ie. return uploaded_file(...);
}
} // end upload_file() function

I think doing this with recursion is a bit unnecessary, a simple loop
would do fine, but since you want to use recursion, at least use it
right and return the recursive call.

--
Rami.Elo...@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
Duh...and thanks.

May 16 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.