Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 24th, 2009, 11:59 AM
Authorized User
 
Join Date: Apr 2007
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default capturing file name after upload

Hello great programmer gurus!
I have this excellent code I want use so my clients can upload photos to their database.

The photos upload successfully. What I need to do is have the file name they uploaded to be entered into the mySQL database.

I have having trouble capturing the file name to be used in the insert query...

Here is my code..... I removed all passwords and names from query for security reasons...

I currently used $buff to try to capture that file name but doesnt work...


<?
// Simple upload interface in PHP.
// Please send your comments to [email protected]
// 07.11.2001

$UPLOAD_PATH = "/hiddden for security here/"; // directory on the server where are you going to upload files
// on Unix servers directories look like "/home/users/jack/files/"
// Don't forget about final slashes!!!

$UPLOAD_NUM = 1; // the number of upload fields

// display uploaded file list or errors during uploading
// and upload files
function displayUploadedList ()
{
global $UPLOAD_PATH,
$UPLOAD_NUM,
$HTTP_POST_FILES;

$buff = '';
$error = 0;
for ($i = 0; $i < $UPLOAD_NUM; $i++)
{
$name = $HTTP_POST_FILES["File$i"]['name']; // this is the real name of your file
$tmp = $HTTP_POST_FILES["File$i"]['tmp_name']; // this is the temporary name of your file in temporary
// directory on the server

if (!is_uploaded_file ($tmp)) // is this temporary file really uploaded?
continue;

$buff .= "<li><span class=Text><b>".$name."</b></span>";




if (!@move_uploaded_file($tmp, $UPLOAD_PATH."/".$name)) // move temporary file to your upload directory
$error = 1;
}

if (strlen ($buff) == 0)
{
?>
<P align=center class=bodycopy><font color="#FF0000"><b>ALERT! Select at least one file to upload.</b></font></P>
<?
return false;
}
else if ($error)
{
?>
<P align=center class=bodycopy><font color="#FF0000"><b>Some of selected files have not been uploaded.</b></font></P><br>
<P align=center class=bodycopy>Check existence of upload directory: <b><?=$UPLOAD_PATH?></b> and your permissions.</P>
<?

return false;
}


?>
<P align=center class=bodycopy><b>The following file has been uploaded:</b></font></P>
<P class=bodycopy align=center><b><?=$buff?></b></P>
<HR width=400>
<P align=center class=bodycopy>
<b><a href="javascript:window.close();">Close Window</a></P>
<?

return true;
}

// display upload form
// this is not interesting function ;-(
function displayUploadForm () // display upload form,
{
global $UPLOAD_NUM;

$tablePre = "
<table border=0 cellspacing=0 cellpadding=0 align=center>
<tr>
<td align=left valign=top>
<table border=1 cellspacing=1 cellpadding=6 align=center>
<tr bgcolor=#FFFFFF>

";

$displayed = '';
for ($i = 0; $i < $UPLOAD_NUM; $i++)
{
$displayed .= "<td class=TableElement bgcolor=#CCFFFF><input type=file name=File$i length=25></td></tr>";
}

$tableSuf = "
</table>
</td>
</tr>
</table>
<p align=center><input type=submit name=Upload value=\"Upload Now!\" style=\"font-size: 10pt;\"></p>";

print $tablePre.$displayed.$tableSuf;
}
?>
<?

$name = $_GET["name"];


$con = mysql_connect("blocked out"," "," ");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db(" ", $con);
mysql_query("UPDATE uploadtest SET test = $buff
WHERE item = 'WUC-2442R'");
mysql_close($con);

?>


<html>
<head>
<title>"ODD LIST" Photo Upload System</title>
<link rel="stylesheet" type="text/css" href="main.css">

</head>
<body>
<BR><BR>
<table border="0" cellspacing="1" cellpadding="5" align="center">
<tr>
<td bgcolor="#FFFFFF" align=center>
<?
print "<span class=subhead><b>Photo Upload</b></span><BR>
<span class=bodycopy>Upload the photo for the item you just entered.<BR><b>Photo MUST be 640 pixels wide x 480 pixels high</b></span>";
?>
</td>
</tr>
<tr>
<form method=POST enctype="multipart/form-data" action="uploadfile.php" >
<td align="left" valign="top" bgcolor="#FFFFFF" height=100%>
<?
displayUploadForm ();
?>
</td>
</tr>
<tr>
<td align="left" valign="top" bgcolor="#FFFFFF" width="50%">
<?

if (isset ($Upload)) // if user has clicked on Upload button we must try to upload something
displayUploadedList ();
?>
</td>
</form>
</tr>
</table>


</body>
</html>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Clear the filepath details of File Upload Control after saving a file DineshGirij008 C# 2005 0 March 7th, 2009 03:52 AM
Filter File Types in ASP.NET File Upload ramuis78 ASP.NET 2.0 Basics 2 May 31st, 2007 10:50 AM
Capturing\Reading File Contents into DB with PHP aingalsbe PHP How-To 2 December 30th, 2005 09:48 AM
Whole Folder upload(Multi file Upload) ramasamy_rams XML 1 September 9th, 2005 12:43 PM
Capturing date from transferred file Axxess Access VBA 2 July 18th, 2005 05:21 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.