|
Posted by Chris on 07/12/06 18:27
I'm not fully understanding the move_uploaded_file function. PHP.net is
either vague about usage (unclear examples) or overly technical in their
language (as though whoever is looking for info has already been programming
for 10 years). Hence, I usually just get more confused after reading their
'explanations'. The point is that in most cases from tutorials, books, etc.
move_uploaded_file is supposed to move the file to a new location from the
temp_dir. Tutorials seem to focus on people only uploading image files and
leaving them in an arbitrary folders where all uploads reside. I need to
move the files from the root temp directory to as many as 3 subfolder levels
(Home/category or Home/group/project/category) depending on the selections
the user makes. However, it is defined as a boolean and it appears only to
be used in an 'if' statement: i.e:
if(move_uploaded_file($_FILES['uploadedfile']['name'], $target_path)) {
do something (like insert data into db and send success message?)
}
So, in order to move the file from the temp directory to it's permanent home
I'm getting lost between the db tables/queries, the form selections (values,
etc.). Here is my form code - it seems kind of long and you may get lost -
I know I have. Note that this form is covering both uploads and links to
off-site locations that go into different tables and that files may also be
cross listed with different projects. Any guidance/help with this is so
appreciated (supposed to have it finalized by tomorrow!).
Thanks
Chris
Form code:
<?php
//Get projects from db
mysql_select_db($database_website, $website);
$query_projects = "SELECT proj.projCode, proj.projID FROM proj WHERE
proj.archive IS NULL";
$projects = mysql_query($query_projects, $website) or die(mysql_error());
$row_projects = mysql_fetch_assoc($projects);
$totalRows_projects = mysql_num_rows($projects);
//Get groups from db
mysql_select_db($database_website, $website);
$query_groups = "SELECT groupCode, groups.groupID, proj.projID,
proj.projCode FROM groups, proj WHERE proj.groupID = groups.groupID;";
$groups = mysql_query($query_groups, $website) or die(mysql_error());
$row_groups = mysql_fetch_assoc($groups);
$totalRows_groups = mysql_num_rows($groups);
mysql_select_db($database_website, $website);
$query_tooltypes = "SELECT toolType.toolTypeID, toolType.toolType FROM
toolType";
$tooltypes = mysql_query($query_tooltypes, $website) or die(mysql_error());
$row_tooltypes = mysql_fetch_assoc($tooltypes);
$totalRows_tooltypes = mysql_num_rows($tooltypes);
mysql_select_db($database_website, $website);
$query_toolcats = "SELECT toolCat.toolCatID, toolCat.toolCat FROM toolCat";
$toolcats = mysql_query($query_toolcats, $website) or die(mysql_error());
$row_toolcats = mysql_fetch_assoc($toolcats);
$totalRows_toolcats = mysql_num_rows($toolcats);
?>
<form enctype="multipart/form-data" action="upload.php" method="post">
<!--//Create the file and description inputs. -->
<table width="100%" border="0" cellspacing="5" cellpadding="0">
<tr>
<td width="43%"> <fieldset>
<legend>Document to Upload</legend><br />Upload a document from your
computer.
<p><strong>File:</strong>
<input type="file" name="uploadedfile" />
</p></fieldset></td>
<td width="57%"><fieldset>
<legend>External link</legend>
<br />
Copy and paste URL here if document is located outside of Debug Tools
site. Be sure to include entire URL, i.e. <em>https://www.site.it.etc.<br />
</em> <strong>Link URL:</strong>
<input name="textfield" type="text" size="40" />
</fieldset></td>
</tr>
</table>
<br />
<table width="100%" border="0" cellpadding="0" cellspacing="5">
<tr>
<td><fieldset>
<legend>Basic Document Information</legend>
<strong>Document Title</strong>:
<input name="title" type="text" size="40" maxlength="50" />
<br />
(Required - no more than 50 characters)<br />
<br />
<strong>Description:</strong>(Required -150 characters) <br />
<textarea name="description" cols="30" rows="5"></textarea>
<br />
<input type="checkbox" name="tool" />
<strong>Tool</strong> (select this if document is a tool)
</fieldset></td>
<td><fieldset>
<legend>Project Data</legend>
</p>
<strong>Primary Project:</strong>
<select name="project">
<option value="value">Select a project...</option>
<?php
do {
?>
<option value="<?php echo $row_projects['projID']?>"><?php echo
$row_projects['projCode']?></option>
<?php
} while ($row_projects = mysql_fetch_assoc($projects));
$rows = mysql_num_rows($projects);
if($rows > 0) {
mysql_data_seek($projects, 0);
$row_projects = mysql_fetch_assoc($projects);
}
?>
</select>
</p>
<p><b> Associated Project/Projects:</b> <em><font size="-1"><br />
(Hold down Shift key to select more than one)</font> </em> <br />
<select name="types[]" multiple="multiple" size="5">
<?php
$query = "SELECT proj.ProjID, proj.projName FROM proj ORDER BY proj.projID
ASC";
$result = @mysql_query ($query);
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<option value=\"$row[0]\"";
if (isset($_POST['types']) &&
(in_array($row[0], $_POST
['types']))) {
echo ' selected="selected"';
}
echo ">$row[1]</option>\n";
}
?>
</select>
</fieldset></td>
</tr>
</table>
<br />
<fieldset>
<legend>Tools</legend>
IMPORTANT: Complete this section only if document/link is a Tool,
otherwise continue to <a href="#Standard">Standard Document/Link
Category</a> section)<br /><br />
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="54%"><strong>Tool Type:</strong> (Select one)
<br />
<?php do{ ?>
<label>
<input type="radio" name="toolType" value="<?php echo
$row_tooltypes['toolTypeID']?>" />
<?php echo $row_tooltypes['toolType']?></label>
<?php }
while ($row_tooltypes = mysql_fetch_assoc($tooltypes)); ?>
</td>
<td width="46%"><strong>Tool Category: </strong>(Select one) <br />
<?php do{ ?>
<label>
<input type="radio" name="toolCat" value="<?php echo
$row_toolcats['toolCatID']?>" />
<?php echo $row_toolcats['toolCat']?></label>
<?php }
while ($row_toolcats = mysql_fetch_assoc($toolcats)); ?>
</td>
</tr>
</table>
</fieldset>
<div align="center">
<input type="submit" name="submit" value="Submit" />
</div>
</form>
<?php
mysql_close();
mysql_free_result($categories);
mysql_free_result($projects);
mysql_free_result($subcat);
mysql_free_result($groups);
mysql_free_result($tooltypes);
mysql_free_result($toolcats);
?>
This is the script for moving files and inserting db data (I have only
tackled an uploaded file that is a tool so far):
<?php
mysql_select_db($database_website, $website);
//set variables:
$root = $_SERVER['DOCUMENT_ROOT'];
$uploadedname = $_FILES['uploadedfile']['name'];
$tooltitle = $_POST['title'];
$tooldesc = $_POST['description'];
$tooltype = $_POST['toolType'];
$toolcat = $_POST['toolCat'];
$primeproj = $_POST['project'];
$projCode = $row_projects['projCode'];
$groupName = $row_groups['groupName'];
//include header and left nav bar
include ('includes/header.php');
//check that form has been submitted
if (isset($_POST['upload'])) {
//check if file uploaded
if (isset($_POST['uploadedfile'])) {
//check for type of upload
if (isset($_POST['tool'])) {
// Create target path
if ($primeproj == 1) {
$target_path = $root."/Tools/".$tooltype."/".$toolcat."/";
else
$target_path =
$root."/".$groupname."/"$projcode."/Tools/".$toolnype."/".$toolcat."/";
}
//Move the uploaded Tool document
move_uploaded_file($_FILES['uploadedfile']['name'], $target_path)
if(move_uploaded_file($_FILES['uploadedfile']['name'], $target_path)) {
//insert data into tools table
mysql_select_db($database_website, $website);
$tool_insert = 'INSERT INTO tools (toolURL, toolName, toolDesc,
toolTypeID, toolCatID, projID, locationID) VALUES ('', $uploadedname,
$tooldesc, $tooltype, $toolcat, $primeproj, '1')'
//check if multiple projects
if (isset($_POST['types']) && (is_array($_POST['types']))) {
$type = TRUE;
$query = 'INSERT INTO proj_assoc (toolID, projID) VALUES ';
foreach ($_POST['types'] as $v) {
$query .= "($toolID, $v), ";
}
}
echo "The file ". basename( $_FILES['uploadedfile']['name'])." has
been uploaded";
}
else{//Start error statement
echo "There was an error uploading the file, please try again!";
}
}
}
Navigation:
[Reply to this message]
|