Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

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

Enthusiast ,
Nov 22, 2006 Nov 22, 2006

Copy link to clipboard

Copied

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
TOPICS
Server side applications

Views

402
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 22, 2006 Nov 22, 2006

Copy link to clipboard

Copied

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 22, 2006 Nov 22, 2006

Copy link to clipboard

Copied

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 22, 2006 Nov 22, 2006

Copy link to clipboard

Copied

On Wed, 22 Nov 2006 16:29:40 +0000 (UTC), "Iain71"
<webforumsuser@macromedia.com> wrote:

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

Is the directory to move the file to called Photos or photos? If your
server is *nix the case is important.
--
Steve
steve at flyingtigerwebdesign dot com

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 22, 2006 Nov 22, 2006

Copy link to clipboard

Copied

LATEST
It is Photos, I'm pretty sure.

I had another copy of my original form as well, and that one is actually working great - so thanks for posting that. It's another of these things that isn't that tricky if you know what you're doing, but at the moment I'm doing this at my job which isn't really part of my job, and I'm only really trying to pursue this sort of stuff as the opportunity presents itself as I do a bit in my own time all self taught - hopefully I'll wangle a course out of it at some point.

Iain

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines