Skip to main content
Inspiring
November 22, 2006
Question

Taking a filename in a 'file' field for upload..

  • November 22, 2006
  • 1 reply
  • 475 views
What I'm ideally trying to achieve here is to have a 'file' field in an insert record page, where I can select a file, and upload it to a folder on the server. I've sorted that part on it's own at least.

But there also needs to be a text field, eg 'File_Name', in the table so that the page can display the image.

So my question is, is it possible to browse to a file, and have the actual file upload, but also dump the text in a 'File_Name' field automatically?

Even then, there's the issue that you wouldn't want the whole text, but just the actual file name, as obviously the paths will be different - this may be something the 'trim' command can take care of?

Hope that makes sense, and that there might be a solution...

Cheers,
Iain
This topic has been closed for replies.

1 reply

Inspiring
November 22, 2006
On Wed, 22 Nov 2006 14:04:27 +0000 (UTC), "Iain71"
<webforumsuser@macromedia.com> wrote:

>What I'm ideally trying to achieve here is to have a 'file' field in an insert
>record page, where I can select a file, and upload it to a folder on the
>server. I've sorted that part on it's own at least.
>
> But there also needs to be a text field, eg 'File_Name', in the table so that
>the page can display the image.
>
> So my question is, is it possible to browse to a file, and have the actual
>file upload, but also dump the text in a 'File_Name' field automatically?

If you have a file upload field in your form (with the generic name
file) then in your insert record you need to fill the filename field
with this:

GetSQLValueString($_FILES['file']['name'], "text"),

That's all there is to it.
--
Steve
steve at flyingtigerwebdesign dot com
Inspiring
November 22, 2006
Thanks Steve - it's almost working - I even thought it was working at first - file name is dropping nicely into the file_name field - just the name without the path which is perfect.

But the files don't seem to be uploading now. I've probably just got some things wrongly named, but I've been trying everything without success.

At the moment I have a file upload page that looks like :

<?php require_once('Connections/Photolibrary.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}



if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO photos (Photo_File) VALUES (%s)",
GetSQLValueString($_FILES['file']['name'], "text"));


mysql_select_db($database_Photolibrary, $Photolibrary);
$Result1 = mysql_query($insertSQL, $Photolibrary) or die(mysql_error());

$insertGoTo = "fileuploaded.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_Photolibrary, $Photolibrary);
$query_Recordset1 = "SELECT * FROM photos";
$Recordset1 = mysql_query($query_Recordset1, $Photolibrary) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<form name="form1" action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data">
Type (or select) Filename: <input type="file" name="file">
<input type="hidden" name="MAX_FILE_SIZE" value="25000" /> <input type="submit" value="Upload file">
<input type="hidden" name="MM_insert" value="form1">
</form>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

And a fileuploaded.php page that looks like :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<?php

move_uploaded_file ($_FILES['file'] ['tmp_name'], "Photos/{$_FILES['file'] ['name']}")

?>
File uploaded
</body>
</html>

Where 'file' is the name of the file upload field in the first page.

Any ideas?

Iain