Ciao a tutti,
devo integrare l'opzione di upload immagini nel sistema funzionante che ho già scritto per l'editing dei contenuti che allego qui:
codice:
<div id="EditPages"> <table width="800" border="1" align="center">
<tr>
<td><form action="<?php echo $editFormAction; ?>" method="POST" name="EditPageForm" id="EditPageForm">
<table width="400" height="556" border="1" align="center">
<tr>
<td><span id="sprytextarea1">
<label for="PageContent"></label>
Page Content:<br>
<textarea name="PageContent" id="PageContent" cols="45" rows="5"><?php echo $row_EditPage['PageContent']; ?></textarea>
<span class="textareaRequiredMsg">A value is required.</span></span></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td><span id="sprytextarea2">
<label for="Keywords"></label>
Keywords:<br>
<textarea name="Keywords" id="Keywords" cols="45" rows="5"><?php echo $row_EditPage['Keywords']; ?></textarea>
<span class="textareaRequiredMsg">A value is required.</span></span></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td height="142"><span id="sprytextarea3">
<label for="MetaDesc"></label>
Meta Desc:<br>
<textarea name="MetaDesc" id="MetaDesc" cols="45" rows="5"><?php echo $row_EditPage['MetaDesc']; ?></textarea>
<span class="textareaRequiredMsg">A value is required.</span></span></td>
</tr>
<tr>
<td><label for="UploadFileField"></label>
<input type="file" name="UploadFileField" id="UploadFileField" value="<?php echo $row_EditPage['Image']; ?>" /></td>
</tr>
<tr>
<td><input type="submit" name="Update" id="Update" value="Update">
<input name="IDhiddenField" type="hidden" id="IDhiddenField" value="<?php echo $row_EditPage['ID']; ?>"></td>
</tr>
<tr>
<td height="55"> </td>
</tr>
<tr>
<td> </td>
</tr>
</table>
<input type="hidden" name="MM_update" value="EditPageForm">
</form></td>
</tr>
</table>
</div>
Il codice php che utlizzo è questo:
Codice PHP:
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($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_update"])) && ($_POST["MM_update"] == "EditPageForm")) {
$updateSQL = sprintf("UPDATE pages SET PageContent=%s, Keywords=%s, MetaDesc=%s WHERE ID=%s",
GetSQLValueString($_POST['PageContent'], "text"),
GetSQLValueString($_POST['Keywords'], "text"),
GetSQLValueString($_POST['MetaDesc'], "text"),
GetSQLValueString($_POST['IDhiddenField'], "int"));
$localhost->select_db($database_localhost); $Result1 = $localhost->query($updateSQL) or die($localhost->error);
$updateGoTo = "addPages.php"; if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $updateGoTo));}
$colname_EditPage = "-1";if (isset($_GET['ID'])) {
$colname_EditPage = $_GET['ID'];}$localhost->select_db($database_localhost);$query_EditPage = sprintf("SELECT * FROM pages WHERE ID = %s", GetSQLValueString($colname_EditPage, "int"));
$EditPage = $localhost->query($query_EditPage) or die($localhost->error);$row_EditPage = $EditPage->fetch_assoc();$totalRows_EditPage = $EditPage->num_rows;
?>
Funziona tutto perfettamente ma come vedete dalla tabella iniziale voglio inserire un upload immagini che andrà a salvare le stesse in una cartella e inserirà il percorso nel database ma solo alla riga con l'ID specifico di quella pagina
Ecco il codice per l'upload:
Codice PHP:
<?php if(isset($_FILES['UploadFileField'])){
// Creates the Variables needed to upload the file
$UploadName = $_FILES['UploadFileField']['name'];
$UploadName = mt_rand(100000, 999999).
$UploadName;
$UploadTmp = $_FILES['UploadFileField']['tmp_name'];
$UploadType = $_FILES['UploadFileField']['type'];
$FileSize = $_FILES['UploadFileField']['size'];
// Removes Unwanted Spaces and characters from the files names of the files being uploaded
$UploadName = preg_replace("#[^a-z0-9.]#i", "", $UploadName);
// Upload File Size Limit
if(($FileSize > 250000)){ die("Error - File to Big");
}
// Checks a File has been Selected and Uploads them into a Directory on your Server
if(!$UploadTmp){ die("No File Selected, Please Upload Again");
}
else{
$move=move_uploaded_file($UploadTmp, 'images/'.$UploadName);
mysqli_query($localhost,"INSERT INTO pages (Image) VALUES ('/pages/images/.$UploadName')");mysqli_close($localhost);
}
if($move) { echo("file ".$UploadName." has been uploaded!");
echo("<br>/pages/images/".$UploadName); }
else { exit("unable to upload the file" .$UploadName);
}
}
?>
Vorrei un consiglio su come integrare questo codice di upload in modo che il tutto possa funzionare.
Grazie un saluto.