|
Posted by ELINTPimp on 05/01/07 21:02
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_management\\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'> </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();
}
*/
Navigation:
[Reply to this message]
|