Jump to content

Archived

This topic is now archived and is closed to further replies.

sabadell

Viewing uploaded images problem

Recommended Posts

Hi, I am new to PHP and am working on a very simple mysql driven site using PHP4 with apache2. So far I have managed to get the script to upload images into a individual session named folders due to the fact that the real site will have a high potential for images of the same name being uploaded The session name is actually the next available ID number from the dbase. As I have said this all works correctly.

My problem is two fold:-
1. I have not been place the image name in the database nor any other info about it.
2. I have not been able to view the uploaded images at all and Dreamweaver is very little help.

I believe that if I could get the photo name atleast into the database and then bring it down as a local level variable this may help me. And secondly the actually <img scr> path is not shown anything other than the dreaded small red cross! It should be something like: <img src="../test3/uploads/<?php $ID ?>/

I hope someone can help as it has frustrated me now for a few days so any help would be appreciated.

I enclose scripts so far......

create.php...............upload.php........upload1.php

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
create.php
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

<?php session_start(); ?>
<?php require_once('../../Connections/Punks_conn.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 = $HTTP_SERVER_VARS['PHP_SELF'];
if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
$editFormAction .= "?" . $HTTP_SERVER_VARS['QUERY_STRING'];
}

if ((isset($HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO punks (FirstName, Surname, `Group`) VALUES (%s, %s, %s)",
GetSQLValueString($HTTP_POST_VARS['FirstName'], "text"),
GetSQLValueString($HTTP_POST_VARS['Surname'], "text"),
GetSQLValueString($HTTP_POST_VARS['Group'], "text"));

mysql_select_db($database_Punks_conn, $Punks_conn);
$Result1 = mysql_query($insertSQL, $Punks_conn) or die(mysql_error());

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

mysql_select_db($database_Punks_conn, $Punks_conn);
$query_Recordset1 = "SELECT Max(PropID)+1 FROM punks ";
$Recordset1 = mysql_query($query_Recordset1, $Punks_conn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<body>
<?php
$_SESSION[mySession] = $row_Recordset1['Max(PropID)+1'];
echo $_SESSION[mySession];
?>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">

<table align="center">



<tr valign="baseline">
<td nowrap align="right">First Name:</td>
<td><input type="text" name="FirstName" value="" size="32"></td>
</tr>



<tr valign="baseline">
<td nowrap align="right">Surname:</td>
<td><input type="text" name="Surname" value="" size="32"></td>
</tr>



<tr valign="baseline">
<td nowrap align="right">Group:</td>
<td><input type="text" name="Group" value="" size="32"></td>
</tr>



<tr valign="baseline">

<td nowrap align="right"> </td>
<td><input type="submit" value="Insert Record"></td>
</tr>


</table>
<input type="hidden" name="MM_insert" value="form1">


</form>
<p> </p>

<p> </p>
<p><br>


</p>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>


::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Upload.php
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
<?php session_start(); ?>
<?php require_once('../../Connections/Punks_conn.php'); ?>
<?php
mysql_select_db($database_Punks_conn, $Punks_conn);
$query_Recordset1 = "SELECT Max(PropID)-1 FROM punks ";
$Recordset1 = mysql_query($query_Recordset1, $Punks_conn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);?>



<html>
<head>
<title>upload.php</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
echo $_SESSION[mySession]-1;
$ID = $_SESSION[mySession]-1;
?>

<FORM ENCTYPE="multipart/form-data" ACTION="upload1.php" METHOD=POST>
<p> Session No:<?php echo $_SESSION[mySession]-1;?>
</p>
<p>Upload this file:
<INPUT NAME="file" TYPE="file" id="file">
<INPUT TYPE="submit" VALUE="Send File">
<input name="IDno" type="hidden" id="IDno" value="<?php echo $ID ?> ">
</p>
</FORM>

<?php
// if(!is_dir("../test3/uploads/".$ID)){//this line tests for the existance of a same named directory
// mkdir("../test3/uploads/".$ID,0777); //global writing properties & trying to add on session ID value & $ID
// return;
// }


?>

</body>
</html>
<?php
mysql_free_result($Recordset1);
?>


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
upload1.php
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

<?php session_start()?>


<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php $ID = trim($_SESSION[mySession]-1); ?>


<p><a href="create.php">Punk creator
</a>

<p>This is session number: <?php echo $_SESSION[mySession]-1;?>
<p>

<?php
/* $file_dir = "../test3/uploads/$ID"; */
/* This upload works if the $ID is removed */

// this is the new line as suggested in codewalkers.
//$file_dir = "../test3/uploads/$ID";
//if( !file_exists( $file_dir ) )
// mkdir( $file_dir );

//folder creation using chmod advice from codewalkers - change in permissions to 775
$file_dir = "../test3/uploads/$ID";
if( !file_exists( $file_dir ) )
{
mkdir( $file_dir );
chmod( $file_dir, 0777 ); // 775 should be enough, I think. You can try 775
// and even 777 if this doesn't work.
}
?>





<?php
foreach($_FILES as $file_name => $file_array) {
echo "path: ".$file_array['tmp_name']."<br>\n";
echo "name: ".$file_array['name']."<br>\n";
echo "type: ".$file_array['type']."<br>\n";
echo "size: ".$file_array['size']."<br>\n";

if (is_uploaded_file($file_array['tmp_name'])) {
move_uploaded_file($file_array['tmp_name'], "$file_dir/$file_array[name]") or die ("Couldn't copy");
echo "file was uploaded!<br><br>";
}
}

?>




<p>


<p>

</body>
</html>

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Displaying the image
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

I created $ID in the display as a local variable via...

$ID = $row_Recordset1['PropID'] //PropID is ID number of dbase and individual upload folder name.

The path to the pictures should I believe be something like.... <img src="../test3/uploads/<?php $ID ?>/ with $ID being the session named folder but this doesn't bring up an image.


Thanks in advance for any help.



Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.